Skip to content

Commit

Permalink
fix: regressions in 4.4.0
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mgechev committed Jun 23, 2018
1 parent a3f8679 commit c68b0f7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 19 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -86,7 +86,7 @@
"rxjs-compat": "^6.1.0",
"ts-node": "^6.0.2",
"tslint": "^5.10.0",
"typescript": "^2.8.0",
"typescript": "~2.7.0",
"zone.js": "^0.8.26"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/angular/metadataReader.ts
Expand Up @@ -83,7 +83,7 @@ export class MetadataReader {
inlineAnimations!.elements
.filter(inlineAnimation => isSimpleTemplateString(inlineAnimation))
.map<AnimationMetadata>(inlineAnimation => ({
animation: normalizeTransformed({ code: (inlineAnimation as ts.StringLiteralLike).text }),
animation: normalizeTransformed({ code: (inlineAnimation as ts.StringLiteral).text }),
node: inlineAnimation as ts.Node
}))
);
Expand Down Expand Up @@ -115,7 +115,7 @@ export class MetadataReader {
// Resolve Inline styles
inlineStyles!.elements.filter(inlineStyle => isSimpleTemplateString(inlineStyle)).map<StyleMetadata>(inlineStyle => ({
node: inlineStyle,
style: normalizeTransformed(Config.transformStyle((inlineStyle as ts.StringLiteralLike).text))
style: normalizeTransformed(Config.transformStyle((inlineStyle as ts.StringLiteral).text))
}))
)
.catch(() =>
Expand Down
6 changes: 3 additions & 3 deletions src/angular/urlResolvers/abstractResolver.ts
Expand Up @@ -17,12 +17,12 @@ export abstract class AbstractResolver {
}

const prop = arg.properties.find(
p => (p.name as ts.StringLiteralLike).text === 'templateUrl' && isSimpleTemplateString((p as ts.PropertyAssignment).initializer)
p => (p.name as ts.StringLiteral).text === 'templateUrl' && isSimpleTemplateString((p as ts.PropertyAssignment).initializer)
);

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

protected getStyleUrls(decorator: ts.Decorator): string[] {
Expand All @@ -39,7 +39,7 @@ export abstract class AbstractResolver {
if (prop) {
return ((prop as ts.PropertyAssignment).initializer as ts.ArrayLiteralExpression).elements
.filter(isSimpleTemplateString)
.map(e => (e as ts.StringLiteralLike).text);
.map(e => (e as ts.StringLiteral).text);
}

return [];
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -25,6 +25,7 @@ export { Rule as NoUnusedCssRule } from './noUnusedCssRule';
export { Rule as PipeImpureRule } from './pipeImpureRule';
export { Rule as PipeNamingRule } from './pipeNamingRule';
export { Rule as PreferOutputReadonlyRule } from './preferOutputReadonlyRule';
export { Rule as PreferInlineDecorator } from './preferInlineDecoratorRule';
export { Rule as TemplateConditionalComplexityRule } from './templateConditionalComplexityRule';
export { Rule as TemplateCyclomaticComplexityRule } from './templateCyclomaticComplexityRule';
export { Rule as TemplatesNoNegatedAsync } from './templatesNoNegatedAsyncRule';
Expand Down
7 changes: 6 additions & 1 deletion src/preferInlineDecoratorRule.ts
@@ -1,5 +1,6 @@
import * as ts from 'typescript';

import { IOptions, IRuleMetadata, Replacement, RuleFailure, Rules } from 'tslint/lib';
import { isSameLine } from 'tsutils';
import { Decorator, Node, PropertyAccessExpression, SourceFile } from 'typescript';
import { NgWalker } from './angular/ngWalker';
import { getDecoratorName } from './util/utils';
Expand Down Expand Up @@ -102,3 +103,7 @@ export class PreferInlineDecoratorWalker extends NgWalker {
this.addFailureAt(decoratorStartPos, property.getWidth(), getFailureMessage(), fix);
}
}

function isSameLine(sourceFile: ts.SourceFile, pos1: number, pos2: number) {
return ts.getLineAndCharacterOfPosition(sourceFile, pos1).line === ts.getLineAndCharacterOfPosition(sourceFile, pos2).line;
}
2 changes: 1 addition & 1 deletion src/selectorNameBase.ts
Expand Up @@ -148,7 +148,7 @@ export class SelectorValidatorWalker extends Lint.RuleWalker {
}

private validateProperty(p: ts.PropertyAssignment): boolean {
return ts.isStringLiteralLike(p.initializer) && ts.isIdentifier(p.name) && p.name.text === 'selector';
return p.initializer.kind === ts.SyntaxKind.StringLiteral && ts.isIdentifier(p.name) && p.name.text === 'selector';
}

private extractMainSelector(expression: ts.StringLiteral): compiler.CssSelector[] {
Expand Down
6 changes: 3 additions & 3 deletions src/util/astQuery.ts
Expand Up @@ -29,13 +29,13 @@ export function getInitializer(p: ts.ObjectLiteralElement): Maybe<ts.Expression
export function getStringInitializerFromProperty(
propertyName: string,
ps: ts.NodeArray<ts.ObjectLiteralElement>
): Maybe<ts.StringLiteralLike | undefined> {
): Maybe<ts.StringLiteral | undefined> {
const property = ps.find(p => isProperty(propertyName, p))!;

return (
getInitializer(property)
// A little wrinkle to return Maybe<ts.StringLiteralLike>
.fmap(expr => (expr && isSimpleTemplateString(expr) ? (expr as ts.StringLiteralLike) : undefined))
// A little wrinkle to return Maybe<ts.StringLiteral>
.fmap(expr => (expr && isSimpleTemplateString(expr) ? (expr as ts.StringLiteral) : undefined))
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/ngQuery.ts
Expand Up @@ -18,10 +18,10 @@ export function getInlineStyle(dec: ts.Decorator): Maybe<ts.ArrayLiteralExpressi
});
}

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

export function getTemplateUrl(dec: ts.Decorator): Maybe<ts.StringLiteralLike | undefined> {
export function getTemplateUrl(dec: ts.Decorator): Maybe<ts.StringLiteral | undefined> {
return decoratorArgument(dec).bind(expr => getStringInitializerFromProperty('templateUrl', expr!.properties));
}
4 changes: 2 additions & 2 deletions src/util/utils.ts
@@ -1,7 +1,7 @@
import * as ts from 'typescript';

export const isSimpleTemplateString = (e: any): e is ts.SyntaxKind.FirstTemplateToken | ts.StringLiteralLike => {
return ts.isStringLiteralLike(e) || e.kind === ts.SyntaxKind.FirstTemplateToken;
export const isSimpleTemplateString = (e: any): e is ts.SyntaxKind.FirstTemplateToken | ts.StringLiteral => {
return e.kind === ts.SyntaxKind.StringLiteral || e.kind === ts.SyntaxKind.FirstTemplateToken;
};

export const getClassName = (property: ts.PropertyDeclaration): string | undefined => {
Expand Down

0 comments on commit c68b0f7

Please sign in to comment.