Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building blocks for ivy/i18n + demo #22654

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/compiler/src/aot/compiler_factory.ts
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
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
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
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};
}
}