Skip to content

Commit

Permalink
fix(ts5): Update build dependencies and types for TS 5
Browse files Browse the repository at this point in the history
This update appears to fix thymikee#2138. It builds with TS 5, and all tests and example tests pass.
  • Loading branch information
johncrim committed Jan 3, 2024
1 parent 8589634 commit be09743
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"rxjs": "^7.8.1",
"ts-node": "^10.9.1",
"tslib": "^2.5.2",
"typescript": "^4.9.5",
"typescript": "^5.3.3",
"zone.js": "^0.13.0"
},
"packageManager": "yarn@3.5.1"
Expand Down
13 changes: 11 additions & 2 deletions src/ngtsc/ts_compatibility/src/ts_cross_version_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ export const updateConstructorDeclaration: Ts48UpdateConstructorDeclarationFn =
* We should remove it once we have dropped support for the older versions.
*/
export const getDecorators: (node: ts.Node) => readonly ts.Decorator[] | undefined =
IS_AFTER_TS_48 ? (ts as any).getDecorators : node => node.decorators;
IS_AFTER_TS_48 ? (ts.getDecorators as (node: ts.Node) => readonly ts.Decorator[] | undefined)
: node => (node as any).decorators;

/**
* Gets the modifiers that have been set on a node.
Expand All @@ -273,7 +274,8 @@ export const getDecorators: (node: ts.Node) => readonly ts.Decorator[] | undefin
* We should remove it once we have dropped support for the older versions.
*/
export const getModifiers: (node: ts.Node) => readonly ts.Modifier[] | undefined =
IS_AFTER_TS_48 ? (ts as any).getModifiers : node => node.modifiers;
IS_AFTER_TS_48 ? (ts.getModifiers as (node: ts.Node) => readonly ts.Modifier[] | undefined)
: node => (node as any).modifiers;

/**
* Combines an optional array of decorators with an optional array of modifiers into a single
Expand Down Expand Up @@ -341,3 +343,10 @@ function isAfterVersion(targetMajor: number, targetMinor: number): boolean {

return major === targetMajor ? minor >= targetMinor : true;
}

export function toMutableArray<T>(immutableArray: readonly T[] | undefined): T[] | undefined {
if (immutableArray) {
return [...immutableArray];
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export function getDownlevelDecoratorsTransform(
const modifiers = decoratorsToKeep.length ?
ts.setTextRange(
ts.factory.createNodeArray(combineModifiers(decoratorsToKeep, getModifiers(element))),
element.modifiers) :
(element as any).modifiers) :
getModifiers(element);

return [element.name.text, cloneClassElementWithModifiers(element, modifiers), toLower];
Expand Down
35 changes: 24 additions & 11 deletions src/transformers/replace-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { TsCompilerInstance } from 'ts-jest';
import ts from 'typescript';

import { STYLES, STYLE_URLS, TEMPLATE_URL, TEMPLATE, REQUIRE, COMPONENT, STYLE_URL } from '../constants';
import { getDecorators, getModifiers, toMutableArray } from '../ngtsc/ts_compatibility';

const isAfterVersion = (targetMajor: number, targetMinor: number): boolean => {
const [major, minor] = ts.versionMajorMinor.split('.').map((part) => parseInt(part));
Expand All @@ -24,6 +25,9 @@ const isAfterVersion = (targetMajor: number, targetMinor: number): boolean => {

const IS_TS_48 = isAfterVersion(4, 8);

/** Whether the current TypeScript version is after 5.0. */
const IS_AFTER_TS_50 = isAfterVersion(5, 0);

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 @@ -77,7 +81,7 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme
return sourceFile;
}

const updatedSourceFile = ts.visitNode(sourceFile, visitNode);
const updatedSourceFile = ts.visitNode(sourceFile, visitNode) as ts.SourceFile;
if (resourceImportDeclarations.length) {
// Add resource imports
return nodeFactory.updateSourceFile(
Expand Down Expand Up @@ -115,8 +119,8 @@ function visitClassDeclaration(
}
});
} else {
decorators = node.decorators as unknown as ts.Decorator[];
modifiers = node.modifiers as unknown as ts.Modifier[];
decorators = toMutableArray(getDecorators(node));
modifiers = toMutableArray(getModifiers(node));
}

