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

Object literal shorthand generator methods have incorrect range #36

Closed
btmills opened this issue Jan 19, 2015 · 5 comments
Closed

Object literal shorthand generator methods have incorrect range #36

btmills opened this issue Jan 19, 2015 · 5 comments
Labels

Comments

@btmills
Copy link
Member

btmills commented Jan 19, 2015

Perhaps this is intentional, but the range given to object literal shorthand generator methods includes only the method body {}. Since they key is part of the parent property and overlapping, non-ancestral ranges might get confusing, I'm not sure that the FunctionExpression's range should include all of *foo(arg1) {}, but it seems like the range should expand to at least (arg1) {}.

Related: eslint/eslint#1677

//test.js
var inspect = require('util').inspect,
    espree = require('espree');

var source = [
    "({",
    "    *foo(arg1) {}",
    "})"
].join("\n");

var ast = espree.parse(source, {
    range: true,
    tokens: true,
    ecmaFeatures: {
        generators: true,
        objectLiteralShorthandMethods: true
    }
});

console.log(inspect(ast.body[0].expression.properties[0]));
console.log(inspect(ast.body[0].expression.properties[0].value.params[0]));
console.log(inspect(ast.tokens));
$  node test.js
{ type: 'Property',
  key: { type: 'Identifier', name: 'foo', range: [ 8, 11 ] },
  value:
   { type: 'FunctionExpression',
     id: null,
     params: [ [Object] ],
     defaults: [],
     body: { type: 'BlockStatement', body: [], range: [Object] },
     rest: null,
     generator: true,
     expression: false,
     range: [ 18, 20 ] },
  kind: 'init',
  method: true,
  shorthand: false,
  computed: false,
  range: [ 7, 20 ] }
{ type: 'Identifier', name: 'arg1', range: [ 12, 16 ] }
[ { type: 'Punctuator', value: '(', range: [ 0, 1 ] },
  { type: 'Punctuator', value: '{', range: [ 1, 2 ] },
  { type: 'Punctuator', value: '*', range: [ 7, 8 ] },
  { type: 'Identifier', value: 'foo', range: [ 8, 11 ] },
  { type: 'Punctuator', value: '(', range: [ 11, 12 ] },
  { type: 'Identifier', value: 'arg1', range: [ 12, 16 ] },
  { type: 'Punctuator', value: ')', range: [ 16, 17 ] },
  { type: 'Punctuator', value: '{', range: [ 18, 19 ] },
  { type: 'Punctuator', value: '}', range: [ 19, 20 ] },
  { type: 'Punctuator', value: '}', range: [ 21, 22 ] },
  { type: 'Punctuator', value: ')', range: [ 22, 23 ] } ]

Edit: Add arg1 to output.

@nzakas
Copy link
Member

nzakas commented Jan 19, 2015

Interesting point. The conundrum here is that this behavior matches Esprima's, so even if it's the wrong thing, there's a large compatibly issue to consider.

This definitely seems weird, it's just a matter of figuring out of its a bug to fix or not.

@btmills
Copy link
Member Author

btmills commented Jan 19, 2015

Agreed; I wouldn't advocate breaking compatibility unnecessarily. However, I might be able to make the case that this would be a bug in Esprima:

Now that I edited the test script above to output arg1, it shows a strange scenario in which one can traverse, starting at the FunctionExpression (range 18-20), down into a child, arg1 (range 12-16), that lies outside the range of its parent. I can't think of any other instance off the top of my head where that would be the case.

@nzakas
Copy link
Member

nzakas commented Jan 19, 2015

I think a good case can be made that this is a bug. Would you mind filing an issue with Esprima to start the discussion?

@btmills
Copy link
Member Author

btmills commented Jan 19, 2015

Upon further investigation, this applies to non-generator shorthand methods as well.

Esprima issue: https://code.google.com/p/esprima/issues/detail?id=625

@nzakas nzakas added the bug label Feb 10, 2015
@btmills
Copy link
Member Author

btmills commented Feb 14, 2015

The Esprima issue has been moved to jquery/esprima#1029.

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

No branches or pull requests

2 participants