Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted prefer-declare-function from TSLint to ESLint #538

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/dtslint/dtslint.json
Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type doesn't need to be exported to be an error.

if (node.id.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType) {
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.

39 changes: 39 additions & 0 deletions 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also needs to forbid const example: () => void, without the export

}
`,
errors: [
{
column: 26,
endColumn: 45,
line: 3,
messageId: "variableFunction",
},
],
},
],
valid: [`function 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.