Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 8915146

Browse files
greenkeeperio-botKnisterPeter
authored andcommitted
Update tslint to version 4.0.1 🚀 (#233)
* chore(package): update tslint to version 4.0.1 https://greenkeeper.io/ * chore: use shared standard tslint configuration
1 parent 283423e commit 8915146

File tree

11 files changed

+66
-123
lines changed

11 files changed

+66
-123
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ matrix:
88
branches:
99
only:
1010
- master
11+
- /^greenkeeper-.*$/
1112
notifications:
1213
email:
1314
on_success: never
1415
cache:
1516
directories:
1617
- node_modules
1718

19+
before_script: 'npm run linter'
20+
script: 'npm run test'
1821
after_script: 'npm run coverage'
22+

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"index.d.ts"
1313
],
1414
"scripts": {
15+
"linter": "tslint --project ./tsconfig.json --type-check",
1516
"start": "npm test",
1617
"clean": "rimraf dist",
1718
"prebuild": "npm run clean",
@@ -35,6 +36,7 @@
3536
},
3637
"license": "MIT",
3738
"devDependencies": {
39+
"@knisterpeter/standard-tslint": "1.0.2",
3840
"@types/chalk": "0.4.31",
3941
"@types/diff": "0.0.31",
4042
"@types/node": "6.0.46",
@@ -55,7 +57,7 @@
5557
"rimraf": "2.5.4",
5658
"source-map-support": "0.4.6",
5759
"standard-version": "3.0.0",
58-
"tslint": "4.0.0",
60+
"tslint": "4.0.1",
5961
"typescript": "2.0.10"
6062
},
6163
"dependencies": {

src/analyzer.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as ASTQ from 'astq';
22

3-
import { InstanceOfResolver } from './index';
43
import { IPropTypes, IProp } from './deprecated';
4+
import { InstanceOfResolver } from './index';
55

6-
interface IASTNode {
6+
type IASTNode = ASTNode;
7+
interface ASTNode {
78
type: string;
89
loc: Object;
910
[name: string]: any;
@@ -30,13 +31,14 @@ export function parsePropTypes(node: any, instanceOfResolver?: InstanceOfResolve
3031

3132
function getOptionalDocumentation(propertyNode: any): string {
3233
return (((propertyNode.leadingComments || []) as any[])
33-
.filter(comment => comment.type == 'CommentBlock')[0] || {})
34+
.filter(comment => comment.type === 'CommentBlock')[0] || {})
3435
.value;
3536
}
3637

3738
/**
3839
* @internal
3940
*/
41+
// tslint:disable:next-line cyclomatic-complexity
4042
export function getTypeFromPropType(node: IASTNode, instanceOfResolver = defaultInstanceOfResolver): IProp {
4143
const result: IProp = {
4244
type: 'any',
@@ -91,14 +93,14 @@ export function getTypeFromPropType(node: IASTNode, instanceOfResolver = default
9193
}
9294

9395
function isNode(obj: IASTNode): boolean {
94-
return obj && typeof obj.type != 'undefined' && typeof obj.loc != 'undefined';
96+
return obj && typeof obj.type !== 'undefined' && typeof obj.loc !== 'undefined';
9597
}
9698

9799
function getReactPropTypeFromExpression(node: any, instanceOfResolver: InstanceOfResolver): any {
98-
if (node.type == 'MemberExpression' && node.object.type == 'MemberExpression'
99-
&& node.object.object.name == 'React' && node.object.property.name == 'PropTypes') {
100+
if (node.type === 'MemberExpression' && node.object.type === 'MemberExpression'
101+
&& node.object.object.name === 'React' && node.object.property.name === 'PropTypes') {
100102
return node.property;
101-
} else if (node.type == 'CallExpression') {
103+
} else if (node.type === 'CallExpression') {
102104
const callType = getReactPropTypeFromExpression(node.callee, instanceOfResolver);
103105
switch (callType.name) {
104106
case 'instanceOf':
@@ -111,7 +113,7 @@ function getReactPropTypeFromExpression(node: any, instanceOfResolver: InstanceO
111113
const arrayType = getTypeFromPropType(node.arguments[0], instanceOfResolver);
112114
return {
113115
name: 'array',
114-
arrayType: arrayType.type,
116+
arrayType: arrayType.type
115117
};
116118
case 'oneOfType':
117119
const unionTypes = node.arguments[0].elements.map((element: IASTNode) =>
@@ -126,7 +128,7 @@ function getReactPropTypeFromExpression(node: any, instanceOfResolver: InstanceO
126128
}
127129

128130
function isRequiredPropType(node: any, instanceOfResolver: InstanceOfResolver): any {
129-
const isRequired = node.type == 'MemberExpression' && node.property.name == 'isRequired';
131+
const isRequired = node.type === 'MemberExpression' && node.property.name === 'isRequired';
130132
return {
131133
isRequired,
132134
type: getReactPropTypeFromExpression(isRequired ? node.object : node, instanceOfResolver)

src/deprecated.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import * as ASTQ from 'astq';
2-
import { IOptions, InstanceOfResolver } from './index';
3-
import { Generator } from './generator';
2+
43
import { parsePropTypes } from './analyzer';
4+
import { Generator } from './generator';
5+
import { IOptions, InstanceOfResolver } from './index';
56

67
export enum ExportType {
78
default,
89
named
910
}
1011

12+
// tslint:disable:next-line interface-name
1113
export interface IProp {
1214
type: string;
1315
optional: boolean;
@@ -16,6 +18,7 @@ export interface IProp {
1618
documentation?: string;
1719
}
1820

21+
// tslint:disable:next-line interface-name
1922
export interface IPropTypes {
2023
[name: string]: IProp;
2124
}
@@ -64,6 +67,7 @@ export interface IParsingResult {
6467
propTypes: IPropTypes;
6568
}
6669

70+
// tslint:disable:next-line cyclomatic-complexity
6771
function parseAst(ast: any, instanceOfResolver?: InstanceOfResolver): IParsingResult {
6872
let exportType: ExportType|undefined;
6973
let functionname: string|undefined;

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class Generator {
9292
public exportDeclaration(exportType: ExportType, fn: () => void): void {
9393
this.indent();
9494
this.code += 'export ';
95-
if (exportType == ExportType.default) {
95+
if (exportType === ExportType.default) {
9696
this.code += 'default ';
9797
}
9898
fn();

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as fs from 'fs';
21
import * as babylon from 'babylon';
2+
import * as fs from 'fs';
33
import * as getStdin from 'get-stdin';
44

55
import { generateTypings } from './deprecated';
@@ -10,7 +10,9 @@ export interface InstanceOfResolver {
1010
(name: string): string|undefined;
1111
};
1212

13-
export interface IOptions {
13+
// the IOptions is for backward compatibility
14+
export type IOptions = Options;
15+
export interface Options {
1416

1517
/**
1618
* Resolves type names to import paths.

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function get(astq: ASTQ, propertyAst: any, propTypesName: string|undefine
3434
};
3535
}
3636

37+
// tslint:disable:next-line cyclomatic-complexity
3738
function getSimpleType(astq: ASTQ, propertyAst: any, propTypesName: string|undefined): TypeDeclaration|undefined {
3839
const [required, simpleTypeName] = getSimpleTypeName(astq, propertyAst, propTypesName);
3940
switch (simpleTypeName) {
@@ -79,6 +80,7 @@ function getComplexType(astq: ASTQ, propertyAst: any, propTypesName: string|unde
7980
const typeDecl = get(astq, typeAst.arguments[0], propTypesName);
8081
return getTypeDeclaration(dom.create.array(typeDecl.type), !required);
8182
case 'oneOf':
83+
// tslint:disable:next-line comment-format
8284
// FIXME: This should better be a real enum
8385
const enumEntries = getEnumValues(typeAst.arguments[0].elements);
8486
return getTypeDeclaration(dom.create.union(enumEntries as dom.Type[]), !required);
@@ -90,7 +92,7 @@ function getComplexType(astq: ASTQ, propertyAst: any, propTypesName: string|unde
9092
});
9193
return getTypeDeclaration(dom.create.objectType(entries), !required);
9294
}
93-
return undefined;
95+
return undefined;
9496
}
9597

9698
function isRequired(astq: ASTQ, propertyAst: any): [boolean, any] {
@@ -128,6 +130,7 @@ function getComplexTypeName(astq: ASTQ, propertyAst: any,
128130

129131
function getEnumValues(oneOfTypes: any[]): any[] {
130132
return oneOfTypes.map((element: any) => {
133+
// tslint:disable:next-line comment-format
131134
// FIXME: This are not named references!
132135
if (element.type === 'StringLiteral') {
133136
return dom.create.namedTypeReference(`'${element.value}'`);

src/typings.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function createTypings(moduleName: string|null, ast: any,
2222
}
2323
if (importStatements.length > 0) {
2424
importStatements.forEach(importStatement => {
25-
if (importStatement.name == undefined) {
25+
if (importStatement.name === undefined) {
2626
m.members.push(dom.create.importDefault(importStatement.local, importStatement.path));
2727
} else {
2828
throw new Error('Named imports are currently unsupported');
@@ -34,28 +34,7 @@ export function createTypings(moduleName: string|null, ast: any,
3434
const propTypes = getPropTypesFromAssignment(astq, ast, componentName) ||
3535
getPropTypesFromStaticAttribute(astq, ast, componentName);
3636
if (exportType) {
37-
const classComponent = isClassComponent(astq, ast, componentName, reactComponentName);
38-
39-
const interf = dom.create.interface(`${componentName}Props`);
40-
interf.flags = dom.DeclarationFlags.Export;
41-
if (propTypes) {
42-
createPropTypeTypings(interf, astq, propTypes, propTypesName);
43-
}
44-
if (propTypes || classComponent) {
45-
m.members.push(interf);
46-
}
47-
48-
if (classComponent) {
49-
const classDecl = dom.create.class(componentName);
50-
classDecl.baseType = dom.create.interface(`React.Component<${interf.name}, any>`);
51-
classDecl.flags = exportType;
52-
m.members.push(classDecl);
53-
} else {
54-
const funcDelc = dom.create.function(componentName, propTypes ? [dom.create.parameter('props', interf)] : [],
55-
dom.create.namedTypeReference('JSX.Element'));
56-
funcDelc.flags = exportType;
57-
m.members.push(funcDelc);
58-
}
37+
createExportedTypes(m, astq, ast, componentName, reactComponentName, propTypes, propTypesName, exportType);
5938
}
6039
});
6140

@@ -68,6 +47,33 @@ export function createTypings(moduleName: string|null, ast: any,
6847
}
6948
};
7049

50+
function createExportedTypes(m: dom.ModuleDeclaration, astq: ASTQ, ast: any, componentName: string,
51+
reactComponentName: string|undefined, propTypes: any, propTypesName: string|undefined,
52+
exportType: dom.DeclarationFlags): void {
53+
const classComponent = isClassComponent(astq, ast, componentName, reactComponentName);
54+
55+
const interf = dom.create.interface(`${componentName}Props`);
56+
interf.flags = dom.DeclarationFlags.Export;
57+
if (propTypes) {
58+
createPropTypeTypings(interf, astq, propTypes, propTypesName);
59+
}
60+
if (propTypes || classComponent) {
61+
m.members.push(interf);
62+
}
63+
64+
if (classComponent) {
65+
const classDecl = dom.create.class(componentName);
66+
classDecl.baseType = dom.create.interface(`React.Component<${interf.name}, any>`);
67+
classDecl.flags = exportType;
68+
m.members.push(classDecl);
69+
} else {
70+
const funcDelc = dom.create.function(componentName, propTypes ? [dom.create.parameter('props', interf)] : [],
71+
dom.create.namedTypeReference('JSX.Element'));
72+
funcDelc.flags = exportType;
73+
m.members.push(funcDelc);
74+
}
75+
}
76+
7177
function createPropTypeTypings(interf: dom.InterfaceDeclaration, astq: ASTQ, propTypes: any,
7278
propTypesName: string|undefined): void {
7379
const res = astq.query(propTypes, `

tests/parse-prop-types-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22

3-
import { IProp } from '../src/deprecated';
43
import { getTypeFromPropType } from '../src/analyzer';
4+
import { IProp } from '../src/deprecated';
55

66
const instanceOfResolver = (): any => undefined;
77
const reactPropTypesMemberExpression: any = {

tests/parsing-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import test, { ContextualTestContext } from 'ava';
22

3-
import * as fs from 'fs';
4-
import * as path from 'path';
53
import * as chalk from 'chalk';
64
import * as diff from 'diff';
5+
import * as fs from 'fs';
6+
import * as path from 'path';
77

88
import * as react2dts from '../src/index';
99

0 commit comments

Comments
 (0)