diff --git a/docs/rules/array-type.md b/docs/rules/array-type.md new file mode 100644 index 0000000..e69de29 diff --git a/lib/rules/array-type.js b/lib/rules/array-type.js new file mode 100644 index 0000000..e4fcb20 --- /dev/null +++ b/lib/rules/array-type.js @@ -0,0 +1,58 @@ +/** + * @fileoverview Requires using either 'T[]' or 'Array' for arrays. + * @author Mackie Underdown + */ +"use strict"; + +const util = require("../util"); + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = { + meta: { + docs: { + description: "Requires using either 'T[]' or 'Array' for arrays", + extraDescription: [util.tslintRule("array-type")], + category: "TypeScript", + url: + "https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/array-type.md" + }, + schema: [ + { + enum: ["array", "generic", "array-simple"] + } + ] + }, + create(context) { + const arrayType = context.options[0] || "array"; + //---------------------------------------------------------------------- + // Public + //---------------------------------------------------------------------- + + return { + TypeAnnotation: console.log, // eslint-disable-line no-console + TSArrayType(node) { + console.log(node); // eslint-disable-line no-console + if (arrayType === "generic") { + context.report({ + node, + message: + "Array type using 'T[]' is forbidden. Use 'Array' instead." + }); + } + }, + TSTypeReference(node) { + console.log(node); // eslint-disable-line no-console + if (node.typeName.name === "Array" && arrayType === "array") { + context.report({ + node, + message: + "Array type using 'Array' is forbidden. Use 'T[]' instead." + }); + } + } + }; + } +}; diff --git a/tests/lib/rules/array-type.js b/tests/lib/rules/array-type.js new file mode 100644 index 0000000..f95adb9 --- /dev/null +++ b/tests/lib/rules/array-type.js @@ -0,0 +1,122 @@ +/** + * @fileoverview Requires using either 'T[]' or 'Array' for arrays. + * @author Mackie Underdown + */ +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const rule = require("../../../lib/rules/array-type"), + RuleTester = require("eslint").RuleTester; + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +const arrayMessage = + "Array type using 'Array' is forbidden. Use 'T[]' instead."; +const genericMessage = + "Array type using 'T[]' is forbidden. Use 'Array' instead."; + +const ruleTester = new RuleTester({ + parser: "typescript-eslint-parser" +}); + +ruleTester.run("array-type", rule, { + valid: [ + { + code: "let a: string[] = []", + options: ["array"] + }, + { + code: "let a: (string | number)[] = []", + options: ["array"] + }, + { + code: "let a: ({ foo: Bar[] })[] = []", + options: ["array"] + }, + { + code: "let a: Array = []", + options: ["generic"] + }, + { + code: "let a: Array = []", + options: ["generic"] + }, + { + code: "let a: Array<{ foo: Array }> = []", + options: ["generic"] + } + ], + invalid: [ + { + code: "let a: Array = []", + options: ["array"], + errors: [ + { + message: arrayMessage, + line: 1, + column: 8 + } + ] + }, + { + code: "let a: Array = []", + options: ["array"], + errors: [ + { + message: arrayMessage, + line: 1, + column: 8 + } + ] + }, + { + code: "let a: ({ foo: Array })[] = []", + options: ["array"], + errors: [ + { + message: arrayMessage, + line: 1, + column: 8 + } + ] + }, + { + code: "let a: string[] = []", + options: ["generic"], + errors: [ + { + message: genericMessage, + line: 1, + column: 8 + } + ] + }, + { + code: "let a: (string | number)[] = []", + options: ["generic"], + errors: [ + { + message: genericMessage, + line: 1, + column: 8 + } + ] + }, + { + code: "let a: Array<{ foo: Bar[] }> = []", + options: ["generic"], + errors: [ + { + message: genericMessage, + line: 1, + column: 8 + } + ] + } + ] +});