mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
fix: script tag sourcemap locations
This commit is contained in:
parent
a06818e556
commit
2142dfd05d
7
.changeset/pink-trainers-cross.md
Normal file
7
.changeset/pink-trainers-cross.md
Normal file
@ -0,0 +1,7 @@
|
||||
---
|
||||
"@marko/runtime-tags": patch
|
||||
"@marko/compiler": patch
|
||||
"marko": patch
|
||||
---
|
||||
|
||||
Use statement parsing for script tag to improve sourcemap accuracy.
|
||||
7
.changeset/shy-pots-act.md
Normal file
7
.changeset/shy-pots-act.md
Normal 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.
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 = [];
|
||||
}
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user