Skip to content

Commit

Permalink
fix: switch to our fork acorn typescript plugin (#15393)
Browse files Browse the repository at this point in the history
* fix: switch to our fork acorn typescript plugin

* changeset

* bump

* bump

* bump

* adjust snapshots

* this should be obsolete now

* bump

* bump
  • Loading branch information
dummdidumm authored Mar 4, 2025
1 parent 0f26337 commit ef7fd96
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 68 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-cougars-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: switch acorn-typescript plugin
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
"@jridgewell/sourcemap-codec": "^1.5.0",
"@types/estree": "^1.0.5",
"acorn": "^8.12.1",
"acorn-typescript": "^1.4.13",
"@sveltejs/acorn-typescript": "^1.0.5",
"aria-query": "^5.3.1",
"axobject-query": "^4.1.0",
"clsx": "^2.1.1",
Expand Down
47 changes: 2 additions & 45 deletions packages/svelte/src/compiler/phases/1-parse/acorn.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/** @import { Comment, Program } from 'estree' */
/** @import { Node } from 'acorn' */
import * as acorn from 'acorn';
import { walk } from 'zimmerframe';
import { tsPlugin } from 'acorn-typescript';
import { locator } from '../../state.js';
import { tsPlugin } from '@sveltejs/acorn-typescript';

const ParserWithTS = acorn.Parser.extend(tsPlugin({ allowSatisfies: true }));
const ParserWithTS = acorn.Parser.extend(tsPlugin());

/**
* @param {string} source
Expand Down Expand Up @@ -48,7 +46,6 @@ export function parse(source, typescript, is_script) {
}
}

if (typescript) amend(source, ast);
add_comments(ast);

return /** @type {Program} */ (ast);
Expand All @@ -71,7 +68,6 @@ export function parse_expression_at(source, typescript, index) {
locations: true
});

if (typescript) amend(source, ast);
add_comments(ast);

return ast;
Expand Down Expand Up @@ -173,42 +169,3 @@ function get_comment_handlers(source) {
}
};
}

/**
* Tidy up some stuff left behind by acorn-typescript
* @param {string} source
* @param {Node} node
*/
function amend(source, node) {
return walk(node, null, {
_(node, context) {
// @ts-expect-error
delete node.loc.start.index;
// @ts-expect-error
delete node.loc.end.index;

if (typeof node.loc?.end === 'number') {
const loc = locator(node.loc.end);
if (loc) {
node.loc.end = {
line: loc.line,
column: loc.column
};
}
}

if (
/** @type {any} */ (node).typeAnnotation &&
(node.end === undefined || node.end < node.start)
) {
// i think there might be a bug in acorn-typescript that prevents
// `end` from being assigned when there's a type annotation
let end = /** @type {any} */ (node).typeAnnotation.start;
while (/\s/.test(source[end - 1])) end -= 1;
node.end = end;
}

context.next();
}
});
}
3 changes: 0 additions & 3 deletions packages/svelte/src/compiler/phases/1-parse/ambient.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"id": {
"type": "Identifier",
"start": 52,
"end": 57,
"end": 65,
"loc": {
"start": {
"line": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"type": "Identifier",
"start": 43,
"end": 46,
"end": 54,
"loc": {
"start": {
"line": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"end": 147,
"type": "OnDirective",
"name": "click",
"modifiers": [],
"expression": {
"type": "ArrowFunctionExpression",
"start": 73,
Expand All @@ -48,7 +47,7 @@
{
"type": "Identifier",
"start": 74,
"end": 75,
"end": 87,
"loc": {
"start": {
"line": 6,
Expand Down Expand Up @@ -155,7 +154,7 @@
"id": {
"type": "Identifier",
"start": 102,
"end": 106,
"end": 114,
"loc": {
"start": {
"line": 7,
Expand Down Expand Up @@ -316,7 +315,8 @@
}
]
}
}
},
"modifiers": []
}
],
"fragment": {
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"allowJs": true,
"checkJs": true,
"paths": {
"acorn-typescript": ["./src/compiler/phases/1-parse/ambient.d.ts"],
"svelte": ["./src/index.d.ts"],
"svelte/action": ["./src/action/public.d.ts"],
"svelte/compiler": ["./src/compiler/public.d.ts"],
Expand Down
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef7fd96

Please sign in to comment.