fix: script tag sourcemap locations

This commit is contained in:
dpiercey 2024-12-14 14:45:10 -07:00 committed by Dylan Piercey
parent a06818e556
commit 2142dfd05d
5 changed files with 46 additions and 23 deletions

View File

@ -0,0 +1,7 @@
---
"@marko/runtime-tags": patch
"@marko/compiler": patch
"marko": patch
---
Use statement parsing for script tag to improve sourcemap accuracy.

View File

@ -0,0 +1,7 @@
---
"@marko/runtime-tags": patch
"@marko/compiler": patch
"marko": patch
---
Fix issue where negative sourcemap offets leaning to the previous line were outputting incorrect values.

View File

@ -204,7 +204,17 @@ Object.assign(Printer.prototype, {
if (attributes.length) {
if (tagName === "script") {
for (let i = attributes.length; i--; ) {
if (attributes[i].value.fromBody) {
const attr = attributes[i];
if (
attr.name === "value" &&
(attr.value.type === "ArrowFunctionExpression" ||
attr.value.type === "FunctionExpression") &&
!(
attr.value.generator ||
attr.value.returnType ||
attr.value.typeParameters
)
) {
bodyOverride = attributes[i].value.body.body;
attributes = toSpliced(attributes, i);
break;

View File

@ -75,7 +75,7 @@ export function parseTemplateLiteral(file, str, sourceStart, sourceEnd) {
);
if (parsed.type === "TemplateLiteral") {
return parsed;
return t.templateLiteral(parsed.quasis, parsed.expressions);
}
return ensureParseError(file, parsed, sourceStart, sourceEnd);
@ -119,11 +119,19 @@ function tryParse(
const { parserOpts } = file.opts;
if (typeof sourceStart === "number") {
const startIndex = sourceStart - (sourceOffset || 0);
const startLoc = getLoc(file, startIndex);
const startLoc = getLoc(file, sourceStart);
const startLine = startLoc.line;
let startIndex = sourceStart;
let startColumn = startLoc.column;
if (sourceOffset) {
startIndex -= sourceOffset;
startColumn -= sourceOffset;
}
parserOpts.startLine = startLine;
parserOpts.startIndex = startIndex;
parserOpts.startColumn = startLoc.column;
parserOpts.startLine = startLoc.line;
parserOpts.startColumn = startColumn;
try {
return isExpression

View File

@ -3,7 +3,7 @@ import {
assertNoArgs,
assertNoAttributeTags,
assertNoParams,
parseExpression,
parseStatements,
type Tag,
} from "@marko/compiler/babel-utils";
@ -24,9 +24,7 @@ export default {
const { node } = tag;
const { body } = node.body;
if (body.length) {
const codePrefix = "async ()=>{";
const codeSuffix = "}";
let code = codePrefix;
let code = "";
for (const child of body) {
if (child.type !== "MarkoText") {
throw tag.hub.file.hub.buildError(
@ -39,24 +37,17 @@ export default {
code += child.value;
}
code += codeSuffix;
const start = body[0]?.start;
const end = body[body.length - 1]?.end;
const bodyExpression = parseExpression<t.ArrowFunctionExpression>(
tag.hub.file,
code,
start,
end,
codePrefix.length,
const bodyStatements = parseStatements(tag.hub.file, code, start, end);
const valueFn = t.arrowFunctionExpression(
[],
t.blockStatement(bodyStatements),
traverseContains(bodyStatements, isAwaitExpression),
);
bodyExpression.async = traverseContains(
bodyExpression.body,
isAwaitExpression,
);
(bodyExpression as any).fromBody = true;
node.attributes.push(t.markoAttribute("value", bodyExpression));
node.attributes.push(t.markoAttribute("value", valueFn));
node.body.body = [];
}
},