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

fix(ng-update): do not throw if typescript version is outdated #13927

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/cdk/schematics/ng-update/tslint/component-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {existsSync, readFileSync} from 'fs';
import {dirname, resolve} from 'path';
import * as ts from 'typescript';
import {isStringLiteralLike} from '../typescript/literal';
import {createComponentFile, ExternalResource} from './component-file';
import {ExternalFailureWalker} from './external-failure-walker';

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

if (propertyName === 'template' && ts.isStringLiteralLike(property.initializer)) {
if (propertyName === 'template' && isStringLiteralLike(property.initializer)) {
this.visitInlineTemplate(property.initializer);
}

if (propertyName === 'templateUrl' && ts.isStringLiteralLike(property.initializer)) {
if (propertyName === 'templateUrl' && isStringLiteralLike(property.initializer)) {
this._reportExternalTemplate(property.initializer);
}

Expand Down Expand Up @@ -95,15 +96,15 @@ export class ComponentWalker extends ExternalFailureWalker {

private _reportInlineStyles(expression: ts.ArrayLiteralExpression) {
expression.elements.forEach(node => {
if (ts.isStringLiteralLike(node)) {
if (isStringLiteralLike(node)) {
this.visitInlineStylesheet(node);
}
});
}

private _visitExternalStylesArrayLiteral(expression: ts.ArrayLiteralExpression) {
expression.elements.forEach(node => {
if (ts.isStringLiteralLike(node)) {
if (isStringLiteralLike(node)) {
const stylePath = resolve(dirname(this.getSourceFile().fileName), node.text);

// Check if the external stylesheet file exists before proceeding.
Expand Down
14 changes: 14 additions & 0 deletions src/cdk/schematics/ng-update/typescript/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import * as ts from 'typescript';

/** Finds all start indices of the given search string in the input string. */
export function findAllSubstringIndices(input: string, search: string): number[] {
const result: number[] = [];
Expand All @@ -15,3 +17,15 @@ export function findAllSubstringIndices(input: string, search: string): number[]
}
return result;
}

/**
* Checks whether the given node is either a string literal or a no-substitution template
* literal. Note that we cannot use `ts.isStringLiteralLike()` because if developers update
* an outdated project, their TypeScript version is not automatically being updated
* and therefore could throw because the function is not available yet.
* https://github.com/Microsoft/TypeScript/commit/8518343dc8762475a5e92c9f80b5c5725bd81796
*/
export function isStringLiteralLike(node: ts.Node):
node is (ts.StringLiteral | ts.NoSubstitutionTemplateLiteral) {
return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node);
}