From b20a7d130d640ef66417a7ff13e486d77125e20c Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 21 Jun 2019 09:26:33 -0700 Subject: [PATCH] fix(eslint-plugin): handle `const;` 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 d8d9bb675a21..eea12b9cf845 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 14088c553789..70cf1c37919a 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 6b2a83066dd4..321f32f4cceb 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 19551a911664..89f6c5403032 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 e992b440c6cc..9fb9ef7dadd6 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 e786e83e0dfb..8eed48390d0f 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; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy