-
Notifications
You must be signed in to change notification settings - Fork 12k
/
register_locale_data.ts
104 lines (84 loc) · 3.24 KB
/
register_locale_data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { collectDeepNodes, getFirstNode } from './ast_helpers';
import { insertStarImport } from './insert_import';
import { AddNodeOperation, StandardTransform, TransformOperation } from './interfaces';
import { makeTransform } from './make_transform';
export function registerLocaleData(
shouldTransform: (fileName: string) => boolean,
getEntryModule: () => { path: string, className: string } | null,
locale: string,
): ts.TransformerFactory<ts.SourceFile> {
const standardTransform: StandardTransform = function (sourceFile: ts.SourceFile) {
const ops: TransformOperation[] = [];
const entryModule = getEntryModule();
if (!shouldTransform(sourceFile.fileName) || !entryModule || !locale) {
return ops;
}
// Find all identifiers using the entry module class name.
const entryModuleIdentifiers = collectDeepNodes<ts.Identifier>(sourceFile,
ts.SyntaxKind.Identifier)
.filter(identifier => identifier.text === entryModule.className);
if (entryModuleIdentifiers.length === 0) {
return [];
}
// Find the bootstrap call
entryModuleIdentifiers.forEach(entryModuleIdentifier => {
// Figure out if it's a `platformBrowserDynamic().bootstrapModule(AppModule)` call.
if (!(
entryModuleIdentifier.parent
&& entryModuleIdentifier.parent.kind === ts.SyntaxKind.CallExpression
)) {
return;
}
const callExpr = entryModuleIdentifier.parent as ts.CallExpression;
if (callExpr.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) {
return;
}
const propAccessExpr = callExpr.expression as ts.PropertyAccessExpression;
if (propAccessExpr.name.text !== 'bootstrapModule'
|| propAccessExpr.expression.kind !== ts.SyntaxKind.CallExpression) {
return;
}
const firstNode = getFirstNode(sourceFile);
if (!firstNode) {
return;
}
// Create the import node for the locale.
const localeNamespaceId = ts.createUniqueName('__NgCli_locale_');
ops.push(...insertStarImport(
sourceFile,
localeNamespaceId,
`@angular/common/locales/${locale}`,
firstNode,
true,
));
// Create the import node for the registerLocaleData function.
const regIdentifier = ts.createIdentifier(`registerLocaleData`);
const regNamespaceId = ts.createUniqueName('__NgCli_locale_');
ops.push(
...insertStarImport(sourceFile, regNamespaceId, '@angular/common', firstNode, true),
);
// Create the register function call
const registerFunctionCall = ts.createCall(
ts.createPropertyAccess(regNamespaceId, regIdentifier),
undefined,
[ts.createPropertyAccess(localeNamespaceId, 'default')],
);
const registerFunctionStatement = ts.createStatement(registerFunctionCall);
ops.push(new AddNodeOperation(
sourceFile,
firstNode,
registerFunctionStatement,
));
});
return ops;
};
return makeTransform(standardTransform);
}