diff --git a/packages/dtslint/dtslint.json b/packages/dtslint/dtslint.json index 3bd1e69c1f..e49c6c83f4 100644 --- a/packages/dtslint/dtslint.json +++ b/packages/dtslint/dtslint.json @@ -10,7 +10,6 @@ "no-relative-import-in-test": true, "strict-export-declare-modifiers": true, "no-single-declare-module": true, - "prefer-declare-function": true, "unified-signatures": true, "void-return": true, "npm-naming": 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..11d1b2c009 --- /dev/null +++ b/packages/dtslint/src/rules/prefer-declare-function.ts @@ -0,0 +1,37 @@ +import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"; + +import { createRule } from "../util"; + +const rule = createRule({ + defaultOptions: [], + meta: { + docs: { + description: "Forbids `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 + "VariableDeclaration > VariableDeclarator"(node: TSESTree.VariableDeclarator) { + if ( + node.id.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType && + context.getFilename().endsWith(".d.ts") + ) { + 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..ff52afeabe --- /dev/null +++ b/packages/dtslint/test/prefer-declare-function.test.ts @@ -0,0 +1,66 @@ +import { ESLintUtils } from "@typescript-eslint/utils"; + +import * as preferDeclareFunction from "../src/rules/prefer-declare-function"; + +const ruleTester = new ESLintUtils.RuleTester({ + parser: "@typescript-eslint/parser", +}); + +ruleTester.run("prefer-declare-function", preferDeclareFunction, { + invalid: [ + { + filename: "index.d.ts", + code: `export const example: () => void;`, + errors: [ + { + column: 14, + endColumn: 33, + line: 1, + messageId: "variableFunction", + }, + ], + }, + { + filename: "index.d.ts", + code: ` + namespace N { + export const example: () => void; + } +`, + errors: [ + { + column: 26, + endColumn: 45, + line: 3, + messageId: "variableFunction", + }, + ], + }, + { + filename: "index.d.ts", + code: ` + namespace N { + const example: () => void; + } +`, + errors: [ + { + column: 19, + endColumn: 38, + line: 3, + messageId: "variableFunction", + }, + ], + }, + ], + valid: [ + { + filename: "index.d.ts", + code: `function example(): void`, + }, + { + filename: "test.ts", + code: `export const 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 - } -}