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: Support type annotations in array-bracket-spacing #7445

Merged

Conversation

taion
Copy link
Contributor

@taion taion commented Oct 24, 2016

What is the purpose of this pull request? (put an "X" next to item)

[X] Bug fix (template)

Original issue: #4753

Per comma-dangle, object-curly-spacing, and #7436, it appears that these updates can belong in ESLint now.

What changes did you make? (Give an overview)
In array-bracket-spacing, if a type annotation is present, take the last token and the penultimate token before the type annotation.

Is there anything you'd like reviewers to focus on?
This code is slightly different from the code in eslint-plugin-babel. The code there just steps backward until it finds a ]. Here I instead look at the token immediately before the beginning of the type annotation. This avoids issues when the type annotation is of the form Type[].

cc @zaygraveyard

@eslintbot
Copy link

LGTM

@mention-bot
Copy link

@taion, thanks for your PR! By analyzing the history of the files in this pull request, we identified @nzakas, @alberto and @mysticatea to be potential reviewers.

@vitorbal
Copy link
Member

Thank you for the PR, @taion! Could you fill the bug fix/report template too?

@vitorbal vitorbal added bug ESLint is working incorrectly rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Oct 26, 2016
@taion
Copy link
Contributor Author

taion commented Oct 26, 2016

@vitorbal It's in the linked issue: #4753

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

Thank you for contributing!

But I'm not sure that those tests are legal.
Could you confirm the language spec?


// destructuring with type annotation
{ code: "([ 1, 1 ]: Array<any>)", options: ["always"], parserOptions: { ecmaVersion: 6 }, parser: parser("flow-destructuring-1") },
{ code: "([1, 1]: Array< any >)", options: ["never"], parserOptions: { ecmaVersion: 6 }, parser: parser("flow-destructuring-2") },
Copy link
Member

Choose a reason for hiding this comment

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

Is this valid syntax?
As far as I know, the array patterns of destructuring cannot exist independently.

  • It needs the initializer of variable declarations: e.g. let [a] = b
  • It needs to be at LHS of assignments: e.g. ([a] = b)
  • It needs to be in parameters: e.g. ([a]) => a

Also, those AST looks ArrayExpression, not ArrayPattern. Those are not destructuring.

Copy link
Member

Choose a reason for hiding this comment

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

I found it, those tests are Typecasts syntax.
Those ArrayExpression nodes don't have typeAnnotations, so those tests are not proper.

@taion
Copy link
Contributor Author

taion commented Oct 27, 2016

I was overzealous in cutting down the test case. Will replace.

@eslintbot
Copy link

LGTM

@taion
Copy link
Contributor Author

taion commented Oct 27, 2016

Updated. The test should be correct now.

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

Thank you!
Tests look good to me.

penultimate = sourceCode.getLastToken(node, 1),
last = sourceCode.getLastToken(node),
last = node.typeAnnotation
? sourceCode.getFirstToken(node.typeAnnotation, -1)
Copy link
Member

Choose a reason for hiding this comment

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

Could you use sourceCode.getTokenBefore(node.typeAnnotation) instead?

@eslintbot
Copy link

LGTM

@taion
Copy link
Contributor Author

taion commented Oct 27, 2016

Switched to getTokenBefore.

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@mysticatea mysticatea added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Oct 27, 2016
penultimate = sourceCode.getLastToken(node, 1),
last = sourceCode.getLastToken(node),
last = node.typeAnnotation
? sourceCode.getTokenBefore(node.typeAnnotation)
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering, can this be made into a sourceCode.getTokenAfter(/* last element */) and then work for both cases? But this looks perfectly fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to do that – the problem is that node.elements is an empty array if there are no elements, so there may not be a last element. It turns out that

([]: any) => {}

is actually legal. Deeply silly, yes, but actually a thing.

Copy link
Member

Choose a reason for hiding this comment

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

I guess sourceCode.getTokenAfter(/* last element */) does not work if there is a trailing comma, or if no item (e.g. let [] = a;).

Copy link
Member

Choose a reason for hiding this comment

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

Good points. Let's leave it as is.

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for contributing!

@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants