Skip to content

Commit 4b2ed6b

Browse files
authored
chore: switch to @sveltejs/acorn-typescript (#51)
* switch to `@sveltejs/acorn-typescript` * fix tests
1 parent 9daf5dd commit 4b2ed6b

File tree

11 files changed

+48
-91
lines changed

11 files changed

+48
-91
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"types": "./types/index.d.ts",
2121
"devDependencies": {
2222
"@changesets/cli": "^2.27.11",
23+
"@sveltejs/acorn-typescript": "^1.0.5",
2324
"@typescript-eslint/types": "^8.2.0",
2425
"@vitest/ui": "^2.1.1",
2526
"acorn": "^8.11.3",
26-
"acorn-typescript": "^1.4.13",
2727
"dts-buddy": "^0.5.4",
2828
"prettier": "^3.0.3",
2929
"typescript": "^5.7.2",

pnpm-lock.yaml

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/handlers.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,9 @@ function handle_type_annotation(node, state) {
501501
case 'TSTypeReference':
502502
handle(node.typeName, state);
503503

504-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
505-
if (node.typeParameters) handle_type_annotation(node.typeParameters, state);
504+
if (node.typeArguments) {
505+
handle_type_annotation(node.typeArguments, state);
506+
}
506507
break;
507508
case 'TSTypeParameterInstantiation':
508509
case 'TSTypeParameterDeclaration':
@@ -514,7 +515,7 @@ function handle_type_annotation(node, state) {
514515
state.commands.push('>');
515516
break;
516517
case 'TSTypeParameter':
517-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
518+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
518519
state.commands.push(node.name);
519520

520521
if (node.constraint) {
@@ -536,14 +537,14 @@ function handle_type_annotation(node, state) {
536537
case 'TSFunctionType':
537538
if (node.typeParameters) handle_type_annotation(node.typeParameters, state);
538539

539-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
540+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
540541
const parameters = node.parameters;
541542
state.commands.push('(');
542543
sequence(parameters, state, false, handle);
543544

544545
state.commands.push(') => ');
545546

546-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
547+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
547548
handle_type_annotation(node.typeAnnotation.typeAnnotation, state);
548549
break;
549550
case 'TSIndexSignature':
@@ -552,19 +553,19 @@ function handle_type_annotation(node, state) {
552553
sequence(indexParameters, state, false, handle);
553554
state.commands.push(']');
554555

555-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
556+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
556557
handle_type_annotation(node.typeAnnotation, state);
557558
break;
558559
case 'TSMethodSignature':
559560
handle(node.key, state);
560561

561-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
562+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
562563
const parametersSignature = node.parameters;
563564
state.commands.push('(');
564565
sequence(parametersSignature, state, false, handle);
565566
state.commands.push(')');
566567

567-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
568+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
568569
handle_type_annotation(node.typeAnnotation, state);
569570
break;
570571
case 'TSExpressionWithTypeArguments':
@@ -715,8 +716,7 @@ const shared = {
715716
state.commands.push('?.');
716717
}
717718

718-
// @ts-expect-error
719-
if (node.typeParameters) handle_type_annotation(node.typeParameters, state);
719+
if (node.typeArguments) handle_type_annotation(node.typeArguments, state);
720720

721721
const open = create_sequence();
722722
const join = create_sequence();
@@ -852,7 +852,7 @@ const shared = {
852852
state.commands.push('...');
853853
handle(node.argument, state);
854854

855-
// @ts-expect-error `acorn-typescript` and `@typescript-esling/types` have slightly different type definitions
855+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
856856
if (node.typeAnnotation) handle_type_annotation(node.typeAnnotation, state);
857857
}
858858
};

test/common.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
// @ts-check
2+
13
import * as acorn from 'acorn';
2-
import { tsPlugin } from 'acorn-typescript';
4+
import { tsPlugin } from '@sveltejs/acorn-typescript';
35
import { walk } from 'zimmerframe';
46

7+
/** @import { TSESTree } from '@typescript-eslint/types' */
8+
/** @import { NodeWithComments } from '../src/types' */
9+
510
// @ts-expect-error
611
export const acornTs = acorn.Parser.extend(tsPlugin({ allowSatisfies: true }));
712

813
/** @param {string} input */
914
export function load(input) {
15+
/** @type {any[]} */
1016
const comments = [];
1117

1218
const ast = acornTs.parse(input, {

test/esrap.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
/** @import { TSESTree } from '@typescript-eslint/types' */
3-
/** @import { NodeWithComments, PrintOptions } from '../src/types' */
3+
/** @import { PrintOptions } from '../src/types' */
44
import fs from 'node:fs';
55
import { expect, test } from 'vitest';
66
import { walk } from 'zimmerframe';

test/indent.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-check
2+
13
import { test } from 'vitest';
24
import { load } from './common';
35
import { print } from '../src';

test/quotes.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
// @ts-check
2+
13
import { test } from 'vitest';
24
import { print } from '../src/index.js';
35
import { expect } from 'vitest';
46
import { load } from './common.js';
57
import { walk } from 'zimmerframe';
6-
import { TSESTree } from '@typescript-eslint/types';
8+
9+
/** @import { TSESTree } from '@typescript-eslint/types' */
710

811
/**
912
* Removes the `raw` property from all `Literal` nodes, as the printer is prefering it's
1013
* value. Only if the `raw` value is not present it will try to add the prefered quoting
11-
* @param {TSESTree.Program} ast
14+
* @param {TSESTree.Node} ast
1215
*/
1316
function clean(ast) {
1417
walk(ast, null, {
1518
Literal(node, { next }) {
19+
// @ts-expect-error
1620
delete node.raw;
1721

1822
next();
@@ -100,4 +104,4 @@ test('correctly handle \\n\\r', () => {
100104
const code = print(ast).code;
101105

102106
expect(code).toMatchInlineSnapshot(`"const str = 'a\\n\\rb';"`);
103-
});
107+
});

test/samples/large-file/expected.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,10 @@
22942294
return bySet ? markFunction(superMatcher) : superMatcher;
22952295
}
22962296

2297-
function compile(selector, match) /* Internal Use Only */ {
2297+
function compile(
2298+
selector,
2299+
match /* Internal Use Only */
2300+
) {
22982301
var i,
22992302
setMatchers = [],
23002303
elementMatchers = [],

test/samples/large-file/expected.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sandbox/index.js

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,10 @@
11
// @ts-check
2-
/** @import { TSESTree } from '@typescript-eslint/types' */
3-
/** @import { NodeWithComments } from '../../src/types' */
4-
import * as acorn from 'acorn';
5-
import { tsPlugin } from 'acorn-typescript';
6-
import { walk } from 'zimmerframe';
72
import { print } from '../../src/index.js';
3+
import { load } from '../common.js';
84
import fs from 'fs';
95
import path from 'path';
106
import { fileURLToPath } from 'url';
117

12-
/** @param {string} input */
13-
function load(input) {
14-
const comments = [];
15-
16-
// @ts-expect-error
17-
const acornTs = acorn.Parser.extend(tsPlugin({ allowSatisfies: true }));
18-
19-
const ast = acornTs.parse(input, {
20-
ecmaVersion: 'latest',
21-
sourceType: 'module',
22-
locations: true,
23-
onComment: (block, value, start, end) => {
24-
if (block && /\n/.test(value)) {
25-
let a = start;
26-
while (a > 0 && input[a - 1] !== '\n') a -= 1;
27-
28-
let b = a;
29-
while (/[ \t]/.test(input[b])) b += 1;
30-
31-
const indentation = input.slice(a, b);
32-
value = value.replace(new RegExp(`^${indentation}`, 'gm'), '');
33-
}
34-
35-
comments.push({ type: block ? 'Block' : 'Line', value, start, end });
36-
}
37-
});
38-
39-
walk(ast, null, {
40-
_(node, { next }) {
41-
const commentNode = /** @type {NodeWithComments} */ (/** @type {any} */ (node));
42-
let comment;
43-
44-
while (comments[0] && comments[0].start < node.start) {
45-
comment = comments.shift();
46-
(commentNode.leadingComments ||= []).push(comment);
47-
}
48-
49-
next();
50-
51-
if (comments[0]) {
52-
const slice = input.slice(node.end, comments[0].start);
53-
54-
if (/^[,) \t]*$/.test(slice)) {
55-
commentNode.trailingComments = [comments.shift()];
56-
}
57-
}
58-
}
59-
});
60-
61-
return /** @type {TSESTree.Program} */ (/** @type {any} */ (ast));
62-
}
63-
648
const dir = path.resolve(fileURLToPath(import.meta.url), '..');
659
const input_js = fs.readFileSync(`${dir}/_input.ts`);
6610
const content = input_js.toString();

0 commit comments

Comments
 (0)