Skip to content

Commit 430d628

Browse files
authored
fix(eslint-plugin): handle const; (#633)
Fixes #441
1 parent e325b72 commit 430d628

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

packages/eslint-plugin/src/rules/consistent-type-definitions.ts

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default util.createRule({
2727
const sourceCode = context.getSourceCode();
2828

2929
return {
30-
// VariableDeclaration with kind type has only one VariableDeclarator
3130
"TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"(
3231
node: TSESTree.TSTypeAliasDeclaration,
3332
) {

packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,10 @@ export default createRule<Options, MessageIds>({
13821382
},
13831383

13841384
VariableDeclaration(node) {
1385+
if (node.declarations.length === 0) {
1386+
return;
1387+
}
1388+
13851389
let variableIndent = Object.prototype.hasOwnProperty.call(
13861390
options.VariableDeclarator,
13871391
node.kind,

packages/eslint-plugin/src/rules/indent.ts

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ export default util.createRule<Options, MessageIds>({
175175
}
176176
},
177177

178+
VariableDeclaration(node: TSESTree.VariableDeclaration) {
179+
// https://github.com/typescript-eslint/typescript-eslint/issues/441
180+
if (node.declarations.length === 0) {
181+
return;
182+
}
183+
184+
return rules.VariableDeclaration(node);
185+
},
186+
178187
TSAsExpression(node: TSESTree.TSAsExpression) {
179188
// transform it to a BinaryExpression
180189
return rules['BinaryExpression, LogicalExpression']({

packages/eslint-plugin/src/rules/prefer-for-of.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ export default util.createRule({
186186
return;
187187
}
188188

189-
const [declarator] = node.init.declarations;
189+
const declarator = node.init.declarations[0] as
190+
| TSESTree.VariableDeclarator
191+
| undefined;
190192
if (
193+
!declarator ||
191194
!isZeroInitialized(declarator) ||
192195
declarator.id.type !== AST_NODE_TYPES.Identifier
193196
) {

packages/eslint-plugin/tests/rules/indent/indent.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ const div: JQuery<HTMLElement> = $('<div>')
768768
`,
769769
options: [2, { VariableDeclarator: { const: 3 } }],
770770
},
771+
// https://github.com/typescript-eslint/typescript-eslint/issues/441
772+
`const;`,
771773
],
772774
invalid: [
773775
...individualNodeTests.invalid,

packages/typescript-estree/src/ts-estree/ts-estree.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ export interface UnaryExpression extends UnaryExpressionBase {
13871387

13881388
export interface VariableDeclaration extends BaseNode {
13891389
type: AST_NODE_TYPES.VariableDeclaration;
1390+
// NOTE - this is not guaranteed to have any elements in it. i.e. `const;`
13901391
declarations: VariableDeclarator[];
13911392
kind: 'let' | 'const' | 'var';
13921393
declare?: boolean;

0 commit comments

Comments
 (0)