Skip to content

Commit d922dcb

Browse files
authoredJun 24, 2018
fix: regressions in 4.4.0 (#671)
* fix: regressions in 4.4.0 - Downgrade to TypeScript 2.7 which is the officially supported on by `@angular/core` - Export `preferInlineDecoratorRule` We should introduce a public API guard to make sure we don't hit regressions like the second one. Fix #669 #670 * refactor: address comments * fix: regression in selector type
1 parent a3f8679 commit d922dcb

11 files changed

+69
-72
lines changed
 

‎package-lock.json

+34-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@
8585
"rxjs": "^6.0.0",
8686
"rxjs-compat": "^6.1.0",
8787
"ts-node": "^6.0.2",
88-
"tslint": "^5.10.0",
89-
"typescript": "^2.8.0",
88+
"tslint": "~5.9.1",
89+
"typescript": "~2.7.0",
9090
"zone.js": "^0.8.26"
9191
},
9292
"peerDependencies": {

‎src/angular/metadataReader.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { callExpression, decoratorArgument, getStringInitializerFromProperty, ha
33
import { ifTrue, listToMaybe, Maybe, unwrapFirst } from '../util/function';
44
import { logger } from '../util/logger';
55
import { getAnimations, getInlineStyle, getTemplate } from '../util/ngQuery';
6-
import { isSimpleTemplateString, maybeNodeArray } from '../util/utils';
6+
import { isStringLiteralLike, maybeNodeArray } from '../util/utils';
77
import { Config } from './config';
88
import { FileResolver } from './fileResolver/fileResolver';
99
import { AnimationMetadata, CodeWithSourceMap, ComponentMetadata, DirectiveMetadata, StyleMetadata, TemplateMetadata } from './metadata';
@@ -80,12 +80,10 @@ export class MetadataReader {
8080

8181
protected readComponentAnimationsMetadata(dec: ts.Decorator): Maybe<(AnimationMetadata | undefined)[] | undefined> {
8282
return getAnimations(dec).fmap(inlineAnimations =>
83-
inlineAnimations!.elements
84-
.filter(inlineAnimation => isSimpleTemplateString(inlineAnimation))
85-
.map<AnimationMetadata>(inlineAnimation => ({
86-
animation: normalizeTransformed({ code: (inlineAnimation as ts.StringLiteralLike).text }),
87-
node: inlineAnimation as ts.Node
88-
}))
83+
inlineAnimations!.elements.filter(isStringLiteralLike).map<AnimationMetadata>(inlineAnimation => ({
84+
animation: normalizeTransformed({ code: (inlineAnimation as ts.StringLiteral).text }),
85+
node: inlineAnimation as ts.Node
86+
}))
8987
);
9088
}
9189

@@ -113,9 +111,9 @@ export class MetadataReader {
113111
return getInlineStyle(dec)
114112
.fmap(inlineStyles =>
115113
// Resolve Inline styles
116-
inlineStyles!.elements.filter(inlineStyle => isSimpleTemplateString(inlineStyle)).map<StyleMetadata>(inlineStyle => ({
114+
inlineStyles!.elements.filter(isStringLiteralLike).map<StyleMetadata>(inlineStyle => ({
117115
node: inlineStyle,
118-
style: normalizeTransformed(Config.transformStyle((inlineStyle as ts.StringLiteralLike).text))
116+
style: normalizeTransformed(Config.transformStyle((inlineStyle as ts.StringLiteral).text))
119117
}))
120118
)
121119
.catch(() =>

‎src/angular/urlResolvers/abstractResolver.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from 'typescript';
2-
import { getDecoratorArgument, isSimpleTemplateString } from '../../util/utils';
2+
import { getDecoratorArgument, isStringLiteralLike } from '../../util/utils';
33

44
export interface MetadataUrls {
55
templateUrl: string;
@@ -17,12 +17,12 @@ export abstract class AbstractResolver {
1717
}
1818

1919
const prop = arg.properties.find(
20-
p => (p.name as ts.StringLiteralLike).text === 'templateUrl' && isSimpleTemplateString((p as ts.PropertyAssignment).initializer)
20+
p => (p.name as ts.StringLiteral).text === 'templateUrl' && isStringLiteralLike((p as ts.PropertyAssignment).initializer)
2121
);
2222

2323
// We know that it's has an initializer because it's either
2424
// a template string or a string literal.
25-
return prop ? ((prop as ts.PropertyAssignment).initializer as ts.StringLiteralLike).text : undefined;
25+
return prop ? ((prop as ts.PropertyAssignment).initializer as ts.StringLiteral).text : undefined;
2626
}
2727

2828
protected getStyleUrls(decorator: ts.Decorator): string[] {
@@ -38,8 +38,8 @@ export abstract class AbstractResolver {
3838

3939
if (prop) {
4040
return ((prop as ts.PropertyAssignment).initializer as ts.ArrayLiteralExpression).elements
41-
.filter(isSimpleTemplateString)
42-
.map(e => (e as ts.StringLiteralLike).text);
41+
.filter(isStringLiteralLike)
42+
.map(e => (e as ts.StringLiteral).text);
4343
}
4444

4545
return [];

‎src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export { Rule as NoTemplateCallExpressionRule } from './noTemplateCallExpression
2424
export { Rule as NoUnusedCssRule } from './noUnusedCssRule';
2525
export { Rule as PipeImpureRule } from './pipeImpureRule';
2626
export { Rule as PipeNamingRule } from './pipeNamingRule';
27+
export { Rule as PreferInlineDecorator } from './preferInlineDecoratorRule';
2728
export { Rule as PreferOutputReadonlyRule } from './preferOutputReadonlyRule';
2829
export { Rule as TemplateConditionalComplexityRule } from './templateConditionalComplexityRule';
2930
export { Rule as TemplateCyclomaticComplexityRule } from './templateCyclomaticComplexityRule';

‎src/preferInlineDecoratorRule.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import * as ts from 'typescript';
2+
13
import { IOptions, IRuleMetadata, Replacement, RuleFailure, Rules } from 'tslint/lib';
2-
import { isSameLine } from 'tsutils';
34
import { Decorator, Node, PropertyAccessExpression, SourceFile } from 'typescript';
45
import { NgWalker } from './angular/ngWalker';
5-
import { getDecoratorName } from './util/utils';
6+
import { getDecoratorName, isSameLine } from './util/utils';
67

78
enum Decorators {
89
ContentChild = 'ContentChild',

‎src/selectorNameBase.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { sprintf } from 'sprintf-js';
33
import * as Lint from 'tslint';
44
import * as ts from 'typescript';
55
import { SelectorValidator } from './util/selectorValidator';
6-
import { getDecoratorArgument, getDecoratorName } from './util/utils';
6+
import { getDecoratorArgument, getDecoratorName, isStringLiteralLike } from './util/utils';
77

88
export type SelectorType = 'element' | 'attribute';
99
export type SelectorTypeInternal = 'element' | 'attrs';
@@ -148,7 +148,7 @@ export class SelectorValidatorWalker extends Lint.RuleWalker {
148148
}
149149

150150
private validateProperty(p: ts.PropertyAssignment): boolean {
151-
return ts.isStringLiteralLike(p.initializer) && ts.isIdentifier(p.name) && p.name.text === 'selector';
151+
return isStringLiteralLike(p.initializer) && ts.isIdentifier(p.name) && p.name.text === 'selector';
152152
}
153153

154154
private extractMainSelector(expression: ts.StringLiteral): compiler.CssSelector[] {

‎src/util/astQuery.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as ts from 'typescript';
22
import { Maybe, ifTrue } from './function';
3-
import { isSimpleTemplateString } from './utils';
3+
import { isStringLiteralLike } from './utils';
44

55
export function callExpression(dec?: ts.Decorator): Maybe<ts.CallExpression | undefined> {
66
return Maybe.lift(dec!.expression).fmap(expr => (expr && ts.isCallExpression(expr) ? expr : undefined));
@@ -29,13 +29,13 @@ export function getInitializer(p: ts.ObjectLiteralElement): Maybe<ts.Expression
2929
export function getStringInitializerFromProperty(
3030
propertyName: string,
3131
ps: ts.NodeArray<ts.ObjectLiteralElement>
32-
): Maybe<ts.StringLiteralLike | undefined> {
32+
): Maybe<ts.StringLiteral | undefined> {
3333
const property = ps.find(p => isProperty(propertyName, p))!;
3434

3535
return (
3636
getInitializer(property)
37-
// A little wrinkle to return Maybe<ts.StringLiteralLike>
38-
.fmap(expr => (expr && isSimpleTemplateString(expr) ? (expr as ts.StringLiteralLike) : undefined))
37+
// A little wrinkle to return Maybe<ts.StringLiteral>
38+
.fmap(expr => (expr && isStringLiteralLike(expr) ? (expr as ts.StringLiteral) : undefined))
3939
);
4040
}
4141

‎src/util/ngQuery.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export function getInlineStyle(dec: ts.Decorator): Maybe<ts.ArrayLiteralExpressi
1818
});
1919
}
2020

21-
export function getTemplate(dec: ts.Decorator): Maybe<ts.StringLiteralLike | undefined> {
21+
export function getTemplate(dec: ts.Decorator): Maybe<ts.StringLiteral | undefined> {
2222
return decoratorArgument(dec).bind(expr => getStringInitializerFromProperty('template', expr!.properties));
2323
}
2424

25-
export function getTemplateUrl(dec: ts.Decorator): Maybe<ts.StringLiteralLike | undefined> {
25+
export function getTemplateUrl(dec: ts.Decorator): Maybe<ts.StringLiteral | undefined> {
2626
return decoratorArgument(dec).bind(expr => getStringInitializerFromProperty('templateUrl', expr!.properties));
2727
}

‎src/util/utils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import * as ts from 'typescript';
22

3-
export const isSimpleTemplateString = (e: any): e is ts.SyntaxKind.FirstTemplateToken | ts.StringLiteralLike => {
4-
return ts.isStringLiteralLike(e) || e.kind === ts.SyntaxKind.FirstTemplateToken;
5-
};
6-
73
export const getClassName = (property: ts.PropertyDeclaration): string | undefined => {
84
const { parent } = property;
95
const identifier = parent && ts.isClassDeclaration(parent) ? parent.name : undefined;
@@ -61,3 +57,11 @@ export const getSymbolName = (expression: ts.ExpressionWithTypeArguments): strin
6157
export const maybeNodeArray = <T extends ts.Node>(nodes: ts.NodeArray<T>): ReadonlyArray<T> => {
6258
return nodes || [];
6359
};
60+
61+
export const isSameLine = (sourceFile: ts.SourceFile, pos1: number, pos2: number) => {
62+
return ts.getLineAndCharacterOfPosition(sourceFile, pos1).line === ts.getLineAndCharacterOfPosition(sourceFile, pos2).line;
63+
};
64+
65+
export const isStringLiteralLike = (node: ts.Node) => {
66+
return node.kind === ts.SyntaxKind.StringLiteral || node.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral;
67+
};

‎test/componentSelectorRule.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ describe('component-selector-prefix', () => {
144144
});
145145
});
146146
});
147+
147148
describe('component-selector-type', () => {
148149
describe('invalid component selectors', () => {
149150
it('should fail when component used as attribute', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.