if (!decorators || !decorators.length) {
Expand All @@ -136,7 +140,7 @@ function visitClassDeclaration(
node.heritageClauses,
node.members,
)
: nodeFactory.updateClassDeclaration(
: (nodeFactory as any).updateClassDeclaration(
node,
decorators,
modifiers,
Expand Down Expand Up @@ -192,7 +196,7 @@ function visitDecorator(
return nodeFactory.updateDecorator(
node,
nodeFactory.updateCallExpression(decoratorFactory, decoratorFactory.expression, decoratorFactory.typeArguments, [
nodeFactory.updateObjectLiteralExpression(objectExpression, properties),
nodeFactory.updateObjectLiteralExpression(objectExpression, properties as any),
]),
);
}
Expand Down Expand Up @@ -280,12 +284,21 @@ function createResourceImport(
return nodeFactory.createCallExpression(nodeFactory.createIdentifier(REQUIRE), [], [urlLiteral]);
} else {
const importName = nodeFactory.createIdentifier(`__NG_CLI_RESOURCE__${resourceImportDeclarations.length}`);
const importDeclaration = nodeFactory.createImportDeclaration(
undefined,
undefined,
nodeFactory.createImportClause(false, importName, undefined),
urlLiteral,
);
let importDeclaration: ts.ImportDeclaration;
if (IS_AFTER_TS_50) {
importDeclaration = nodeFactory.createImportDeclaration(
undefined,
nodeFactory.createImportClause(false, importName, undefined),
urlLiteral,
);
} else {
importDeclaration = (nodeFactory as any).createImportDeclaration(
undefined,
undefined,
nodeFactory.createImportClause(false, importName, undefined),
urlLiteral,
);
}
resourceImportDeclarations.push(importDeclaration);

return importName;
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7715,7 +7715,7 @@ __metadata:
ts-jest: ^29.0.0
ts-node: ^10.9.1
tslib: ^2.5.2
typescript: ^4.9.5
typescript: ^5.3.3
zone.js: ^0.13.0
peerDependencies:
"@angular-devkit/build-angular": ">=15.0.0 <18.0.0"
Expand Down Expand Up @@ -11344,13 +11344,13 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:^4.9.5":
version: 4.9.5
resolution: "typescript@npm:4.9.5"
"typescript@npm:^5.3.3":
version: 5.3.3
resolution: "typescript@npm:5.3.3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: ee000bc26848147ad423b581bd250075662a354d84f0e06eb76d3b892328d8d4440b7487b5a83e851b12b255f55d71835b008a66cbf8f255a11e4400159237db
checksum: 2007ccb6e51bbbf6fde0a78099efe04dc1c3dfbdff04ca3b6a8bc717991862b39fd6126c0c3ebf2d2d98ac5e960bcaa873826bb2bb241f14277034148f41f6a2
languageName: node
linkType: hard

Expand All @@ -11364,13 +11364,13 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:typescript@^4.9.5#~builtin<compat/typescript>":
version: 4.9.5
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=289587"
"typescript@patch:typescript@^5.3.3#~builtin<compat/typescript>":
version: 5.3.3
resolution: "typescript@patch:typescript@npm%3A5.3.3#~builtin<compat/typescript>::version=5.3.3&hash=77c9e2"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 1f8f3b6aaea19f0f67cba79057674ba580438a7db55057eb89cc06950483c5d632115c14077f6663ea76fd09fce3c190e6414bb98582ec80aa5a4eaf345d5b68
checksum: f61375590b3162599f0f0d5b8737877ac0a7bc52761dbb585d67e7b8753a3a4c42d9a554c4cc929f591ffcf3a2b0602f65ae3ce74714fd5652623a816862b610
languageName: node
linkType: hard

Expand Down

0 comments on commit be09743

Please sign in to comment.