Skip to content

Commit

Permalink
Add new object/array-destructuring options to typedef rule (palantir#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomiassaf authored and nchen63 committed Jan 28, 2017
1 parent ffad65f commit 26700c0
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 9 deletions.
54 changes: 46 additions & 8 deletions src/rules/typedefRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export class Rule extends Lint.Rules.AbstractRule {
* \`"parameter"\` checks type specifier of function parameters for non-arrow functions.
* \`"arrow-parameter"\` checks type specifier of function parameters for arrow functions.
* \`"property-declaration"\` checks return types of interface properties.
* \`"variable-declaration"\` checks variable declarations.
* \`"member-variable-declaration"\` checks member variable declarations.`,
* \`"variable-declaration"\` checks non-binding variable declarations.
* \`"member-variable-declaration"\` checks member variable declarations.
* \`"object-destructuring"\` checks object destructuring declarations.
* \`"array-destructuring"\` checks array destructuring declarations.`,
options: {
type: "array",
items: {
Expand All @@ -46,6 +48,8 @@ export class Rule extends Lint.Rules.AbstractRule {
"property-declaration",
"variable-declaration",
"member-variable-declaration",
"object-destructuring",
"array-destructuring",
],
},
minLength: 0,
Expand Down Expand Up @@ -194,7 +198,21 @@ class TypedefWalker extends Lint.RuleWalker {
&& (node as ts.Node).parent!.kind !== ts.SyntaxKind.CatchClause
&& node.parent.parent.kind !== ts.SyntaxKind.ForInStatement
&& node.parent.parent.kind !== ts.SyntaxKind.ForOfStatement) {
this.checkTypeAnnotation("variable-declaration", node.name.getEnd(), node.type, node.name);

let rule: string;
switch (node.name.kind) {
case ts.SyntaxKind.ObjectBindingPattern:
rule = "object-destructuring";
break;
case ts.SyntaxKind.ArrayBindingPattern:
rule = "array-destructuring";
break;
default:
rule = "variable-declaration";
break;
}

this.checkTypeAnnotation(rule, node.name.getEnd(), node.type, node.name);
}
super.visitVariableDeclaration(node);
}
Expand All @@ -212,13 +230,33 @@ class TypedefWalker extends Lint.RuleWalker {
typeAnnotation: ts.TypeNode | undefined,
name?: ts.Node) {
if (this.hasOption(option) && typeAnnotation == null) {
let ns = "";
if (name != null && name.kind === ts.SyntaxKind.Identifier) {
ns = `: '${(name as ts.Identifier).text}'`;
}
this.addFailureAt(location, 1, "expected " + option + ns + " to have a typedef");
this.addFailureAt(location, 1, "expected " + option + getName(name, ": '", "'") + " to have a typedef");
}
}
}

function getName(name?: ts.Node, prefix?: string, suffix?: string): string {
let ns = "";

if (name != null) {
switch (name.kind) {
case ts.SyntaxKind.Identifier:
ns = (name as ts.Identifier).text;
break;
case ts.SyntaxKind.BindingElement:
ns = getName((name as ts.BindingElement).name);
break;
case ts.SyntaxKind.ArrayBindingPattern:
ns = `[ ${(name as ts.ArrayBindingPattern).elements.map( (n) => getName(n) ).join(", ")} ]`;
break;
case ts.SyntaxKind.ObjectBindingPattern:
ns = `{ ${(name as ts.ObjectBindingPattern).elements.map( (n) => getName(n) ).join(", ")} }`;
break;
default:
break;
}
}
return ns ? `${prefix || ""}${ns}${suffix || ""}` : "";
}

function isPropertyDeclaration(node: ts.Node): node is ts.PropertyDeclaration {
Expand Down
12 changes: 12 additions & 0 deletions test/rules/typedef/all/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,15 @@ class A {
~ [expected member-variable-declaration: 'foo' to have a typedef]
private bar: number;
}


const { paramA, paramB } = { paramA: "test", paramB: 15 };
~ [expected object-destructuring: '{ paramA, paramB }' to have a typedef]

const { paramA, paramB }: { paramA: string, paramB: number } = { paramA: "test", paramB: 15 };

const [ paramA, paramB ] = [15, 'test'];
~ [expected array-destructuring: '[ paramA, paramB ]' to have a typedef]

const [ paramA3, paramB3 ]: [ number, string ] = [15, 'test'];

4 changes: 3 additions & 1 deletion test/rules/typedef/all/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"arrow-parameter",
"variable-declaration",
"property-declaration",
"member-variable-declaration"
"member-variable-declaration",
"object-destructuring",
"array-destructuring"
]
}
}
4 changes: 4 additions & 0 deletions test/rules/typedef/array-destructuring/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const [ paramA, paramB ] = [15, 'test'];
~ [expected array-destructuring: '[ paramA, paramB ]' to have a typedef]

const [ paramA3, paramB3 ]: { number: string, paramB1: string } = [15, 'test'];
7 changes: 7 additions & 0 deletions test/rules/typedef/array-destructuring/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rules": {
"typedef": [true,
"array-destructuring"
]
}
}
8 changes: 8 additions & 0 deletions test/rules/typedef/none/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,11 @@ try {
for (let i of [1, 2, 3]) {

}

const [ paramA, paramB ] = [ 'test', 15 ];

const { paramA, paramB } = { paramA: "test", paramB: 15 };

const [ paramA3, paramB3 ]: [ number, string ] = [15, 'test'];

const { paramA, paramB }: { paramA: string, paramB: number } = { paramA: "test", paramB: 15 };
7 changes: 7 additions & 0 deletions test/rules/typedef/object-destructuring/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


const { paramA, paramB } = { paramA: "test", paramB: 15 };
~ [expected object-destructuring: '{ paramA, paramB }' to have a typedef]


const { paramA, paramB }: { paramA: string, paramB: number } = { paramA: "test", paramB: 15 };
7 changes: 7 additions & 0 deletions test/rules/typedef/object-destructuring/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rules": {
"typedef": [true,
"object-destructuring"
]
}
}
6 changes: 6 additions & 0 deletions test/rules/typedef/variable-declaration/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var NoTypeObjectLiteralWithPropertyGetter = { }
~ [expected variable-declaration: 'NoTypeObjectLiteralWithPropertyGetter' to have a typedef]


const { paramA, paramB } = { paramA: "test", paramB: 15 };
const [ paramA, paramB ] = [15, 'test'];
7 changes: 7 additions & 0 deletions test/rules/typedef/variable-declaration/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rules": {
"typedef": [true,
"variable-declaration"
]
}
}

0 comments on commit 26700c0

Please sign in to comment.