Skip to content

Commit d295d49

Browse files
authored
Fix TemplateElement location (#27)
1 parent eb4f70c commit d295d49

File tree

5 files changed

+1022
-1
lines changed

5 files changed

+1022
-1
lines changed

src/parser/converts/es.ts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,98 @@ const EXTRACT_TOKENS: {
462462
parent: ESTree.Node | null,
463463
) => void
464464
} = EXTRACT_TOKENS0
465+
const GET_LOC: {
466+
[key in ESTree.Node["type"]]: (
467+
node: any,
468+
ctx: Context,
469+
parent: ESTree.Node | null,
470+
) => { start: number; end: number }
471+
} = {
472+
ArrayExpression: getWithLoc,
473+
ArrayPattern: getWithLoc,
474+
ArrowFunctionExpression: getWithLoc,
475+
AssignmentExpression: getWithLoc,
476+
AssignmentPattern: getWithLoc,
477+
AwaitExpression: getWithLoc,
478+
BinaryExpression: getWithLoc,
479+
BlockStatement: getWithLoc,
480+
BreakStatement: getWithLoc,
481+
CallExpression: getWithLoc,
482+
CatchClause: getWithLoc,
483+
ChainExpression: getWithLoc,
484+
ClassBody: getWithLoc,
485+
ClassDeclaration: getWithLoc,
486+
ClassExpression: getWithLoc,
487+
ConditionalExpression: getWithLoc,
488+
ContinueStatement: getWithLoc,
489+
DebuggerStatement: getWithLoc,
490+
DoWhileStatement: getWithLoc,
491+
EmptyStatement: getWithLoc,
492+
ExportAllDeclaration: getWithLoc,
493+
ExportDefaultDeclaration: getWithLoc,
494+
ExportNamedDeclaration: getWithLoc,
495+
ExportSpecifier: getWithLoc,
496+
ExpressionStatement: getWithLoc,
497+
ForInStatement: getWithLoc,
498+
ForOfStatement: getWithLoc,
499+
ForStatement: getWithLoc,
500+
FunctionDeclaration: getWithLoc,
501+
FunctionExpression: getWithLoc,
502+
Identifier: getWithLoc,
503+
IfStatement: getWithLoc,
504+
ImportDeclaration: getWithLoc,
505+
ImportDefaultSpecifier: getWithLoc,
506+
ImportExpression: getWithLoc,
507+
ImportNamespaceSpecifier: getWithLoc,
508+
ImportSpecifier: getWithLoc,
509+
LabeledStatement: getWithLoc,
510+
Literal: getWithLoc,
511+
LogicalExpression: getWithLoc,
512+
MemberExpression: getWithLoc,
513+
MetaProperty: getWithLoc,
514+
MethodDefinition: getWithLoc,
515+
NewExpression: getWithLoc,
516+
ObjectExpression: getWithLoc,
517+
ObjectPattern: getWithLoc,
518+
Program: getWithLoc,
519+
Property: getWithLoc,
520+
RestElement: getWithLoc,
521+
ReturnStatement: getWithLoc,
522+
SequenceExpression: getWithLoc,
523+
SpreadElement: getWithLoc,
524+
Super: getWithLoc,
525+
SwitchCase: getWithLoc,
526+
SwitchStatement: getWithLoc,
527+
TaggedTemplateExpression: getWithLoc,
528+
TemplateElement(
529+
node: ESTree.TemplateElement,
530+
_ctx: Context,
531+
parent: ESTree.Node | null,
532+
) {
533+
const literal: ESTree.TemplateLiteral = parent as never
534+
535+
const start =
536+
literal.quasis[0] === node
537+
? getWithLoc(literal).start
538+
: getWithLoc(node).start - 1
539+
const end =
540+
literal.quasis[literal.quasis.length - 1] === node
541+
? getWithLoc(literal).end
542+
: getWithLoc(node).end + 2
543+
return { start, end }
544+
},
545+
TemplateLiteral: getWithLoc,
546+
ThisExpression: getWithLoc,
547+
ThrowStatement: getWithLoc,
548+
TryStatement: getWithLoc,
549+
UnaryExpression: getWithLoc,
550+
UpdateExpression: getWithLoc,
551+
VariableDeclaration: getWithLoc,
552+
VariableDeclarator: getWithLoc,
553+
WhileStatement: getWithLoc,
554+
WithStatement: getWithLoc,
555+
YieldExpression: getWithLoc,
556+
}
465557

466558
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- ignore
467559
export function convertESNode<N extends ESTree.Node>(
@@ -524,7 +616,9 @@ function convertESNode0<N extends ESTree.Node>(
524616
const result = {
525617
...node,
526618
parent,
527-
...ctx.getConvertLocation(getWithLoc(node)),
619+
...ctx.getConvertLocation(
620+
(GET_LOC[node.type] ?? getWithLoc)(node, ctx, parent),
621+
),
528622
}
529623

530624
for (const key of getKeys(node)) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
const item = {}
3+
</script>
4+
<li
5+
class={`bytemd-toc-${item.level}`}
6+
/>

0 commit comments

Comments
 (0)