Skip to content

Commit

Permalink
chore: update to typescript 2.8
Browse files Browse the repository at this point in the history
Migrate to TypeScript 2.8 and open the version range.
  • Loading branch information
mgechev committed Apr 27, 2018
1 parent 204c8ef commit af8488a
Show file tree
Hide file tree
Showing 12 changed files with 1,346 additions and 916 deletions.
2,015 changes: 1,208 additions & 807 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"rxjs-compat": "^6.0.0",
"ts-node": "1.2.2",
"tslint": "^5.0.0",
"typescript": "2.4.0",
"typescript": "^2.8.0",
"zone.js": "^0.8.20"
},
"peerDependencies": {
Expand Down
9 changes: 5 additions & 4 deletions src/angular/metadataReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
decoratorArgument
} from '../util/astQuery';
import { getTemplate, getInlineStyle } from '../util/ngQuery';
import { maybeNodeArray } from '../util/utils';

const normalizeTransformed = (t: CodeWithSourceMap) => {
if (!t.map) {
Expand All @@ -36,17 +37,17 @@ export class MetadataReader {
}

read(d: ts.ClassDeclaration): DirectiveMetadata {
let componentMetadata = unwrapFirst(
(d.decorators || ([] as ts.Decorator[])).map((dec: ts.Decorator) => {
const componentMetadata = unwrapFirst(
maybeNodeArray(d.decorators).map((dec: ts.Decorator) => {
return Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('Component'))
.fmap(() => this.readComponentMetadata(d, dec));
})
);

let directiveMetadata = unwrapFirst(
(d.decorators || ([] as ts.Decorator[])).map((dec: ts.Decorator) =>
const directiveMetadata = unwrapFirst(
maybeNodeArray(d.decorators).map((dec: ts.Decorator) =>
Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('Directive'))
Expand Down
12 changes: 6 additions & 6 deletions src/angular/ngWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ngWalkerFactoryUtils } from './ngWalkerFactoryUtils';
import { Config } from './config';

import { logger } from '../util/logger';
import { getDecoratorName } from '../util/utils';
import { getDecoratorName, maybeNodeArray } from '../util/utils';

const getDecoratorStringArgs = (decorator: ts.Decorator) => {
let baseExpr = <any>decorator.expression || {};
Expand Down Expand Up @@ -59,17 +59,17 @@ export class NgWalker extends Lint.RuleWalker {
} else if (metadata instanceof DirectiveMetadata) {
this.visitNgDirective(metadata);
}
(<ts.Decorator[]>declaration.decorators || []).forEach(this.visitClassDecorator.bind(this));
maybeNodeArray(<ts.NodeArray<ts.Decorator>>declaration.decorators).forEach(this.visitClassDecorator.bind(this));
super.visitClassDeclaration(declaration);
}

visitMethodDeclaration(method: ts.MethodDeclaration) {
(<ts.Decorator[]>method.decorators || []).forEach(this.visitMethodDecorator.bind(this));
maybeNodeArray(<ts.NodeArray<ts.Decorator>>method.decorators).forEach(this.visitMethodDecorator.bind(this));
super.visitMethodDeclaration(method);
}

visitPropertyDeclaration(prop: ts.PropertyDeclaration) {
(<ts.Decorator[]>prop.decorators || []).forEach(this.visitPropertyDecorator.bind(this));
maybeNodeArray(<ts.NodeArray<ts.Decorator>>prop.decorators).forEach(this.visitPropertyDecorator.bind(this));
super.visitPropertyDeclaration(prop);
}

Expand Down Expand Up @@ -227,10 +227,10 @@ export class NgWalker extends Lint.RuleWalker {
}
const sf = ts.createSourceFile(path, `\`${content}\``, ts.ScriptTarget.ES5);
const original = sf.getFullText;
sf.getFullText = function() {
sf.getFullText = () => {
const text = original.apply(sf);
return text.substring(1, text.length - 1);
}.bind(sf);
};
return sf;
}
}
28 changes: 18 additions & 10 deletions src/propertyDecoratorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class UsePropertyDecorator extends Lint.Rules.AbstractRule {
public static formatFailureString(config: IUsePropertyDecoratorConfig, decoratorName: string, className: string) {
let decorators = config.decoratorName;
if (decorators instanceof Array) {
decorators = (<string[]>decorators).map(d => `"@${d}"`).join(', ');
decorators = (<string[]> decorators).map(d => `"@${d}"`).join(', ');
} else {
decorators = `"@${decorators}"`;
}
Expand All @@ -26,7 +26,9 @@ export class UsePropertyDecorator extends Lint.Rules.AbstractRule {
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new DirectiveMetadataWalker(sourceFile, this.getOptions(), this.config));
return this.applyWithWalker(
new DirectiveMetadataWalker(sourceFile,
this.getOptions(), this.config));
}
}

Expand All @@ -36,12 +38,13 @@ class DirectiveMetadataWalker extends Lint.RuleWalker {
}

visitClassDeclaration(node: ts.ClassDeclaration) {
(<ts.Decorator[]>node.decorators || []).forEach(this.validateDecorator.bind(this, node.name.text));
(<ts.NodeArray<ts.Decorator>> node.decorators)
.forEach(this.validateDecorator.bind(this, node.name.text));
super.visitClassDeclaration(node);
}

private validateDecorator(className: string, decorator: ts.Decorator) {
let baseExpr = <any>decorator.expression || {};
let baseExpr = <any> decorator.expression || {};
let expr = baseExpr.expression || {};
let name = expr.text;
let args = baseExpr.arguments || [];
Expand All @@ -53,12 +56,17 @@ class DirectiveMetadataWalker extends Lint.RuleWalker {

private validateProperty(className: string, decoratorName: string, arg: ts.ObjectLiteralExpression) {
if (arg.kind === SyntaxKind.current().ObjectLiteralExpression) {
(<ts.ObjectLiteralExpression>arg).properties.filter(prop => (<any>prop.name).text === this.config.propertyName).forEach(prop => {
let p = <any>prop;
this.addFailure(
this.createFailure(p.getStart(), p.getWidth(), UsePropertyDecorator.formatFailureString(this.config, decoratorName, className))
);
});
(<ts.ObjectLiteralExpression> arg)
.properties
.filter(prop => (<any> prop.name).text === this.config.propertyName)
.forEach(prop => {
let p = <any> prop;
this.addFailure(
this.createFailure(
p.getStart(),
p.getWidth(),
UsePropertyDecorator.formatFailureString(this.config, decoratorName, className)));
});
}
}
}
14 changes: 8 additions & 6 deletions src/selectorNameBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ export class SelectorValidatorWalker extends Lint.RuleWalker {
}

visitClassDeclaration(node: ts.ClassDeclaration) {
(<ts.Decorator[]>node.decorators || []).forEach(this.validateDecorator.bind(this, node.name.text));
if (node.decorators) {
(<ts.NodeArray<ts.Decorator>> node.decorators)
.forEach(this.validateDecorator.bind(this, node.name.text));
}
super.visitClassDeclaration(node);
}

private validateDecorator(className: string, decorator: ts.Decorator) {
let baseExpr = <any>decorator.expression || {};
let baseExpr = <any> decorator.expression || {};
let expr = baseExpr.expression || {};
let name = expr.text;
let args = baseExpr.arguments || [];
Expand All @@ -121,9 +124,8 @@ export class SelectorValidatorWalker extends Lint.RuleWalker {

private validateSelector(className: string, arg: ts.Node) {
if (arg.kind === SyntaxKind.current().ObjectLiteralExpression) {
(<ts.ObjectLiteralExpression>arg).properties
.filter(prop => this.validateProperty(prop))
.map(prop => (<any>prop).initializer)
(<ts.ObjectLiteralExpression> arg).properties.filter(prop => this.validateProperty(prop))
.map(prop => (<any> prop).initializer)
.forEach(i => {
const selectors: compiler.CssSelector[] = this.extractMainSelector(i);
if (!this.rule.validateType(selectors)) {
Expand All @@ -145,7 +147,7 @@ export class SelectorValidatorWalker extends Lint.RuleWalker {
}

private validateProperty(p: any) {
return (<any>p.name).text === 'selector' && p.initializer && this.isSupportedKind(p.initializer.kind);
return (<any> p.name).text === 'selector' && p.initializer && this.isSupportedKind(p.initializer.kind);
}

private isSupportedKind(kind: number): boolean {
Expand Down
17 changes: 11 additions & 6 deletions src/usePipeDecoratorRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Lint from 'tslint';
import * as ts from 'typescript';
import { sprintf } from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
import { maybeNodeArray } from './util/utils';

const getInterfaceName = (t: any) => {
if (t.expression && t.expression.name) {
Expand Down Expand Up @@ -32,13 +33,17 @@ export class Rule extends Lint.Rules.AbstractRule {
export class ClassMetadataWalker extends Lint.RuleWalker {
visitClassDeclaration(node: ts.ClassDeclaration) {
if (this.hasIPipeTransform(node)) {
let decorators = <any[]>node.decorators || [];
let className: string = node.name.text;
let pipes: Array<string> = decorators
.map(d => (<any>d.expression).text || ((<any>d.expression).expression || {}).text)
.filter(t => t === 'Pipe');
const decorators = maybeNodeArray(<ts.NodeArray<any>> node.decorators);
const className: string = node.name.text;
const pipes: Array<string> = decorators.map(d =>
(<any> d.expression).text ||
((<any> d.expression).expression || {}).text).filter(t => t === 'Pipe');
if (pipes.length === 0) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), sprintf.apply(this, [Rule.FAILURE, className])));
this.addFailure(
this.createFailure(
node.getStart(),
node.getWidth(),
sprintf.apply(this, [Rule.FAILURE, className])));
}
}
super.visitClassDeclaration(node);
Expand Down
5 changes: 4 additions & 1 deletion src/util/astQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export function getInitializer(p: ts.ObjectLiteralElement): Maybe<ts.Expression>
return Maybe.lift(isPropertyAssignment(p) && isIdentifier(p.name) ? p.initializer : undefined);
}

export function getStringInitializerFromProperty(propertyName: string, ps: ts.ObjectLiteralElement[]): Maybe<WithStringInitializer> {
export function getStringInitializerFromProperty(
propertyName: string,
ps: ts.NodeArray<ts.ObjectLiteralElement>
): Maybe<WithStringInitializer> {
const property = ps.find(p => isProperty(propertyName, p));
return (
getInitializer(property)
Expand Down
9 changes: 8 additions & 1 deletion src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const getDecoratorName = (decorator: ts.Decorator) => {
};

export const getComponentDecorator = (declaration: ts.ClassDeclaration) => {
return (<ts.Decorator[]>declaration.decorators || [])
return ([].slice.apply(declaration.decorators) || [])
.filter((d: any) => {
if (
!(<ts.CallExpression>d.expression).arguments ||
Expand All @@ -60,3 +60,10 @@ export const getComponentDecorator = (declaration: ts.ClassDeclaration) => {
})
.pop();
};

export const maybeNodeArray = <T extends ts.Node>(nodes: ts.NodeArray<T>): ReadonlyArray<T> => {
if (!nodes) {
return [];
}
return nodes;
};
Loading

0 comments on commit af8488a

Please sign in to comment.