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

Make template token objects adhere token object structure #343

Merged
merged 3 commits into from Jul 10, 2017

Conversation

iancmyers
Copy link
Contributor

While fixing an issue in eslint-plugin-react (jsx-eslint/eslint-plugin-react#1061), I noticed that template tokens differ from other tokens. A typical token looks like this:

Token {
  type: 'String',
  value: '"bar"',
  start: 44,
  end: 49,
  loc:
   SourceLocation {
     start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }

A template token looks like this:

{ type: 'Template',
  value: '`bar`',
  loc:
   { start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }

I've not be able to figure out why templates are plain JavaScript objects and not of the class type Token (aside from the fact that the template tokens are constructed differently in the token translator.

This fix copies the range values into start and end properties on the template tokens to make them adhere to the same structure as other token objects.

Fixes #319

@jsf-clabot
Copy link

jsf-clabot commented Jun 19, 2017

CLA assistant check
All committers have signed the CLA.

Copy link
Sponsor Contributor

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Any chance of a regression test? :-D

@@ -58,6 +58,8 @@ function convertTemplatePart(tokens, code) {

if (firstToken.range) {
token.range = [firstToken.range[0], lastTemplateToken.range[1]];
token.start = firstToken.range[0];
token.end = lastTemplateToken.range[1];
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

nbd but maybe token.range = [token.start, token.end] after these lines would reduce some of the duplication?

@iancmyers
Copy link
Contributor Author

@ljharb I'd be happy to add a test, but I'd need to be pointed in the correct direction.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jun 20, 2017

I think perhaps the tokenize fixtures might need start and end added alongside range and loc, but I'm not 100% confident :-/

@iancmyers
Copy link
Contributor Author

@ljharb Looks like the tester explicitly strips out start and end and the root level of the token object: https://github.com/eslint/espree/blob/master/tests/lib/tester.js#L28

While fixing an issue in `eslint-plugin-react`
(jsx-eslint/eslint-plugin-react#1061),
I noticed that template tokens differ from other tokens. A typical token
looks like this:

```
Token {
  type: 'String',
  value: '"bar"',
  start: 44,
  end: 49,
  loc:
   SourceLocation {
     start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

A template token looks like this:

```
{ type: 'Template',
  value: '`bar`',
  loc:
   { start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

I've not be able to figure out why templates are plain JavaScript
objects and not of the class type `Token` (aside from the fact that the
template tokens are constructed differently in the token translator.

This fix copies the `range` values into `start` and `end` properties on
the template tokens to make them adhere to the same structure as other
token objects.

Fixes eslint#319
While fixing an issue in `eslint-plugin-react`
(jsx-eslint/eslint-plugin-react#1061),
I noticed that template tokens differ from other tokens. A typical token
looks like this:

```
Token {
  type: 'String',
  value: '"bar"',
  start: 44,
  end: 49,
  loc:
   SourceLocation {
     start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

A template token looks like this:

```
{ type: 'Template',
  value: '`bar`',
  loc:
   { start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

I've not be able to figure out why templates are plain JavaScript
objects and not of the class type `Token` (aside from the fact that the
template tokens are constructed differently in the token translator.

This fix copies the `range` values into `start` and `end` properties on
the template tokens to make them adhere to the same structure as other
token objects.

Fixes eslint#319
@kaicataldo
Copy link
Member

If we're not testing these values anywhere else in our tests it seems to me like we shouldn't hold this change up for that. If we decide to merge this as is, I can make a follow up issue to see if we can make this testable somehow.

As @nzakas mentioned in the original response, this seems like a pretty clear bug and an easy, low risk fix.

@eslint/eslint-team Does anyone know why the tester does this?

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jun 29, 2017

@kaicataldo totally; my comment wasn't meant as a blocker, more killing time/seeing if the PR can be improved while waiting for a response from eslint core ;-) it'd be great to merge it straightaway!

@not-an-aardvark not-an-aardvark merged commit 3805aea into eslint:master Jul 10, 2017
@not-an-aardvark
Copy link
Member

Thanks for contributing!

not-an-aardvark pushed a commit that referenced this pull request Jul 10, 2017
While fixing an issue in `eslint-plugin-react`
(jsx-eslint/eslint-plugin-react#1061),
I noticed that template tokens differ from other tokens. A typical token
looks like this:

```
Token {
  type: 'String',
  value: '"bar"',
  start: 44,
  end: 49,
  loc:
   SourceLocation {
     start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

A template token looks like this:

```
{ type: 'Template',
  value: '`bar`',
  loc:
   { start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

I've not be able to figure out why templates are plain JavaScript
objects and not of the class type `Token` (aside from the fact that the
template tokens are constructed differently in the token translator.

This fix copies the `range` values into `start` and `end` properties on
the template tokens to make them adhere to the same structure as other
token objects.

Fixes #319
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template token objects differ from other token objects
5 participants