Skip to content

Commit 774c378

Browse files
committed
refactor(transformers): remove workaround for ts < 4.0 in replace-resource transformer (#1100)
Since we don't support Angular 9 anymore, we can remove workaround for ts `NodeFactory` in `replace-resource` AST transformer
1 parent ad7e0da commit 774c378

File tree

1 file changed

+37
-76
lines changed

1 file changed

+37
-76
lines changed

src/transformers/replace-resources.ts

Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import ts from 'typescript';
1010

1111
import { STYLES, STYLE_URLS, TEMPLATE_URL, TEMPLATE, REQUIRE, COMPONENT } from '../constants';
1212

13-
const useNodeFactory = +ts.versionMajorMinor >= 4.0;
1413
const shouldTransform = (fileName: string) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
1514
/**
1615
* Source https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/transformers/replace_resources.ts
@@ -57,25 +56,15 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme
5756
: node,
5857
);
5958

60-
return useNodeFactory
61-
? context.factory.updateClassDeclaration(
62-
node,
63-
decorators,
64-
node.modifiers,
65-
node.name,
66-
node.typeParameters,
67-
node.heritageClauses,
68-
node.members,
69-
)
70-
: ts.updateClassDeclaration(
71-
node,
72-
decorators,
73-
node.modifiers,
74-
node.name,
75-
node.typeParameters,
76-
node.heritageClauses,
77-
node.members,
78-
);
59+
return context.factory.updateClassDeclaration(
60+
node,
61+
decorators,
62+
node.modifiers,
63+
node.name,
64+
node.typeParameters,
65+
node.heritageClauses,
66+
node.members,
67+
);
7968
}
8069

8170
return ts.visitEachChild(node, visitNode, context);
@@ -89,21 +78,13 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme
8978
const updatedSourceFile = ts.visitNode(sourceFile, visitNode);
9079
if (resourceImportDeclarations.length) {
9180
// Add resource imports
92-
return useNodeFactory
93-
? context.factory.updateSourceFile(
94-
updatedSourceFile,
95-
ts.setTextRange(
96-
context.factory.createNodeArray([...resourceImportDeclarations, ...updatedSourceFile.statements]),
97-
updatedSourceFile.statements,
98-
),
99-
)
100-
: ts.updateSourceFileNode(
101-
updatedSourceFile,
102-
ts.setTextRange(
103-
ts.createNodeArray([...resourceImportDeclarations, ...updatedSourceFile.statements]),
104-
updatedSourceFile.statements,
105-
),
106-
);
81+
return context.factory.updateSourceFile(
82+
updatedSourceFile,
83+
ts.setTextRange(
84+
context.factory.createNodeArray([...resourceImportDeclarations, ...updatedSourceFile.statements]),
85+
updatedSourceFile.statements,
86+
),
87+
);
10788
}
10889

10990
return updatedSourceFile;
@@ -145,34 +126,20 @@ function visitDecorator(
145126

146127
// replace properties with updated properties
147128
if (styleReplacements.length) {
148-
const styleProperty = useNodeFactory
149-
? nodeFactory.createPropertyAssignment(
150-
nodeFactory.createIdentifier(STYLES),
151-
nodeFactory.createArrayLiteralExpression(styleReplacements),
152-
)
153-
: ts.createPropertyAssignment(ts.createIdentifier(STYLES), ts.createArrayLiteral(styleReplacements));
154-
155-
properties = useNodeFactory
156-
? nodeFactory.createNodeArray([...properties, styleProperty])
157-
: ts.createNodeArray([...properties, styleProperty]);
129+
const styleProperty = nodeFactory.createPropertyAssignment(
130+
nodeFactory.createIdentifier(STYLES),
131+
nodeFactory.createArrayLiteralExpression(styleReplacements),
132+
);
133+
134+
properties = nodeFactory.createNodeArray([...properties, styleProperty]);
158135
}
159136

160-
return useNodeFactory
161-
? nodeFactory.updateDecorator(
162-
node,
163-
nodeFactory.updateCallExpression(
164-
decoratorFactory,
165-
decoratorFactory.expression,
166-
decoratorFactory.typeArguments,
167-
[nodeFactory.updateObjectLiteralExpression(objectExpression, properties)],
168-
),
169-
)
170-
: ts.updateDecorator(
171-
node,
172-
ts.updateCall(decoratorFactory, decoratorFactory.expression, decoratorFactory.typeArguments, [
173-
ts.updateObjectLiteral(objectExpression, properties),
174-
]),
175-
);
137+
return nodeFactory.updateDecorator(
138+
node,
139+
nodeFactory.updateCallExpression(decoratorFactory, decoratorFactory.expression, decoratorFactory.typeArguments, [
140+
nodeFactory.updateObjectLiteralExpression(objectExpression, properties),
141+
]),
142+
);
176143
}
177144

178145
function visitComponentMetadata(
@@ -200,9 +167,7 @@ function visitComponentMetadata(
200167
return node;
201168
}
202169

203-
return useNodeFactory
204-
? nodeFactory.updatePropertyAssignment(node, nodeFactory.createIdentifier(TEMPLATE), importName)
205-
: ts.updatePropertyAssignment(node, ts.createIdentifier(TEMPLATE), importName);
170+
return nodeFactory.updatePropertyAssignment(node, nodeFactory.createIdentifier(TEMPLATE), importName);
206171

207172
case STYLES:
208173
case STYLE_URLS:
@@ -241,21 +206,17 @@ function createResourceImport(
241206
resourceImportDeclarations: ts.ImportDeclaration[],
242207
moduleKind = ts.ModuleKind.ES2015,
243208
): ts.Identifier | ts.Expression | null {
244-
const urlLiteral = useNodeFactory ? nodeFactory.createStringLiteral(url) : ts.createLiteral(url);
209+
const urlLiteral = nodeFactory.createStringLiteral(url);
245210
if (moduleKind < ts.ModuleKind.ES2015) {
246-
return useNodeFactory
247-
? nodeFactory.createCallExpression(nodeFactory.createIdentifier(REQUIRE), [], [urlLiteral])
248-
: ts.createCall(ts.createIdentifier(REQUIRE), undefined, [urlLiteral]);
211+
return nodeFactory.createCallExpression(nodeFactory.createIdentifier(REQUIRE), [], [urlLiteral]);
249212
} else {
250213
const importName = ts.createIdentifier(`__NG_CLI_RESOURCE__${resourceImportDeclarations.length}`);
251-
const importDeclaration = useNodeFactory
252-
? nodeFactory.createImportDeclaration(
253-
undefined,
254-
undefined,
255-
nodeFactory.createImportClause(false, importName, undefined),
256-
urlLiteral,
257-
)
258-
: ts.createImportDeclaration(undefined, undefined, ts.createImportClause(importName, undefined), urlLiteral);
214+
const importDeclaration = nodeFactory.createImportDeclaration(
215+
undefined,
216+
undefined,
217+
nodeFactory.createImportClause(false, importName, undefined),
218+
urlLiteral,
219+
);
259220
resourceImportDeclarations.push(importDeclaration);
260221

261222
return importName;

0 commit comments

Comments
 (0)