From b013b6bace31ae02cf332b140f4d14ec259c1e29 Mon Sep 17 00:00:00 2001 From: "anh.pham" Date: Sun, 28 Aug 2022 22:40:40 +0200 Subject: [PATCH] fix(transformers): adopt ts 4.9 changes for `replace-resource` --- .eslintrc.js | 2 +- examples/example-app-v14/package.json | 4 +- examples/example-app-v14/yarn.lock | 32 +++++------ package.json | 2 +- src/transformers/replace-resources.ts | 83 ++++++++++++++++++++++----- yarn.lock | 26 ++++++++- 6 files changed, 111 insertions(+), 38 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index dae95f9464..95646dded3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', diff --git a/examples/example-app-v14/package.json b/examples/example-app-v14/package.json index e7d25ffcbc..99298fb365 100644 --- a/examples/example-app-v14/package.json +++ b/examples/example-app-v14/package.json @@ -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" @@ -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" } } diff --git a/examples/example-app-v14/yarn.lock b/examples/example-app-v14/yarn.lock index adfada3f3b..d230d252fc 100644 --- a/examples/example-app-v14/yarn.lock +++ b/examples/example-app-v14/yarn.lock @@ -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 @@ -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 @@ -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": - version: 4.6.4 - resolution: "typescript@patch:typescript@npm%3A4.6.4#~builtin::version=4.6.4&hash=f456af" +"typescript@patch:typescript@^4.8.2#~builtin": + version: 4.8.2 + resolution: "typescript@patch:typescript@npm%3A4.8.2#~builtin::version=4.8.2&hash=f456af" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 1cb434fbc637d347be90e3a0c6cd05e33c38f941713c8786d3031faf1842c2c148ba91d2fac01e7276b0ae3249b8633f1660e32686cc7a8c6a8fd5361dc52c66 + checksum: 6f49363af8af2fe480da1d5fa68712644438785208b06690a3cbe5e7365fd652c3a0f1e587bc8684d78fb69de3dde4de185c0bad7bb4f3664ddfc813ce8caad6 languageName: node linkType: hard diff --git a/package.json b/package.json index 1934c2b668..ce778e9994 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/transformers/replace-resources.ts b/src/transformers/replace-resources.ts index 693de2613e..32330e543d 100644 --- a/src/transformers/replace-resources.ts +++ b/src/transformers/replace-resources.ts @@ -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 @@ -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); @@ -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, diff --git a/yarn.lock b/yarn.lock index 41fe1aad4c..d39dcd0a0c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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: @@ -11201,7 +11201,17 @@ __metadata: languageName: node linkType: hard -"typescript@patch:typescript@^4.6.4#~builtin, typescript@patch:typescript@^4.7.4#~builtin": +"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": version: 4.7.4 resolution: "typescript@patch:typescript@npm%3A4.7.4#~builtin::version=4.7.4&hash=f456af" bin: @@ -11211,6 +11221,16 @@ __metadata: languageName: node linkType: hard +"typescript@patch:typescript@^4.8.2#~builtin": + version: 4.8.2 + resolution: "typescript@patch:typescript@npm%3A4.8.2#~builtin::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"