Skip to content

Commit

Permalink
fix(transformers): adopt ts 4.8 changes for replace-resource (#1739)
Browse files Browse the repository at this point in the history
Fixes #1732
  • Loading branch information
ahnpnl authored Aug 28, 2022
1 parent 4494966 commit aea7205
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 44 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=a1c5e5"
"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=a1c5e5"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 1cb434fbc637d347be90e3a0c6cd05e33c38f941713c8786d3031faf1842c2c148ba91d2fac01e7276b0ae3249b8633f1660e32686cc7a8c6a8fd5361dc52c66
checksum: 5cb0f02f414f5405f4b0e7ee1fd7fa9177b6a8783c9017b6cad85f56ce4c4f93e0e6f2ce37e863cb597d44227cd009474c9fbd85bf7a50004e5557426cb58079
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.3"
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
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7409,7 +7409,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 @@ -11198,23 +11198,23 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:^4.6.4, typescript@npm:^4.7.4":
version: 4.7.4
resolution: "typescript@npm:4.7.4"
"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: 5750181b1cd7e6482c4195825547e70f944114fb47e58e4aa7553e62f11b3f3173766aef9c281783edfd881f7b8299cf35e3ca8caebe73d8464528c907a164df
checksum: 7f5b81d0d558c9067f952c7af52ab7f19c2e70a916817929e4a5b256c93990bf3178eccb1ac8a850bc75df35f6781b6f4cb3370ce20d8b1ded92ed462348f628
languageName: node
linkType: hard

"typescript@patch:typescript@^4.6.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.7.4#~builtin<compat/typescript>":
version: 4.7.4
resolution: "typescript@patch:typescript@npm%3A4.7.4#~builtin<compat/typescript>::version=4.7.4&hash=a1c5e5"
"typescript@patch:typescript@^4.6.4#~builtin<compat/typescript>, 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=a1c5e5"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 9096d8f6c16cb80ef3bf96fcbbd055bf1c4a43bd14f3b7be45a9fbe7ada46ec977f604d5feed3263b4f2aa7d4c7477ce5f9cd87de0d6feedec69a983f3a4f93e
checksum: 5cb0f02f414f5405f4b0e7ee1fd7fa9177b6a8783c9017b6cad85f56ce4c4f93e0e6f2ce37e863cb597d44227cd009474c9fbd85bf7a50004e5557426cb58079
languageName: node
linkType: hard

Expand Down

0 comments on commit aea7205

Please sign in to comment.