Skip to content

Commit d558ce1

Browse files
fix: add support for TypeScript 4.8 (#3548)
Closes #3547
1 parent ecb6377 commit d558ce1

File tree

36 files changed

+180
-66
lines changed

36 files changed

+180
-66
lines changed

modules/component-store/schematics-core/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ export {
9292
visitNgModules,
9393
visitTemplates,
9494
} from './utility/visitors';
95+
96+
export { getNodeDecorators } from './utility/decorators';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as ts from 'typescript';
2+
3+
/**
4+
* In TS 4.8 the `decorators` are combined with the `modifiers` array.
5+
* Once we drop support for older versions, we can remove this function
6+
* and use `ts.getDecorators`.
7+
*/
8+
export function getNodeDecorators(node: ts.Node): ts.Decorator[] | undefined {
9+
return (ts as any).getDecorators?.(node) || node.decorators;
10+
}

modules/component-store/schematics-core/utility/visitors.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import { normalize, resolve } from '@angular-devkit/core';
33
import { Tree, DirEntry } from '@angular-devkit/schematics';
4+
import { getNodeDecorators } from './decorators';
45

56
export function visitTSSourceFiles<Result = void>(
67
tree: Tree,
@@ -166,15 +167,13 @@ export function visitDecorator(
166167
}
167168

168169
const classDeclarationNode = node as ts.ClassDeclaration;
170+
const decorators = getNodeDecorators(classDeclarationNode);
169171

170-
if (
171-
!classDeclarationNode.decorators ||
172-
!classDeclarationNode.decorators.length
173-
) {
172+
if (!decorators || !decorators.length) {
174173
return;
175174
}
176175

177-
const componentDecorator = classDeclarationNode.decorators.find((d) => {
176+
const componentDecorator = decorators.find((d) => {
178177
return (
179178
ts.isCallExpression(d.expression) &&
180179
ts.isIdentifier(d.expression.expression) &&

modules/component/schematics-core/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ export {
9292
visitNgModules,
9393
visitTemplates,
9494
} from './utility/visitors';
95+
96+
export { getNodeDecorators } from './utility/decorators';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as ts from 'typescript';
2+
3+
/**
4+
* In TS 4.8 the `decorators` are combined with the `modifiers` array.
5+
* Once we drop support for older versions, we can remove this function
6+
* and use `ts.getDecorators`.
7+
*/
8+
export function getNodeDecorators(node: ts.Node): ts.Decorator[] | undefined {
9+
return (ts as any).getDecorators?.(node) || node.decorators;
10+
}

modules/component/schematics-core/utility/visitors.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import { normalize, resolve } from '@angular-devkit/core';
33
import { Tree, DirEntry } from '@angular-devkit/schematics';
4+
import { getNodeDecorators } from './decorators';
45

56
export function visitTSSourceFiles<Result = void>(
67
tree: Tree,
@@ -166,15 +167,13 @@ export function visitDecorator(
166167
}
167168

168169
const classDeclarationNode = node as ts.ClassDeclaration;
170+
const decorators = getNodeDecorators(classDeclarationNode);
169171

170-
if (
171-
!classDeclarationNode.decorators ||
172-
!classDeclarationNode.decorators.length
173-
) {
172+
if (!decorators || !decorators.length) {
174173
return;
175174
}
176175

177-
const componentDecorator = classDeclarationNode.decorators.find((d) => {
176+
const componentDecorator = decorators.find((d) => {
178177
return (
179178
ts.isCallExpression(d.expression) &&
180179
ts.isIdentifier(d.expression.expression) &&

modules/data/schematics-core/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ export {
9292
visitNgModules,
9393
visitTemplates,
9494
} from './utility/visitors';
95+
96+
export { getNodeDecorators } from './utility/decorators';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as ts from 'typescript';
2+
3+
/**
4+
* In TS 4.8 the `decorators` are combined with the `modifiers` array.
5+
* Once we drop support for older versions, we can remove this function
6+
* and use `ts.getDecorators`.
7+
*/
8+
export function getNodeDecorators(node: ts.Node): ts.Decorator[] | undefined {
9+
return (ts as any).getDecorators?.(node) || node.decorators;
10+
}

modules/data/schematics-core/utility/visitors.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import { normalize, resolve } from '@angular-devkit/core';
33
import { Tree, DirEntry } from '@angular-devkit/schematics';
4+
import { getNodeDecorators } from './decorators';
45

56
export function visitTSSourceFiles<Result = void>(
67
tree: Tree,
@@ -166,15 +167,13 @@ export function visitDecorator(
166167
}
167168

168169
const classDeclarationNode = node as ts.ClassDeclaration;
170+
const decorators = getNodeDecorators(classDeclarationNode);
169171

170-
if (
171-
!classDeclarationNode.decorators ||
172-
!classDeclarationNode.decorators.length
173-
) {
172+
if (!decorators || !decorators.length) {
174173
return;
175174
}
176175

177-
const componentDecorator = classDeclarationNode.decorators.find((d) => {
176+
const componentDecorator = decorators.find((d) => {
178177
return (
179178
ts.isCallExpression(d.expression) &&
180179
ts.isIdentifier(d.expression.expression) &&

modules/effects/migrations/13_0_0/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
replaceImport,
88
commitChanges,
99
visitTSSourceFiles,
10+
getNodeDecorators,
1011
} from '../../schematics-core';
1112

1213
export function migrateToCreators(): Rule {
@@ -15,13 +16,10 @@ export function migrateToCreators(): Rule {
1516
const effectsPerClass = sourceFile.statements
1617
.filter(ts.isClassDeclaration)
1718
.map((clas) =>
18-
clas.members
19-
.filter(ts.isPropertyDeclaration)
20-
.filter(
21-
(property) =>
22-
property.decorators &&
23-
property.decorators.some(isEffectDecorator)
24-
)
19+
clas.members.filter(ts.isPropertyDeclaration).filter((property) => {
20+
const decorators = getNodeDecorators(property);
21+
return decorators && decorators.some(isEffectDecorator);
22+
})
2523
);
2624

2725
const effects = effectsPerClass.reduce(
@@ -60,7 +58,9 @@ function replaceEffectDecorators(
6058
if (!effect.initializer) {
6159
return [];
6260
}
63-
const decorator = (effect.decorators || []).find(isEffectDecorator);
61+
const decorator = (getNodeDecorators(effect) || []).find(
62+
isEffectDecorator
63+
);
6464
if (!decorator) {
6565
return [];
6666
}
@@ -86,7 +86,7 @@ function replaceEffectDecorators(
8686
.reduce((acc, inserts) => acc.concat(inserts), []);
8787

8888
const removes = effects
89-
.map((effect) => effect.decorators)
89+
.map((effect) => getNodeDecorators(effect))
9090
.map((decorators) => {
9191
if (!decorators) {
9292
return [];

0 commit comments

Comments
 (0)