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
sandersn
merged 5 commits into
microsoft:master
from
JoshuaKGoldberg:tseslint-prefer-declare-function
Dec 30, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21b2569
Converted prefer-declare-function from TSLint to ESLint
JoshuaKGoldberg f9131cd
Rule applies to all variable declarations, not just exported ones
sandersn be15258
Merge branch 'master' into tseslint-prefer-declare-function
sandersn e0ccf6f
Merge branch 'master' into tseslint-prefer-declare-function
sandersn 7e2587f
Check non-exported consts too, but only in .d.ts files
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -20,4 +20,3 @@ ruleTester.run("no-any-union", noAnyUnion, { | |
], | ||
valid: [`export const x: any;`], | ||
}); | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theexport