Skip to content

Commit

Permalink
fix(jsx): fix wrong range and loc on JSXEmptyExpression
Browse files Browse the repository at this point in the history
closes #125
  • Loading branch information
3cp committed Oct 27, 2020
1 parent d62d0b8 commit 11765ce
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9106,7 +9106,7 @@ function parseJSXExpressionContainer(
if (parser.token === Token.RightBrace) {
// JSX attributes must only be assigned a non-empty 'expression'
if (isAttr) report(parser, Errors.InvalidNonEmptyJSXExpr);
expression = parseJSXEmptyExpression(parser, context, tokenPos, linePos, colPos);
expression = parseJSXEmptyExpression(parser, context, parser.startPos, parser.startLine, parser.startColumn);
} else {
expression = parseExpression(parser, context, 1, 0, 0, tokenPos, linePos, colPos);
}
Expand Down Expand Up @@ -9163,6 +9163,13 @@ function parseJSXEmptyExpression(
line: number,
column: number
): ESTree.JSXEmptyExpression {
// Since " }" is treated as single token, we have to artificially break
// it into " " and "}".
// Move token start from beginning of whitespace(s) to beginning of "}",
// so JSXEmptyExpression can have correct end loc.
parser.startPos = parser.tokenPos;
parser.startLine = parser.linePos;
parser.startColumn = parser.colPos;
return finishNode(parser, context, start, line, column, {
type: 'JSXEmptyExpression'
});
Expand Down
157 changes: 157 additions & 0 deletions test/parser/miscellaneous/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11645,6 +11645,163 @@ describe('Miscellaneous - JSX', () => {
}
]
}
],
[
'<div>{ }</div>',
Context.OptionsJSX | Context.OptionsRanges | Context.OptionsLoc,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'JSXElement',
children: [
{
type: 'JSXExpressionContainer',
expression: {
type: 'JSXEmptyExpression',
start: 6,
end: 7,
range: [6, 7],
loc: {
start: {
line: 1,
column: 6
},
end: {
line: 1,
column: 7
}
}
},
start: 5,
end: 8,
range: [5, 8],
loc: {
start: {
line: 1,
column: 5
},
end: {
line: 1,
column: 8
}
}
}
],
openingElement: {
type: 'JSXOpeningElement',
name: {
type: 'JSXIdentifier',
name: 'div',
start: 1,
end: 4,
range: [1, 4],
loc: {
start: {
line: 1,
column: 1
},
end: {
line: 1,
column: 4
}
}
},
attributes: [],
selfClosing: false,
start: 0,
end: 5,
range: [0, 5],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 5
}
}
},
closingElement: {
type: 'JSXClosingElement',
name: {
type: 'JSXIdentifier',
name: 'div',
start: 10,
end: 13,
range: [10, 13],
loc: {
start: {
line: 1,
column: 10
},
end: {
line: 1,
column: 13
}
}
},
start: 8,
end: 14,
range: [8, 14],
loc: {
start: {
line: 1,
column: 8
},
end: {
line: 1,
column: 14
}
}
},
start: 0,
end: 14,
range: [0, 14],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 14
}
}
},
start: 0,
end: 14,
range: [0, 14],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 14
}
}
}
],
start: 0,
end: 14,
range: [0, 14],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 14
}
}
}
]
]);
});

0 comments on commit 11765ce

Please sign in to comment.