Skip to content

Commit

Permalink
Fix: Extend property method range and loc to include params (fixes #36)
Browse files Browse the repository at this point in the history
This is jquery/esprima#1036 ported to Espree.

Previously, the range and loc for get, set, and shorthand methods
inside object literal properties included only the method body,
meaning a method's parameters were outside the range of their parent
`FunctionExpression` node. This was happening because the marker for
the `FunctionExpression` node wasn't being created until
`parsePropertyFunction` was called, by which point the mothod's
parameters had already been parsed. This PR solves the issue by
creating the marker for the start of the `FunctionExpression` node as
soon as it determines that the property is a get, set, or shorthand
method, then passing the marker into `parsePropertyFunction`.

Though #36 only describes the specific case of generator shorthand
methods, the same bug is present on get, set, and shorthand property
methods, and this PR fixes all three cases.
  • Loading branch information
btmills committed Feb 17, 2015
1 parent 692b9b1 commit b4aa879
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 102 deletions.
21 changes: 14 additions & 7 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2375,8 +2375,7 @@ function parsePropertyFunction(options) {
previousYieldAllowed = state.yieldAllowed,
params = options.params || [],
defaults = options.defaults || [],
body,
marker = markerCreate();
body;

state.yieldAllowed = options.generator;

Expand All @@ -2393,7 +2392,7 @@ function parsePropertyFunction(options) {
strict = previousStrict;
state.yieldAllowed = previousYieldAllowed;

return markerApply(marker, astNodeFactory.createFunctionExpression(
return markerApply(options.marker, astNodeFactory.createFunctionExpression(
null,
params,
defaults,
Expand All @@ -2406,6 +2405,7 @@ function parsePropertyFunction(options) {

function parsePropertyMethodFunction(options) {
var previousStrict = strict,
marker = markerCreate(),
tmp,
method;

Expand All @@ -2421,7 +2421,8 @@ function parsePropertyMethodFunction(options) {
params: tmp.params,
defaults: tmp.defaults,
rest: tmp.rest,
generator: options ? options.generator : false
generator: options ? options.generator : false,
marker: marker
});

strict = previousStrict;
Expand Down Expand Up @@ -2461,7 +2462,7 @@ function parseObjectPropertyKey() {
}

function parseObjectProperty() {
var token, key, id, param, computed;
var token, key, id, param, computed, methodMarker;
var allowComputed = extra.ecmaFeatures.objectLiteralComputedProperties,
allowMethod = extra.ecmaFeatures.objectLiteralShorthandMethods,
allowShorthand = extra.ecmaFeatures.objectLiteralShorthandProperties,
Expand All @@ -2483,14 +2484,18 @@ function parseObjectProperty() {
if (token.value === "get" && !(match(":") || match("("))) {
computed = (lookahead.value === "[");
key = parseObjectPropertyKey();
methodMarker = markerCreate();
expect("(");
expect(")");
return markerApply(
marker,
astNodeFactory.createProperty(
"get",
key,
parsePropertyFunction({ generator: false }),
parsePropertyFunction({
generator: false,
marker: methodMarker
}),
false,
false,
computed
Expand All @@ -2501,6 +2506,7 @@ function parseObjectProperty() {
if (token.value === "set" && !(match(":") || match("("))) {
computed = (lookahead.value === "[");
key = parseObjectPropertyKey();
methodMarker = markerCreate();
expect("(");
token = lookahead;
param = [ parseVariableIdentifier() ];
Expand All @@ -2513,7 +2519,8 @@ function parseObjectProperty() {
parsePropertyFunction({
params: param,
generator: false,
name: token
name: token,
marker: methodMarker
}),
false,
false,
Expand Down
68 changes: 34 additions & 34 deletions tests/fixtures/ast/Object-Initializer.json
Original file line number Diff line number Diff line change
Expand Up @@ -1219,13 +1219,13 @@
"generator": false,
"expression": false,
"range": [
18,
15,
36
],
"loc": {
"start": {
"line": 1,
"column": 18
"column": 15
},
"end": {
"line": 1,
Expand Down Expand Up @@ -1371,13 +1371,13 @@
"generator": false,
"expression": false,
"range": [
18,
15,
20
],
"loc": {
"start": {
"line": 1,
"column": 18
"column": 15
},
"end": {
"line": 1,
Expand Down Expand Up @@ -1523,13 +1523,13 @@
"generator": false,
"expression": false,
"range": [
15,
12,
17
],
"loc": {
"start": {
"line": 1,
"column": 15
"column": 12
},
"end": {
"line": 1,
Expand Down Expand Up @@ -1675,13 +1675,13 @@
"generator": false,
"expression": false,
"range": [
17,
14,
19
],
"loc": {
"start": {
"line": 1,
"column": 17
"column": 14
},
"end": {
"line": 1,
Expand Down Expand Up @@ -1827,13 +1827,13 @@
"generator": false,
"expression": false,
"range": [
18,
15,
20
],
"loc": {
"start": {
"line": 1,
"column": 18
"column": 15
},
"end": {
"line": 1,
Expand Down Expand Up @@ -1979,13 +1979,13 @@
"generator": false,
"expression": false,
"range": [
17,
14,
19
],
"loc": {
"start": {
"line": 1,
"column": 17
"column": 14
},
"end": {
"line": 1,
Expand Down Expand Up @@ -2132,13 +2132,13 @@
"generator": false,
"expression": false,
"range": [
20,
17,
22
],
"loc": {
"start": {
"line": 1,
"column": 20
"column": 17
},
"end": {
"line": 1,
Expand Down Expand Up @@ -2285,13 +2285,13 @@
"generator": false,
"expression": false,
"range": [
15,
12,
17
],
"loc": {
"start": {
"line": 1,
"column": 15
"column": 12
},
"end": {
"line": 1,
Expand Down Expand Up @@ -2528,13 +2528,13 @@
"generator": false,
"expression": false,
"range": [
19,
15,
34
],
"loc": {
"start": {
"line": 1,
"column": 19
"column": 15
},
"end": {
"line": 1,
Expand Down Expand Up @@ -2771,13 +2771,13 @@
"generator": false,
"expression": false,
"range": [
16,
12,
28
],
"loc": {
"start": {
"line": 1,
"column": 16
"column": 12
},
"end": {
"line": 1,
Expand Down Expand Up @@ -3014,13 +3014,13 @@
"generator": false,
"expression": false,
"range": [
18,
14,
32
],
"loc": {
"start": {
"line": 1,
"column": 18
"column": 14
},
"end": {
"line": 1,
Expand Down Expand Up @@ -3257,13 +3257,13 @@
"generator": false,
"expression": false,
"range": [
19,
15,
34
],
"loc": {
"start": {
"line": 1,
"column": 19
"column": 15
},
"end": {
"line": 1,
Expand Down Expand Up @@ -3500,13 +3500,13 @@
"generator": false,
"expression": false,
"range": [
18,
14,
32
],
"loc": {
"start": {
"line": 1,
"column": 18
"column": 14
},
"end": {
"line": 1,
Expand Down Expand Up @@ -3744,13 +3744,13 @@
"generator": false,
"expression": false,
"range": [
20,
16,
34
],
"loc": {
"start": {
"line": 1,
"column": 20
"column": 16
},
"end": {
"line": 1,
Expand Down Expand Up @@ -3988,13 +3988,13 @@
"generator": false,
"expression": false,
"range": [
16,
12,
30
],
"loc": {
"start": {
"line": 1,
"column": 16
"column": 12
},
"end": {
"line": 1,
Expand Down Expand Up @@ -4697,13 +4697,13 @@
"generator": false,
"expression": false,
"range": [
18,
15,
36
],
"loc": {
"start": {
"line": 1,
"column": 18
"column": 15
},
"end": {
"line": 1,
Expand Down Expand Up @@ -4868,13 +4868,13 @@
"generator": false,
"expression": false,
"range": [
55,
47,
75
],
"loc": {
"start": {
"line": 1,
"column": 55
"column": 47
},
"end": {
"line": 1,
Expand Down
Loading

0 comments on commit b4aa879

Please sign in to comment.