Skip to content

Commit

Permalink
fix(transformers): adopt ts 4.9 changes for replace-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
anh.pham committed Aug 28, 2022
1 parent 245067c commit b013b6b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
],
'@typescript-eslint/comma-spacing': 'error',
'@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/prefer-ts-expect-error': 'error',
'import/order': [
'error',
Expand Down
4 changes: 2 additions & 2 deletions examples/example-app-v14/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@angular/platform-browser": "^14.2.0",
"@angular/platform-browser-dynamic": "^14.2.0",
"@angular/router": "^14.2.0",
"angular-in-memory-web-api": "^0.13.0",
"angular-in-memory-web-api": "^0.14.0",
"rxjs": "~7.5.6",
"tslib": "^2.4.0",
"zone.js": "^0.11.8"
Expand All @@ -33,6 +33,6 @@
"@types/node": "^16.11.56",
"jest": "^28.1.3",
"jest-preset-angular": "^12.2.0",
"typescript": "~4.6.4"
"typescript": "^4.8.2"
}
}
32 changes: 16 additions & 16 deletions examples/example-app-v14/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2981,16 +2981,16 @@ __metadata:
languageName: node
linkType: hard

"angular-in-memory-web-api@npm:^0.13.0":
version: 0.13.0
resolution: "angular-in-memory-web-api@npm:0.13.0"
"angular-in-memory-web-api@npm:^0.14.0":
version: 0.14.0
resolution: "angular-in-memory-web-api@npm:0.14.0"
dependencies:
tslib: ^2.3.0
peerDependencies:
"@angular/common": ^13.0.0
"@angular/core": ^13.0.0
"@angular/common": ^14.0.0
"@angular/core": ^14.0.0
rxjs: ^6.5.3 || ^7.4.0
checksum: 4ff21b69340112199fe6a4b2e7c1f5ccf357f48cf28c743f22971c87f3e5c916155f16fc1429a3e446ab63b3f2d7e3e0675948a291f3b600f6d4699a3dfb54b7
checksum: 9ebf14c385e32dac9a7d5e3bbaca4cda775738dc6e38098bbfbe1e445c3057e8f0a91f3df307b6d7424faa567959ac4e2e40db158f9cad6225e3ba32bc6a0d23
languageName: node
linkType: hard

Expand Down Expand Up @@ -4742,12 +4742,12 @@ __metadata:
"@angular/router": ^14.2.0
"@types/jest": ^27.5.2
"@types/node": ^16.11.56
angular-in-memory-web-api: ^0.13.0
angular-in-memory-web-api: ^0.14.0
jest: ^28.1.3
jest-preset-angular: ^12.2.0
rxjs: ~7.5.6
tslib: ^2.4.0
typescript: ~4.6.4
typescript: ^4.8.2
zone.js: ^0.11.8
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -9446,23 +9446,23 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:~4.6.4":
version: 4.6.4
resolution: "typescript@npm:4.6.4"
"typescript@npm:^4.8.2":
version: 4.8.2
resolution: "typescript@npm:4.8.2"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: e7bfcc39cd4571a63a54e5ea21f16b8445268b9900bf55aee0e02ad981be576acc140eba24f1af5e3c1457767c96cea6d12861768fb386cf3ffb34013718631a
checksum: 7f5b81d0d558c9067f952c7af52ab7f19c2e70a916817929e4a5b256c93990bf3178eccb1ac8a850bc75df35f6781b6f4cb3370ce20d8b1ded92ed462348f628
languageName: node
linkType: hard

"typescript@patch:typescript@~4.6.4#~builtin<compat/typescript>":
version: 4.6.4
resolution: "typescript@patch:typescript@npm%3A4.6.4#~builtin<compat/typescript>::version=4.6.4&hash=f456af"
"typescript@patch:typescript@^4.8.2#~builtin<compat/typescript>":
version: 4.8.2
resolution: "typescript@patch:typescript@npm%3A4.8.2#~builtin<compat/typescript>::version=4.8.2&hash=f456af"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 1cb434fbc637d347be90e3a0c6cd05e33c38f941713c8786d3031faf1842c2c148ba91d2fac01e7276b0ae3249b8633f1660e32686cc7a8c6a8fd5361dc52c66
checksum: 6f49363af8af2fe480da1d5fa68712644438785208b06690a3cbe5e7365fd652c3a0f1e587bc8684d78fb69de3dde4de185c0bad7bb4f3664ddfc813ce8caad6
languageName: node
linkType: hard

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"rimraf": "^3.0.2",
"rxjs": "^7.5.6",
"tslib": "^2.4.0",
"typescript": "^4.7.4",
"typescript": "^4.8.2",
"zone.js": "^0.11.8"
},
"packageManager": "yarn@3.2.2"
Expand Down
83 changes: 68 additions & 15 deletions src/transformers/replace-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import ts from 'typescript';

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

