Skip to content

Commit

Permalink
fix(eslint-plugin): handle const; (#633)
Browse files Browse the repository at this point in the history
Fixes #441
  • Loading branch information
bradzacher committed Jun 28, 2019
1 parent e325b72 commit 430d628
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
Expand Up @@ -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,
) {
Expand Down
Expand Up @@ -1382,6 +1382,10 @@ export default createRule<Options, MessageIds>({
},

VariableDeclaration(node) {
if (node.declarations.length === 0) {
return;
}

let variableIndent = Object.prototype.hasOwnProperty.call(
options.VariableDeclarator,
node.kind,
Expand Down
9 changes: 9 additions & 0 deletions packages/eslint-plugin/src/rules/indent.ts
Expand Up @@ -175,6 +175,15 @@ export default util.createRule<Options, MessageIds>({
}
},

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']({
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-for-of.ts
Expand Up @@ -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
) {
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/tests/rules/indent/indent.test.ts
Expand Up @@ -768,6 +768,8 @@ const div: JQuery<HTMLElement> = $('<div>')
`,
options: [2, { VariableDeclarator: { const: 3 } }],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/441
`const;`,
],
invalid: [
...individualNodeTests.invalid,
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 430d628

Please sign in to comment.