Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 8f22d81

Browse files
committed
WIP: Add array-type rule
1 parent f6041a6 commit 8f22d81

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

docs/rules/array-type.md

Whitespace-only changes.

lib/rules/array-type.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @fileoverview Requires using either 'T[]' or 'Array' for arrays.
3+
* @author Mackie Underdown
4+
*/
5+
"use strict";
6+
7+
const util = require("../util");
8+
9+
//------------------------------------------------------------------------------
10+
// Rule Definition
11+
//------------------------------------------------------------------------------
12+
13+
module.exports = {
14+
meta: {
15+
docs: {
16+
description: "Requires using either 'T[]' or 'Array' for arrays",
17+
extraDescription: [util.tslintRule("array-type")],
18+
category: "TypeScript",
19+
url:
20+
"https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/array-type.md"
21+
},
22+
schema: [
23+
{
24+
enum: ["array", "generic", "array-simple"]
25+
}
26+
]
27+
},
28+
create(context) {
29+
const arrayType = context.options[0] || "array";
30+
//----------------------------------------------------------------------
31+
// Public
32+
//----------------------------------------------------------------------
33+
34+
return {
35+
TypeAnnotation: console.log, // eslint-disable-line no-console
36+
TSArrayType(node) {
37+
console.log(node); // eslint-disable-line no-console
38+
if (arrayType === "generic") {
39+
context.report({
40+
node,
41+
message:
42+
"Array type using 'T[]' is forbidden. Use 'Array<T>' instead."
43+
});
44+
}
45+
},
46+
TSTypeReference(node) {
47+
console.log(node); // eslint-disable-line no-console
48+
if (node.typeName.name === "Array" && arrayType === "array") {
49+
context.report({
50+
node,
51+
message:
52+
"Array type using 'Array<T>' is forbidden. Use 'T[]' instead."
53+
});
54+
}
55+
}
56+
};
57+
}
58+
};

tests/lib/rules/array-type.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @fileoverview Requires using either 'T[]' or 'Array' for arrays.
3+
* @author Mackie Underdown
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
const rule = require("../../../lib/rules/array-type"),
12+
RuleTester = require("eslint").RuleTester;
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const arrayMessage =
19+
"Array type using 'Array<T>' is forbidden. Use 'T[]' instead.";
20+
const genericMessage =
21+
"Array type using 'T[]' is forbidden. Use 'Array<T>' instead.";
22+
23+
const ruleTester = new RuleTester({
24+
parser: "typescript-eslint-parser"
25+
});
26+
27+
ruleTester.run("array-type", rule, {
28+
valid: [
29+
{
30+
code: "let a: string[] = []",
31+
options: ["array"]
32+
},
33+
{
34+
code: "let a: (string | number)[] = []",
35+
options: ["array"]
36+
},
37+
{
38+
code: "let a: ({ foo: Bar[] })[] = []",
39+
options: ["array"]
40+
},
41+
{
42+
code: "let a: Array<string> = []",
43+
options: ["generic"]
44+
},
45+
{
46+
code: "let a: Array<string| number> = []",
47+
options: ["generic"]
48+
},
49+
{
50+
code: "let a: Array<{ foo: Array<Bar> }> = []",
51+
options: ["generic"]
52+
}
53+
],
54+
invalid: [
55+
{
56+
code: "let a: Array<string> = []",
57+
options: ["array"],
58+
errors: [
59+
{
60+
message: arrayMessage,
61+
line: 1,
62+
column: 8
63+
}
64+
]
65+
},
66+
{
67+
code: "let a: Array<string | number> = []",
68+
options: ["array"],
69+
errors: [
70+
{
71+
message: arrayMessage,
72+
line: 1,
73+
column: 8
74+
}
75+
]
76+
},
77+
{
78+
code: "let a: ({ foo: Array<Bar> })[] = []",
79+
options: ["array"],
80+
errors: [
81+
{
82+
message: arrayMessage,
83+
line: 1,
84+
column: 8
85+
}
86+
]
87+
},
88+
{
89+
code: "let a: string[] = []",
90+
options: ["generic"],
91+
errors: [
92+
{
93+
message: genericMessage,
94+
line: 1,
95+
column: 8
96+
}
97+
]
98+
},
99+
{
100+
code: "let a: (string | number)[] = []",
101+
options: ["generic"],
102+
errors: [
103+
{
104+
message: genericMessage,
105+
line: 1,
106+
column: 8
107+
}
108+
]
109+
},
110+
{
111+
code: "let a: Array<{ foo: Bar[] }> = []",
112+
options: ["generic"],
113+
errors: [
114+
{
115+
message: genericMessage,
116+
line: 1,
117+
column: 8
118+
}
119+
]
120+
}
121+
]
122+
});

0 commit comments

Comments
 (0)