From 430d628b0271718e2df470831832fa40b41150be Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 28 Jun 2019 08:32:19 -0700 Subject: [PATCH] fix(eslint-plugin): handle `const;` (#633) Fixes #441 --- .../src/rules/consistent-type-definitions.ts | 1 - .../src/rules/indent-new-do-not-use/index.ts | 4 ++++ packages/eslint-plugin/src/rules/indent.ts | 9 +++++++++ packages/eslint-plugin/src/rules/prefer-for-of.ts | 5 ++++- packages/eslint-plugin/tests/rules/indent/indent.test.ts | 2 ++ packages/typescript-estree/src/ts-estree/ts-estree.ts | 1 + 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts index d8d9bb675a2..eea12b9cf84 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-definitions.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-definitions.ts @@ -27,7 +27,6 @@ export default util.createRule({ const sourceCode = context.getSourceCode(); return { - // VariableDeclaration with kind type has only one VariableDeclarator "TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"( node: TSESTree.TSTypeAliasDeclaration, ) { diff --git a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts index 14088c55378..70cf1c37919 100644 --- a/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts +++ b/packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts @@ -1382,6 +1382,10 @@ export default createRule({ }, VariableDeclaration(node) { + if (node.declarations.length === 0) { + return; + } + let variableIndent = Object.prototype.hasOwnProperty.call( options.VariableDeclarator, node.kind, diff --git a/packages/eslint-plugin/src/rules/indent.ts b/packages/eslint-plugin/src/rules/indent.ts index 6b2a83066dd..321f32f4cce 100644 --- a/packages/eslint-plugin/src/rules/indent.ts +++ b/packages/eslint-plugin/src/rules/indent.ts @@ -175,6 +175,15 @@ export default util.createRule({ } }, + VariableDeclaration(node: TSESTree.VariableDeclaration) { + // https://github.com/typescript-eslint/typescript-eslint/issues/441 + if (node.declarations.length === 0) { + return; + } + + return rules.VariableDeclaration(node); + }, + TSAsExpression(node: TSESTree.TSAsExpression) { // transform it to a BinaryExpression return rules['BinaryExpression, LogicalExpression']({ diff --git a/packages/eslint-plugin/src/rules/prefer-for-of.ts b/packages/eslint-plugin/src/rules/prefer-for-of.ts index 19551a91166..89f6c540303 100644 --- a/packages/eslint-plugin/src/rules/prefer-for-of.ts +++ b/packages/eslint-plugin/src/rules/prefer-for-of.ts @@ -186,8 +186,11 @@ export default util.createRule({ return; } - const [declarator] = node.init.declarations; + const declarator = node.init.declarations[0] as + | TSESTree.VariableDeclarator + | undefined; if ( + !declarator || !isZeroInitialized(declarator) || declarator.id.type !== AST_NODE_TYPES.Identifier ) { diff --git a/packages/eslint-plugin/tests/rules/indent/indent.test.ts b/packages/eslint-plugin/tests/rules/indent/indent.test.ts index e992b440c6c..9fb9ef7dadd 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent.test.ts @@ -768,6 +768,8 @@ const div: JQuery = $('
') `, options: [2, { VariableDeclarator: { const: 3 } }], }, + // https://github.com/typescript-eslint/typescript-eslint/issues/441 + `const;`, ], invalid: [ ...individualNodeTests.invalid, diff --git a/packages/typescript-estree/src/ts-estree/ts-estree.ts b/packages/typescript-estree/src/ts-estree/ts-estree.ts index e786e83e0df..8eed48390d0 100644 --- a/packages/typescript-estree/src/ts-estree/ts-estree.ts +++ b/packages/typescript-estree/src/ts-estree/ts-estree.ts @@ -1387,6 +1387,7 @@ export interface UnaryExpression extends UnaryExpressionBase { export interface VariableDeclaration extends BaseNode { type: AST_NODE_TYPES.VariableDeclaration; + // NOTE - this is not guaranteed to have any elements in it. i.e. `const;` declarations: VariableDeclarator[]; kind: 'let' | 'const' | 'var'; declare?: boolean;