Skip to content

Commit

Permalink
fix(parser): fix wrong loc for BinaryExpression
Browse files Browse the repository at this point in the history
closes #169
  • Loading branch information
3cp committed Jan 28, 2021
1 parent a893c16 commit ab1ab37
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5969,7 +5969,7 @@ export function parseObjectLiteralOrPattern(
if (parser.token === Token.Comma || parser.token === Token.RightBrace) {
if (parser.assignable & AssignmentKind.CannotAssign) destructible |= DestructuringKind.CannotDestruct;
} else {
value = parseMemberOrUpdateExpression(parser, context, value, inGroup, 0, tokenPos, tokenPos, colPos);
value = parseMemberOrUpdateExpression(parser, context, value, inGroup, 0, tokenPos, linePos, colPos);

destructible = parser.assignable & AssignmentKind.CannotAssign ? DestructuringKind.CannotDestruct : 0;

Expand All @@ -5981,7 +5981,7 @@ export function parseObjectLiteralOrPattern(
inGroup,
isPattern,
tokenPos,
tokenPos,
linePos,
colPos,
value
);
Expand Down
181 changes: 181 additions & 0 deletions test/parser/expressions/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,187 @@ describe('Expressions - Binary', () => {
],
sourceType: 'script'
}
],
[
`var a = {b: 'u' + 1 }`,
Context.None | Context.OptionsRanges | Context.OptionsLoc,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'VariableDeclaration',
kind: 'var',
declarations: [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: 'a',
start: 4,
end: 5,
range: [4, 5],
loc: {
start: {
line: 1,
column: 4
},
end: {
line: 1,
column: 5
}
}
},
init: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
key: {
type: 'Identifier',
name: 'b',
start: 9,
end: 10,
range: [9, 10],
loc: {
start: {
line: 1,
column: 9
},
end: {
line: 1,
column: 10
}
}
},
value: {
type: 'BinaryExpression',
left: {
type: 'Literal',
value: 'u',
start: 12,
end: 15,
range: [12, 15],
loc: {
start: {
line: 1,
column: 12
},
end: {
line: 1,
column: 15
}
}
},
right: {
type: 'Literal',
value: 1,
start: 18,
end: 19,
range: [18, 19],
loc: {
start: {
line: 1,
column: 18
},
end: {
line: 1,
column: 19
}
}
},
operator: '+',
start: 12,
end: 19,
range: [12, 19],
loc: {
start: {
line: 1,
column: 12
},
end: {
line: 1,
column: 19
}
}
},
kind: 'init',
computed: false,
method: false,
shorthand: false,
start: 9,
end: 19,
range: [9, 19],
loc: {
start: {
line: 1,
column: 9
},
end: {
line: 1,
column: 19
}
}
}
],
start: 8,
end: 21,
range: [8, 21],
loc: {
start: {
line: 1,
column: 8
},
end: {
line: 1,
column: 21
}
}
},
start: 4,
end: 21,
range: [4, 21],
loc: {
start: {
line: 1,
column: 4
},
end: {
line: 1,
column: 21
}
}
}
],
start: 0,
end: 21,
range: [0, 21],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 21
}
}
}
],
start: 0,
end: 21,
range: [0, 21],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 21
}
}
}
]
]);
});

2 comments on commit ab1ab37

@KFlash
Copy link
Contributor

@KFlash KFlash commented on ab1ab37 Jan 28, 2021

Choose a reason for hiding this comment

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

Maybe off topic. A few comments have wrong loc too and current comment code isn't 100%. perfect.
Should we publish an alternative algo for getting the comments and loc?

Click on the pretty print demo with comments in this readme and you may understand what my point is.

https://github.com/kataw/kataw

@KFlash
Copy link
Contributor

@KFlash KFlash commented on ab1ab37 Jan 28, 2021

Choose a reason for hiding this comment

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

I proposed an alternative method because with current algorithm Prettier can't handle cases like this

let [,,,,,,,,,,,,,/1/,,,,,,,,,,,,,,,,,] = x;

This is the expected output

Please sign in to comment.