Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): handle const; #633

Merged
merged 2 commits into from Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏👏👏
I faced the exact same issue and debugging the code I was right here to add this! Thanks for being on top of it. <3 @bradzacher

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