const isAfterVersion = (targetMajor: number, targetMinor: number): boolean => {
const [major, minor] = ts.versionMajorMinor.split('.').map((part) => parseInt(part));

if (major < targetMajor) {
return false;
} else if (major > targetMajor) {
return true;
} else {
return minor >= targetMinor;
}
};

const IS_TS_48 = isAfterVersion(4, 8);

const shouldTransform = (fileName: string) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
/**
* Source https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/transformers/replace_resources.ts
Expand Down Expand Up @@ -52,21 +66,7 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme

const visitNode: ts.Visitor = (node: ts.Node) => {
if (ts.isClassDeclaration(node)) {
const decorators = ts.visitNodes(node.decorators, (node) =>
ts.isDecorator(node)
? visitDecorator(nodeFactory, node, typeChecker, resourceImportDeclarations, moduleKind)
: node,
);

return nodeFactory.updateClassDeclaration(
node,
decorators,
node.modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
);
return visitClassDeclaration(nodeFactory, typeChecker, node, resourceImportDeclarations, moduleKind);
}

return ts.visitEachChild(node, visitNode, context);
Expand Down Expand Up @@ -94,6 +94,59 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme
};
}

function visitClassDeclaration(
nodeFactory: ts.NodeFactory,
typeChecker: ts.TypeChecker,
node: ts.ClassDeclaration,
resourceImportDeclarations: ts.ImportDeclaration[],
moduleKind: ts.ModuleKind | undefined,
): ts.ClassDeclaration {
let decorators: ts.Decorator[] | undefined;
let modifiers: ts.Modifier[] | undefined;

if (IS_TS_48) {
node.modifiers?.forEach((modifier) => {
if (ts.isDecorator(modifier)) {
decorators ??= [];
decorators.push(modifier);
} else {
modifiers = modifiers ??= [];
modifiers.push(modifier);
}
});
} else {
decorators = node.decorators as unknown as ts.Decorator[];
modifiers = node.modifiers as unknown as ts.Modifier[];
}

if (!decorators || !decorators.length) {
return node;
}

decorators = decorators.map((current) =>
visitDecorator(nodeFactory, current, typeChecker, resourceImportDeclarations, moduleKind),
);

return IS_TS_48
? nodeFactory.updateClassDeclaration(
node,
[...decorators, ...(modifiers ?? [])],
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
)
: nodeFactory.updateClassDeclaration(
node,
decorators,
modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
);
}

function visitDecorator(
nodeFactory: ts.NodeFactory,
node: ts.Decorator,
Expand Down
26 changes: 23 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7402,7 +7402,7 @@ __metadata:
rxjs: ^7.5.6
ts-jest: ^28.0.0
tslib: ^2.4.0
typescript: ^4.7.4
typescript: ^4.8.2
zone.js: ^0.11.8
peerDependencies:
"@angular-devkit/build-angular": ">=0.1102.19 <15.0.0"
Expand Down Expand Up @@ -11191,7 +11191,7 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:^4.6.4, typescript@npm:^4.7.4":
"typescript@npm:^4.6.4":
version: 4.7.4
resolution: "typescript@npm:4.7.4"
bin:
Expand All @@ -11201,7 +11201,17 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:typescript@^4.6.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.7.4#~builtin<compat/typescript>":
"typescript@npm:^4.8.2":
version: 4.8.2
resolution: "typescript@npm:4.8.2"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 7f5b81d0d558c9067f952c7af52ab7f19c2e70a916817929e4a5b256c93990bf3178eccb1ac8a850bc75df35f6781b6f4cb3370ce20d8b1ded92ed462348f628
languageName: node
linkType: hard

"typescript@patch:typescript@^4.6.4#~builtin<compat/typescript>":
version: 4.7.4
resolution: "typescript@patch:typescript@npm%3A4.7.4#~builtin<compat/typescript>::version=4.7.4&hash=f456af"
bin:
Expand All @@ -11211,6 +11221,16 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:typescript@^4.8.2#~builtin<compat/typescript>":
version: 4.8.2
resolution: "typescript@patch:typescript@npm%3A4.8.2#~builtin<compat/typescript>::version=4.8.2&hash=f456af"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 6f49363af8af2fe480da1d5fa68712644438785208b06690a3cbe5e7365fd652c3a0f1e587bc8684d78fb69de3dde4de185c0bad7bb4f3664ddfc813ce8caad6
languageName: node
linkType: hard

"uglify-js@npm:^3.1.4":
version: 3.15.4
resolution: "uglify-js@npm:3.15.4"
Expand Down

0 comments on commit b013b6b

Please sign in to comment.