Repository navigation
fix(compiler): fix i18n IDs generation, add a migration tool #17638
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
Changes from 1 commit
12d930c
a3c6a25
ed6b306
2f07c4a
8ac8b82
6a4960a
f9e9d51
d297238
c572371
e3120af
e4d7184
acce4cb
0d9a67a
0f842fd
ccb2ae9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "8136548302122759730": { | ||
| "ids": [ | ||
| "76e1eccb1b772fa9f294ef9c146ea6d0efa8a2d4" | ||
| ], | ||
| "sources": [ | ||
| { | ||
| "filePath": "src/basic.ts", | ||
| "startLine": 1, | ||
| "startCol": 44, | ||
| "endLine": 1, | ||
| "endCol": 44 | ||
| } | ||
| ] | ||
| }, | ||
| "3492007542396725315": { | ||
| "ids": [ | ||
| "65cc4ab3b4c438e07c89be2b677d08369fb62da2" | ||
| ], | ||
| "sources": [ | ||
| { | ||
| "filePath": "src/basic.ts", | ||
| "startLine": 5, | ||
| "startCol": 35, | ||
| "endLine": 5, | ||
| "endCol": 35 | ||
| } | ||
| ] | ||
| }, | ||
| "126808141597411718": { | ||
| "ids": [ | ||
| "b0a17f08a4bd742b2acf39780c257c2f519d33ed" | ||
| ], | ||
| "sources": [ | ||
| { | ||
| "filePath": "node_modules/third_party/other_comp.d.ts", | ||
| "startLine": 1, | ||
| "startCol": 11, | ||
| "endLine": 2, | ||
| "endCol": 11 | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,21 +3,27 @@ | |
| // For TypeScript 1.8, we have to lay out generated files | ||
| // in the same source directory with your code. | ||
| "genDir": ".", | ||
| "debug": true | ||
| "debug": true, | ||
| "enableSummariesForJit": true, | ||
| "alwaysCompileGeneratedCode": true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we have to change the tsconfig here ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because of a change that @tbosch made on master, without these parameters the tests won't work
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| }, | ||
|
|
||
| "compilerOptions": { | ||
| "target": "es5", | ||
| "experimentalDecorators": true, | ||
| "noImplicitAny": true, | ||
| "strictNullChecks": true, | ||
| "skipLibCheck": true, | ||
| "moduleResolution": "node", | ||
| "outDir": "./out", | ||
| "rootDir": "", | ||
| "declaration": true, | ||
| "lib": ["es6", "dom"], | ||
| "baseUrl": ".", | ||
| // Prevent scanning up the directory tree for types | ||
| "typeRoots": ["node_modules/@types"] | ||
| "typeRoots": ["node_modules/@types"], | ||
| "noUnusedLocals": true, | ||
| "sourceMap": true, | ||
| "outDir": "./out" | ||
| }, | ||
|
|
||
| "files": [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,3 +64,8 @@ export function normalizeMissingTranslation(missingTranslation?: string | null): | |
| } | ||
| return MissingTranslationStrategy.Warning; | ||
| } | ||
|
|
||
| /** @internal */ | ||
| export function normalizeMapping(mapping?: string | boolean | null): boolean { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove ? from here for consistency |
||
| return typeof mapping !== 'undefined' && mapping !== 'false' && mapping !== null; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,15 @@ import {I18nVersion} from '@angular/core'; | |
| import * as tsc from '@angular/tsc-wrapped'; | ||
| import {readFileSync} from 'fs'; | ||
| import * as inquirer from 'inquirer'; | ||
| import {join} from 'path'; | ||
| import * as ts from 'typescript'; | ||
|
|
||
| import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from '../compiler_host'; | ||
| import {Extractor} from '../extractor'; | ||
| import {PathMappedCompilerHost} from '../path_mapped_compiler_host'; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Resolves the conflicts during migration: | ||
| * - manually by asking the user which translation to keep when `autoResolve` is `false`, | ||
|
|
@@ -49,15 +51,21 @@ function resolveConflicts( | |
| */ | ||
| export class V0ToV1Migration { | ||
| constructor( | ||
| private host: ts.CompilerHost, private extractor: Extractor, private files: string[], | ||
| private format: string, private autoResolve: boolean) {} | ||
| private options: tsc.AngularCompilerOptions, private host: ts.CompilerHost, | ||
| private extractor: Extractor, private files: string[], private format: string, | ||
| private autoResolve: boolean) {} | ||
|
|
||
| execute(): Promise<string[]> { | ||
| execute(isMapping: boolean): Promise<string[]> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have |
||
| return this.extractor.extractBundle().then((bundle: compiler.MessageBundle) => { | ||
| const v1toV0 = compiler.generateV1ToV0Map(bundle, this.format); | ||
| const v1toV0 = | ||
| compiler.generateV1ToV0Map(bundle, this.format, join(this.options.genDir, '/')); | ||
|
|
||
| return this.files.reduce( | ||
| (p, file) => p.then(this.genMigrateFileCallback(v1toV0, file)), Promise.resolve([])); | ||
| if (isMapping) { | ||
| return Promise.resolve(this.genMappingFile(v1toV0)); | ||
| } else { | ||
| return this.files.reduce( | ||
| (p, file) => p.then(this.genMigrateFileCallback(v1toV0, file)), Promise.resolve([])); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -79,6 +87,12 @@ export class V0ToV1Migration { | |
| }; | ||
| } | ||
|
|
||
| private genMappingFile(v1toV0: compiler.V1ToV0Map): string[] { | ||
| let dstPath = join(this.options.genDir, `mapping_v1_to_v0.json`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const |
||
| this.host.writeFile(dstPath, JSON.stringify(v1toV0), false); | ||
| return [dstPath]; | ||
| } | ||
|
|
||
| static create( | ||
| options: tsc.AngularCompilerOptions, program: ts.Program, tsCompilerHost: ts.CompilerHost, | ||
| files: string[], format: string, autoResolve: boolean, | ||
|
|
@@ -94,6 +108,6 @@ export class V0ToV1Migration { | |
| I18nVersion.V1, options, program, tsCompilerHost, null, compilerHostContext, | ||
| ngCompilerHost); | ||
|
|
||
| return new V0ToV1Migration(tsCompilerHost, extractor, files, format, autoResolve); | ||
| return new V0ToV1Migration(options, tsCompilerHost, extractor, files, format, autoResolve); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,25 +42,34 @@ export type V0ToV1Map = { | |
| * - `ids`: the list of v0 ids that the v1 id replaces. | ||
| * - `sources`: the list of `MessageSpan` for the v1 id. | ||
| */ | ||
| export function generateV1ToV0Map(messageBundle: MessageBundle, formatName: string): V1ToV0Map { | ||
| export function generateV1ToV0Map( | ||
| messageBundle: MessageBundle, formatName: string, basePath: string = ''): V1ToV0Map { | ||
| const serializerV0 = createSerializer(formatName, I18nVersion.V0); | ||
| const serializerV1 = createSerializer(formatName, I18nVersion.V1); | ||
|
|
||
| const v1ToV0: V1ToV0Map = {}; | ||
|
|
||
| function cleanPaths(sources: MessageSpan[]) { | ||
| return [...sources].map(source => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| source.filePath = source.filePath.replace(basePath, ''); | ||
| return source; | ||
| }); | ||
| } | ||
|
|
||
| messageBundle.getMessages().forEach((msg: Message) => { | ||
| const v0 = serializerV0.digest(msg); | ||
| const v1 = serializerV1.digest(msg); | ||
| const sources = cleanPaths(msg.sources); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the rationale here ? |
||
|
|
||
| if (v1ToV0.hasOwnProperty(v1)) { | ||
| if (v1ToV0[v1].ids.indexOf(v0) === -1) { | ||
| v1ToV0[v1].ids.push(v0); | ||
| v1ToV0[v1].sources !.push(...msg.sources); | ||
| v1ToV0[v1].sources !.push(...sources); | ||
| } | ||
| } else { | ||
| v1ToV0[v1] = { | ||
| ids: [v0], | ||
| sources: msg.sources, | ||
| sources: sources, | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you create an issue for this bug as discussed in the shared doc ? (startCol = endCol)