Skip to content

Commit

Permalink
feat(compiler): add basic support for in ivy/i18n code generation (an…
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe authored and leo6104 committed Mar 25, 2018
1 parent 0493dd8 commit 9405d84
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 83 deletions.
10 changes: 8 additions & 2 deletions packages/compiler/src/aot/compiler_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ export function createAotCompiler(
const symbolResolver = new StaticSymbolResolver(compilerHost, symbolCache, summaryResolver);
const staticReflector =
new StaticReflector(summaryResolver, symbolResolver, [], [], errorCollector);
const htmlParser = new I18NHtmlParser(
new HtmlParser(), translations, options.i18nFormat, options.missingTranslation, console);
let htmlParser: I18NHtmlParser;
if (!!options.enableIvy) {
// Ivy handles i18n at the compiler level so we must use a regular parser
htmlParser = new HtmlParser() as I18NHtmlParser;
} else {
htmlParser = new I18NHtmlParser(
new HtmlParser(), translations, options.i18nFormat, options.missingTranslation, console);
}
const config = new CompilerConfig({
defaultEncapsulation: ViewEncapsulation.Emulated,
useJit: false,
Expand Down
12 changes: 9 additions & 3 deletions packages/compiler/src/constant_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ class KeyVisitor implements o.ExpressionVisitor {
`EX:${ast.value.runtime.name}`;
}

visitReadVarExpr = invalid;
visitReadVarExpr(ast: o.ReadVarExpr): string {
if (!ast.name) {
invalid(ast);
}
return ast.name as string;
}

visitWriteVarExpr = invalid;
visitWriteKeyExpr = invalid;
visitWritePropExpr = invalid;
Expand All @@ -257,9 +263,9 @@ class KeyVisitor implements o.ExpressionVisitor {

function invalid<T>(arg: o.Expression | o.Statement): never {
throw new Error(
`Invalid state: Visitor ${this.constructor.name} doesn't handle ${o.constructor.name}`);
`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
}

function isVariable(e: o.Expression): e is o.ReadVarExpr {
return e instanceof o.ReadVarExpr;
}
}
69 changes: 37 additions & 32 deletions packages/compiler/src/output/output_ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class ReadVarExpr extends Expression {
this.builtin = null;
} else {
this.name = null;
this.builtin = <BuiltinVar>name;
this.builtin = name;
}
}

Expand Down Expand Up @@ -1486,7 +1486,11 @@ export function literal(
}

// The list of JSDoc tags that we currently support. Extend it if needed.
export const enum JSDocTagName {Desc = 'desc', Id = 'id', Meaning = 'meaning'}
export const enum JSDocTagName {
Desc = 'desc',
Id = 'id',
Meaning = 'meaning',
}

/*
* TypeScript has an API for JSDoc already, but it's not exposed.
Expand All @@ -1496,42 +1500,43 @@ export const enum JSDocTagName {Desc = 'desc', Id = 'id', Meaning = 'meaning'}
*/
export type JSDocTag = {
// `tagName` is e.g. "param" in an `@param` declaration
tagName: JSDocTagName | string;
tagName: JSDocTagName | string,
// Any remaining text on the tag, e.g. the description
text?: string;
} | {// no `tagName` for plain text documentation that occurs before any `@param` lines
tagName?: undefined
text?: string,
} | {
// no `tagName` for plain text documentation that occurs before any `@param` lines
tagName?: undefined,
text: string,
};

/*
* Serializes a `Tag` into a string.
* Returns a string like " @foo {bar} baz" (note the leading whitespace before `@foo`).
*/
function tagToString(tag: JSDocTag): string {
let out = '';
if (tag.tagName) {
out += ` @${tag.tagName}`;
}
if (tag.text) {
if (tag.text.match(/\/\*|\*\//)) {
throw new Error('JSDoc text cannot contain "/*" and "*/"');
}
out += ' ' + tag.text.replace(/@/g, '\\@');
/*
* Serializes a `Tag` into a string.
* Returns a string like " @foo {bar} baz" (note the leading whitespace before `@foo`).
*/
function tagToString(tag: JSDocTag): string {
let out = '';
if (tag.tagName) {
out += ` @${tag.tagName}`;
}
if (tag.text) {
if (tag.text.match(/\/\*|\*\//)) {
throw new Error('JSDoc text cannot contain "/*" and "*/"');
}
return out;
out += ' ' + tag.text.replace(/@/g, '\\@');
}
return out;
}

function serializeTags(tags: JSDocTag[]): string {
if (tags.length === 0) return '';
function serializeTags(tags: JSDocTag[]): string {
if (tags.length === 0) return '';

let out = '*\n';
for (const tag of tags) {
out += ' *';
// If the tagToString is multi-line, insert " * " prefixes on subsequent lines.
out += tagToString(tag).replace(/\n/g, '\n * ');
out += '\n';
}
out += ' ';
return out;
let out = '*\n';
for (const tag of tags) {
out += ' *';
// If the tagToString is multi-line, insert " * " prefixes on subsequent lines.
out += tagToString(tag).replace(/\n/g, '\n * ');
out += '\n';
}
out += ' ';
return out;
}
2 changes: 1 addition & 1 deletion packages/compiler/src/render3/r3_identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ export class Identifiers {
static NgOnChangesFeature: o.ExternalReference = {name: 'ɵNgOnChangesFeature', moduleName: CORE};

static listener: o.ExternalReference = {name: 'ɵL', moduleName: CORE};
}
}

0 comments on commit 9405d84

Please sign in to comment.