Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@ngtools/webpack): remove TypeScript 4.7 compatibility code #24450

Merged
merged 1 commit into from Jan 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 24 additions & 91 deletions packages/ngtools/webpack/src/transformers/replace_resources.ts
Expand Up @@ -11,9 +11,6 @@ import { InlineAngularResourceLoaderPath } from '../loaders/inline-resource';

export const NG_COMPONENT_RESOURCE_QUERY = 'ngResource';

/** Whether the current version of TypeScript is after 4.8. */
const IS_TS_48 = isAfterVersion(4, 8);

export function replaceResources(
shouldTransform: (fileName: string) => boolean,
getTypeChecker: () => ts.TypeChecker,
Expand All @@ -27,13 +24,31 @@ export function replaceResources(

const visitNode: ts.Visitor = (node: ts.Node) => {
if (ts.isClassDeclaration(node)) {
return visitClassDeclaration(
nodeFactory,
typeChecker,
const decorators = ts.getDecorators(node);

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

return nodeFactory.updateClassDeclaration(
node,
resourceImportDeclarations,
moduleKind,
inlineStyleFileExtension,
[
...decorators.map((current) =>
visitDecorator(
nodeFactory,
current,
typeChecker,
resourceImportDeclarations,
moduleKind,
inlineStyleFileExtension,
),
),
...(ts.getModifiers(node) ?? []),
],
node.name,
node.typeParameters,
node.heritageClauses,
node.members,
);
}

Expand Down Expand Up @@ -65,75 +80,6 @@ export function replaceResources(
};
}

/**
* Replaces the resources inside of a `ClassDeclaration`. This is a backwards-compatibility layer
* to support TypeScript versions older than 4.8 where the decorators of a node were in a separate
* array, rather than being part of its `modifiers` array.
*
* TODO: remove this function and use the `NodeFactory` directly once support for TypeScript
* 4.6 and 4.7 has been dropped.
*/
function visitClassDeclaration(
nodeFactory: ts.NodeFactory,
typeChecker: ts.TypeChecker,
node: ts.ClassDeclaration,
resourceImportDeclarations: ts.ImportDeclaration[],
moduleKind: ts.ModuleKind | undefined,
inlineStyleFileExtension: string | 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 === 0) {
return node;
}

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

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 Expand Up @@ -384,16 +330,3 @@ function getDecoratorOrigin(

return null;
}

/** Checks if the current version of TypeScript is after the specified major/minor versions. */
function 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;
}
}