Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parsing negative offset locations #2418

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/curvy-squids-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/runtime-tags": patch
"@marko/compiler": patch
"marko": patch
"@marko/translator-interop-class-tags": patch
---

Fix issue when parsing embedded script code with negative offset locations.
52 changes: 14 additions & 38 deletions packages/compiler/babel-utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,69 +246,45 @@ export function withLoc<T extends t.Node>(
export function parseStatements<T extends t.Statement[]>(
file: t.BabelFile,
str: string,
): T;
export function parseStatements<T extends t.Statement[]>(
file: t.BabelFile,
str: string,
sourceStart: number,
sourceEnd: number,
sourceOffset?: number,
sourceStart?: null | number,
sourceEnd?: null | number,
sourceOffset?: null | number,
): T;

export function parseExpression<T extends t.Expression>(
file: t.BabelFile,
str: string,
): T;
export function parseExpression<T extends t.Expression>(
file: t.BabelFile,
str: string,
sourceStart: number,
sourceEnd: number,
sourceOffset?: number,
sourceStart?: null | number,
sourceEnd?: null | number,
sourceOffset?: null | number,
): T;

export function parseParams(
file: t.BabelFile,
str: string,
): t.Function["params"];
export function parseParams(
file: t.BabelFile,
str: string,
sourceStart: number,
sourceEnd: number,
sourceStart?: null | number,
sourceEnd?: null | number,
): t.Function["params"];

export function parseArgs(
file: t.BabelFile,
str: string,
): t.CallExpression["arguments"];
export function parseArgs(
file: t.BabelFile,
str: string,
sourceStart: number,
sourceEnd: number,
sourceStart?: null | number,
sourceEnd?: null | number,
): t.CallExpression["arguments"];

export function parseVar(
file: t.BabelFile,
str: string,
): t.VariableDeclarator["id"];
export function parseVar(
file: t.BabelFile,
str: string,
sourceStart: number,
sourceEnd: number,
sourceStart?: null | number,
sourceEnd?: null | number,
): t.VariableDeclarator["id"];

export function parseTemplateLiteral(
file: t.BabelFile,
str: string,
): t.TemplateLiteral;
export function parseTemplateLiteral(
file: t.BabelFile,
str: string,
sourceStart: number,
sourceEnd: number,
sourceStart?: null | number,
sourceEnd?: null | number,
): t.TemplateLiteral;

export function resolveRelativePath(file: t.BabelFile, request: string): string;
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler/src/babel-utils/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ function getLineIndexes(file) {
}

function findLoc(lineIndexes, startLine, index) {
if (index <= 0) {
return {
line: 1,
column: 0,
};
}

const endLine = lineIndexes.length - 1;
let max = endLine;
let line = startLine;
Expand Down
17 changes: 7 additions & 10 deletions packages/runtime-tags/src/translator/core/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,13 @@ export default {

const start = body[0]?.start;
const end = body[body.length - 1]?.end;
const bodyExpression =
start == null || end == null
? parseExpression<t.ArrowFunctionExpression>(tag.hub.file, code)
: parseExpression<t.ArrowFunctionExpression>(
tag.hub.file,
code,
codePrefix.length,
start,
end,
);
const bodyExpression = parseExpression<t.ArrowFunctionExpression>(
tag.hub.file,
code,
start,
end,
codePrefix.length,
);

bodyExpression.async = traverseContains(
bodyExpression.body,
Expand Down
Loading