Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted prefer-declare-function from TSLint to ESLint (#538)
* 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
1 parent
b5b74f4
commit b3b8393
Showing
6 changed files
with
103 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
10
packages/dtslint/test/prefer-declare-function/test.d.ts.lint
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.