Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add extension rule
init-declarations
(#1814)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
- Loading branch information
1 parent
cea51bf
commit b01f5e7
Showing
7 changed files
with
826 additions
and
0 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,22 @@ | ||
# require or disallow initialization in variable declarations (`init-declarations`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/init-declarations`](https://eslint.org/docs/rules/init-declarations) rule. | ||
It adds support for TypeScript's `declare` variables. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"init-declarations": "off", | ||
"@typescript-eslint/init-declarations": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/init-declarations` options](https://eslint.org/docs/rules/init-declarations#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/init-declarations.md)</sup> |
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
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,53 @@ | ||
import { | ||
TSESTree, | ||
AST_NODE_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/init-declarations'; | ||
import { | ||
InferOptionsTypeFromRule, | ||
InferMessageIdsTypeFromRule, | ||
createRule, | ||
} from '../util'; | ||
|
||
export type Options = InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'init-declarations', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'require or disallow initialization in variable declarations', | ||
category: 'Variables', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: ['always'], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
const mode = context.options[0] || 'always'; | ||
|
||
return { | ||
'VariableDeclaration:exit'(node: TSESTree.VariableDeclaration): void { | ||
if (mode === 'always') { | ||
if (node.declare) { | ||
return; | ||
} | ||
if ( | ||
node.parent?.type === AST_NODE_TYPES.TSModuleBlock && | ||
node.parent.parent?.type === AST_NODE_TYPES.TSModuleDeclaration && | ||
node.parent.parent?.declare | ||
) { | ||
return; | ||
} | ||
} | ||
|
||
rules['VariableDeclaration:exit'](node); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.