Skip to content

Commit

Permalink
Converted prefer-declare-function from TSLint to ESLint (#538)
Browse files Browse the repository at this point in the history
* Converted prefer-declare-function from TSLint to ESLint

* Rule applies to all variable declarations, not just exported ones

* Check non-exported consts too, but only in .d.ts files

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
  • Loading branch information
JoshuaKGoldberg and sandersn committed Dec 30, 2022
1 parent b5b74f4 commit b3b8393
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 53 deletions.
1 change: 0 additions & 1 deletion packages/dtslint/dtslint.json
Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions 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;
36 changes: 0 additions & 36 deletions packages/dtslint/src/rules/preferDeclareFunctionRule.ts

This file was deleted.

66 changes: 66 additions & 0 deletions 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;`,
},
],
});
10 changes: 0 additions & 10 deletions packages/dtslint/test/prefer-declare-function/test.d.ts.lint

This file was deleted.

6 changes: 0 additions & 6 deletions packages/dtslint/test/prefer-declare-function/tslint.json

This file was deleted.

0 comments on commit b3b8393

Please sign in to comment.