Skip to content

Commit d44fcf8

Browse files
devversionjelbourn
authored andcommitted
fix(ng-update): do not throw if typescript version is outdated (#13927)
* Running `ng update` doesn't necessarily mean that the `typescript` version is updated automatically. Therefore we need to avoid using the `isStringLiteralLike` method which has been added in `2.8`. Note that this does not mean that we cannot _compile_ with a higher TypeScript version.. we should just ensure that _at runtime_, imports from `typescript` are working for [email protected]
1 parent f10f8b9 commit d44fcf8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/cdk/schematics/ng-update/tslint/component-walker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {existsSync, readFileSync} from 'fs';
1010
import {dirname, resolve} from 'path';
1111
import * as ts from 'typescript';
12+
import {isStringLiteralLike} from '../typescript/literal';
1213
import {createComponentFile, ExternalResource} from './component-file';
1314
import {ExternalFailureWalker} from './external-failure-walker';
1415

@@ -60,11 +61,11 @@ export class ComponentWalker extends ExternalFailureWalker {
6061
for (const property of directiveMetadata.properties as ts.NodeArray<ts.PropertyAssignment>) {
6162
const propertyName = property.name.getText();
6263

63-
if (propertyName === 'template' && ts.isStringLiteralLike(property.initializer)) {
64+
if (propertyName === 'template' && isStringLiteralLike(property.initializer)) {
6465
this.visitInlineTemplate(property.initializer);
6566
}
6667

67-
if (propertyName === 'templateUrl' && ts.isStringLiteralLike(property.initializer)) {
68+
if (propertyName === 'templateUrl' && isStringLiteralLike(property.initializer)) {
6869
this._reportExternalTemplate(property.initializer);
6970
}
7071

@@ -95,15 +96,15 @@ export class ComponentWalker extends ExternalFailureWalker {
9596

9697
private _reportInlineStyles(expression: ts.ArrayLiteralExpression) {
9798
expression.elements.forEach(node => {
98-
if (ts.isStringLiteralLike(node)) {
99+
if (isStringLiteralLike(node)) {
99100
this.visitInlineStylesheet(node);
100101
}
101102
});
102103
}
103104

104105
private _visitExternalStylesArrayLiteral(expression: ts.ArrayLiteralExpression) {
105106
expression.elements.forEach(node => {
106-
if (ts.isStringLiteralLike(node)) {
107+
if (isStringLiteralLike(node)) {
107108
const stylePath = resolve(dirname(this.getSourceFile().fileName), node.text);
108109

109110
// Check if the external stylesheet file exists before proceeding.

src/cdk/schematics/ng-update/typescript/literal.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import * as ts from 'typescript';
10+
911
/** Finds all start indices of the given search string in the input string. */
1012
export function findAllSubstringIndices(input: string, search: string): number[] {
1113
const result: number[] = [];
@@ -15,3 +17,15 @@ export function findAllSubstringIndices(input: string, search: string): number[]
1517
}
1618
return result;
1719
}
20+
21+
/**
22+
* Checks whether the given node is either a string literal or a no-substitution template
23+
* literal. Note that we cannot use `ts.isStringLiteralLike()` because if developers update
24+
* an outdated project, their TypeScript version is not automatically being updated
25+
* and therefore could throw because the function is not available yet.
26+
* https://github.com/Microsoft/TypeScript/commit/8518343dc8762475a5e92c9f80b5c5725bd81796
27+
*/
28+
export function isStringLiteralLike(node: ts.Node):
29+
node is (ts.StringLiteral | ts.NoSubstitutionTemplateLiteral) {
30+
return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node);
31+
}

0 commit comments

Comments
 (0)