From 21b2569aedcb117f76d107574616fad4674f7e68 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 26 Sep 2022 12:44:27 -0400 Subject: [PATCH 1/3] Converted prefer-declare-function from TSLint to ESLint --- packages/dtslint/dtslint.json | 1 - .../src/rules/prefer-declare-function.ts | 34 ++++++++++++++++ .../src/rules/preferDeclareFunctionRule.ts | 36 ----------------- .../test/prefer-declare-function.test.ts | 39 +++++++++++++++++++ .../prefer-declare-function/test.d.ts.lint | 10 ----- .../test/prefer-declare-function/tslint.json | 6 --- 6 files changed, 73 insertions(+), 53 deletions(-) create mode 100644 packages/dtslint/src/rules/prefer-declare-function.ts delete mode 100644 packages/dtslint/src/rules/preferDeclareFunctionRule.ts create mode 100644 packages/dtslint/test/prefer-declare-function.test.ts delete mode 100644 packages/dtslint/test/prefer-declare-function/test.d.ts.lint delete mode 100644 packages/dtslint/test/prefer-declare-function/tslint.json diff --git a/packages/dtslint/dtslint.json b/packages/dtslint/dtslint.json index a7eeeef2a6..9893c9e8af 100644 --- a/packages/dtslint/dtslint.json +++ b/packages/dtslint/dtslint.json @@ -12,7 +12,6 @@ "no-any-union": true, "no-single-declare-module": true, "no-unnecessary-generics": true, - "prefer-declare-function": true, "trim-file": true, "unified-signatures": true, "void-return": true, diff --git a/packages/dtslint/src/rules/prefer-declare-function.ts b/packages/dtslint/src/rules/prefer-declare-function.ts new file mode 100644 index 0000000000..fd6d54a8ab --- /dev/null +++ b/packages/dtslint/src/rules/prefer-declare-function.ts @@ -0,0 +1,34 @@ +import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"; + +import { createRule } from "../util"; + +const rule = createRule({ + defaultOptions: [], + meta: { + docs: { + description: "Forbids `export const x = () => void`.", + recommended: "error", + }, + messages: { + variableFunction: "Use a function declaration instead of a variable of function type.", + }, + schema: [], + type: "problem", + }, + name: "prefer-declare-function", + create(context) { + return { + // eslint-disable-next-line @typescript-eslint/naming-convention + "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator"(node: TSESTree.VariableDeclarator) { + if (node.id.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType) { + context.report({ + messageId: "variableFunction", + node: node.id, + }); + } + }, + }; + }, +}); + +export = rule; diff --git a/packages/dtslint/src/rules/preferDeclareFunctionRule.ts b/packages/dtslint/src/rules/preferDeclareFunctionRule.ts deleted file mode 100644 index b6bcb5c9d9..0000000000 --- a/packages/dtslint/src/rules/preferDeclareFunctionRule.ts +++ /dev/null @@ -1,36 +0,0 @@ -import * as Lint from "tslint"; -import * as ts from "typescript"; - -import { eachModuleStatement, failure } from "../util"; - -export class Rule extends Lint.Rules.AbstractRule { - static metadata: Lint.IRuleMetadata = { - ruleName: "prefer-declare-function", - description: "Forbids `export const x = () => void`.", - optionsDescription: "Not configurable.", - options: null, - type: "style", - typescriptOnly: true, - }; - - static FAILURE_STRING = failure( - Rule.metadata.ruleName, - "Use a function declaration instead of a variable of function type." - ); - - apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, walk); - } -} - -function walk(ctx: Lint.WalkContext): void { - eachModuleStatement(ctx.sourceFile, (statement) => { - if (ts.isVariableStatement(statement)) { - for (const varDecl of statement.declarationList.declarations) { - if (varDecl.type !== undefined && varDecl.type.kind === ts.SyntaxKind.FunctionType) { - ctx.addFailureAtNode(varDecl, Rule.FAILURE_STRING); - } - } - } - }); -} diff --git a/packages/dtslint/test/prefer-declare-function.test.ts b/packages/dtslint/test/prefer-declare-function.test.ts new file mode 100644 index 0000000000..1e236c6c8d --- /dev/null +++ b/packages/dtslint/test/prefer-declare-function.test.ts @@ -0,0 +1,39 @@ +import { ESLintUtils } from "@typescript-eslint/utils"; + +import * as noConstEnum from "../src/rules/prefer-declare-function"; + +const ruleTester = new ESLintUtils.RuleTester({ + parser: "@typescript-eslint/parser", +}); + +ruleTester.run("prefer-declare-function", noConstEnum, { + invalid: [ + { + code: `export const example: () => void;`, + errors: [ + { + column: 14, + endColumn: 33, + line: 1, + messageId: "variableFunction", + }, + ], + }, + { + code: ` + namespace N { + export const example: () => void; + } +`, + errors: [ + { + column: 26, + endColumn: 45, + line: 3, + messageId: "variableFunction", + }, + ], + }, + ], + valid: [`function example(): void`], +}); diff --git a/packages/dtslint/test/prefer-declare-function/test.d.ts.lint b/packages/dtslint/test/prefer-declare-function/test.d.ts.lint deleted file mode 100644 index 85f9c61011..0000000000 --- a/packages/dtslint/test/prefer-declare-function/test.d.ts.lint +++ /dev/null @@ -1,10 +0,0 @@ -export const x: () => void; - ~~~~~~~~~~~~~ [0] - -namespace N { - const x: () => void; - ~~~~~~~~~~~~~ [0] -} - - -[0]: Use a function declaration instead of a variable of function type. See: https://github.com/microsoft/DefinitelyTyped-tools/blob/master/packages/dtslint/docs/prefer-declare-function.md diff --git a/packages/dtslint/test/prefer-declare-function/tslint.json b/packages/dtslint/test/prefer-declare-function/tslint.json deleted file mode 100644 index 06473197a7..0000000000 --- a/packages/dtslint/test/prefer-declare-function/tslint.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "rulesDirectory": ["../../dist/rules"], - "rules": { - "prefer-declare-function": true - } -} From f9131cd2abf4aca65c4714b40e4e4332a2942b50 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Wed, 28 Dec 2022 11:17:04 -0800 Subject: [PATCH 2/3] Rule applies to all variable declarations, not just exported ones --- .../dtslint/src/rules/prefer-declare-function.ts | 2 +- .../dtslint/test/prefer-declare-function.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/dtslint/src/rules/prefer-declare-function.ts b/packages/dtslint/src/rules/prefer-declare-function.ts index fd6d54a8ab..5f356ceba9 100644 --- a/packages/dtslint/src/rules/prefer-declare-function.ts +++ b/packages/dtslint/src/rules/prefer-declare-function.ts @@ -19,7 +19,7 @@ const rule = createRule({ create(context) { return { // eslint-disable-next-line @typescript-eslint/naming-convention - "ExportNamedDeclaration > VariableDeclaration > VariableDeclarator"(node: TSESTree.VariableDeclarator) { + "VariableDeclaration > VariableDeclarator"(node: TSESTree.VariableDeclarator) { if (node.id.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType) { context.report({ messageId: "variableFunction", diff --git a/packages/dtslint/test/prefer-declare-function.test.ts b/packages/dtslint/test/prefer-declare-function.test.ts index 1e236c6c8d..a96ecd1985 100644 --- a/packages/dtslint/test/prefer-declare-function.test.ts +++ b/packages/dtslint/test/prefer-declare-function.test.ts @@ -34,6 +34,21 @@ ruleTester.run("prefer-declare-function", noConstEnum, { }, ], }, + { + code: ` + namespace N { + const example: () => void; + } +`, + errors: [ + { + column: 19, + endColumn: 38, + line: 3, + messageId: "variableFunction", + }, + ], + }, ], valid: [`function example(): void`], }); From 7e2587f348b2dda533586f054ae027a0c64ac7d9 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 29 Dec 2022 15:11:59 -0800 Subject: [PATCH 3/3] Check non-exported consts too, but only in .d.ts files --- .../src/rules/prefer-declare-function.ts | 7 +++++-- packages/dtslint/test/no-any-union.test.ts | 1 - .../test/prefer-declare-function.test.ts | 18 +++++++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/dtslint/src/rules/prefer-declare-function.ts b/packages/dtslint/src/rules/prefer-declare-function.ts index 5f356ceba9..11d1b2c009 100644 --- a/packages/dtslint/src/rules/prefer-declare-function.ts +++ b/packages/dtslint/src/rules/prefer-declare-function.ts @@ -6,7 +6,7 @@ const rule = createRule({ defaultOptions: [], meta: { docs: { - description: "Forbids `export const x = () => void`.", + description: "Forbids `const x: () => void`.", recommended: "error", }, messages: { @@ -20,7 +20,10 @@ const rule = createRule({ return { // eslint-disable-next-line @typescript-eslint/naming-convention "VariableDeclaration > VariableDeclarator"(node: TSESTree.VariableDeclarator) { - if (node.id.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType) { + if ( + node.id.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType && + context.getFilename().endsWith(".d.ts") + ) { context.report({ messageId: "variableFunction", node: node.id, diff --git a/packages/dtslint/test/no-any-union.test.ts b/packages/dtslint/test/no-any-union.test.ts index 96dac92a72..31d512948f 100644 --- a/packages/dtslint/test/no-any-union.test.ts +++ b/packages/dtslint/test/no-any-union.test.ts @@ -20,4 +20,3 @@ ruleTester.run("no-any-union", noAnyUnion, { ], valid: [`export const x: any;`], }); - diff --git a/packages/dtslint/test/prefer-declare-function.test.ts b/packages/dtslint/test/prefer-declare-function.test.ts index a96ecd1985..ff52afeabe 100644 --- a/packages/dtslint/test/prefer-declare-function.test.ts +++ b/packages/dtslint/test/prefer-declare-function.test.ts @@ -1,14 +1,15 @@ import { ESLintUtils } from "@typescript-eslint/utils"; -import * as noConstEnum from "../src/rules/prefer-declare-function"; +import * as preferDeclareFunction from "../src/rules/prefer-declare-function"; const ruleTester = new ESLintUtils.RuleTester({ parser: "@typescript-eslint/parser", }); -ruleTester.run("prefer-declare-function", noConstEnum, { +ruleTester.run("prefer-declare-function", preferDeclareFunction, { invalid: [ { + filename: "index.d.ts", code: `export const example: () => void;`, errors: [ { @@ -20,6 +21,7 @@ ruleTester.run("prefer-declare-function", noConstEnum, { ], }, { + filename: "index.d.ts", code: ` namespace N { export const example: () => void; @@ -35,6 +37,7 @@ ruleTester.run("prefer-declare-function", noConstEnum, { ], }, { + filename: "index.d.ts", code: ` namespace N { const example: () => void; @@ -50,5 +53,14 @@ ruleTester.run("prefer-declare-function", noConstEnum, { ], }, ], - valid: [`function example(): void`], + valid: [ + { + filename: "index.d.ts", + code: `function example(): void`, + }, + { + filename: "test.ts", + code: `export const example: () => void;`, + }, + ], });