Skip to content

Commit fbda08f

Browse files
committed
extract recast parser config into extra file, add vendor information and license
1 parent 85481c7 commit fbda08f

File tree

2 files changed

+86
-52
lines changed

2 files changed

+86
-52
lines changed

packages/sveltekit/src/vite/autoInstrument.ts

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import * as recast from 'recast';
44
import t = recast.types.namedTypes;
5-
import type { ParserPlugin } from '@babel/parser';
6-
import { parse as babelParse } from '@babel/parser';
75
import type { Plugin } from 'vite';
86
import { WRAPPED_MODULE_SUFFIX } from '../common/utils';
7+
import { parser } from './recastTypescriptParser';
98

109
export type AutoInstrumentSelection = {
1110
/**
@@ -103,56 +102,6 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>
103102

104103
const code = (await fs.promises.readFile(id, 'utf8')).toString();
105104

106-
// Taken from recast's typescript parser config, minus the JSX plugin
107-
// see: https://github.com/benjamn/recast/blob/master/parsers/_babel_options.ts
108-
// see: https://github.com/benjamn/recast/blob/master/parsers/babel-ts.ts
109-
const parser = {
110-
parse: (source: string) =>
111-
babelParse(source, {
112-
plugins: [
113-
'typescript',
114-
'asyncGenerators',
115-
'bigInt',
116-
'classPrivateMethods',
117-
'classPrivateProperties',
118-
'classProperties',
119-
'classStaticBlock',
120-
'decimal',
121-
'decorators-legacy',
122-
'doExpressions',
123-
'dynamicImport',
124-
'exportDefaultFrom',
125-
'exportNamespaceFrom',
126-
'functionBind',
127-
'functionSent',
128-
'importAssertions',
129-
'exportExtensions' as ParserPlugin,
130-
'importMeta',
131-
'nullishCoalescingOperator',
132-
'numericSeparator',
133-
'objectRestSpread',
134-
'optionalCatchBinding',
135-
'optionalChaining',
136-
[
137-
'pipelineOperator',
138-
{
139-
proposal: 'minimal',
140-
},
141-
],
142-
[
143-
'recordAndTuple',
144-
{
145-
syntaxType: 'hash',
146-
},
147-
],
148-
'throwExpressions',
149-
'topLevelAwait',
150-
'v8intrinsic',
151-
],
152-
sourceType: 'module',
153-
}),
154-
};
155-
156105
const ast = recast.parse(code, {
157106
parser,
158107
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This babel parser config is taken from recast's typescript parser config, specifically from these two files:
2+
// see: https://github.com/benjamn/recast/blob/master/parsers/_babel_options.ts
3+
// see: https://github.com/benjamn/recast/blob/master/parsers/babel-ts.ts
4+
//
5+
// Changes:
6+
// - we don't add the 'jsx' plugin, to correctly parse TypeScript angle bracket type assertions
7+
// (see https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions)
8+
// - minor import and export changes
9+
// - merged the two files linked above into one for simplicity
10+
11+
// Date of access: 2025-03-04
12+
// Commit: https://github.com/benjamn/recast/commit/ba5132174894b496285da9d001f1f2524ceaed3a
13+
14+
// Recast license:
15+
16+
// Copyright (c) 2012 Ben Newman <[email protected]>
17+
18+
// Permission is hereby granted, free of charge, to any person obtaining
19+
// a copy of this software and associated documentation files (the
20+
// "Software"), to deal in the Software without restriction, including
21+
// without limitation the rights to use, copy, modify, merge, publish,
22+
// distribute, sublicense, and/or sell copies of the Software, and to
23+
// permit persons to whom the Software is furnished to do so, subject to
24+
// the following conditions:
25+
26+
// The above copyright notice and this permission notice shall be
27+
// included in all copies or substantial portions of the Software.
28+
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33+
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34+
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
37+
import type { ParserPlugin } from '@babel/parser';
38+
import { parse as babelParse } from '@babel/parser';
39+
40+
export const parser = {
41+
parse: (source: string) =>
42+
babelParse(source, {
43+
plugins: [
44+
'typescript',
45+
'asyncGenerators',
46+
'bigInt',
47+
'classPrivateMethods',
48+
'classPrivateProperties',
49+
'classProperties',
50+
'classStaticBlock',
51+
'decimal',
52+
'decorators-legacy',
53+
'doExpressions',
54+
'dynamicImport',
55+
'exportDefaultFrom',
56+
'exportNamespaceFrom',
57+
'functionBind',
58+
'functionSent',
59+
'importAssertions',
60+
'exportExtensions' as ParserPlugin,
61+
'importMeta',
62+
'nullishCoalescingOperator',
63+
'numericSeparator',
64+
'objectRestSpread',
65+
'optionalCatchBinding',
66+
'optionalChaining',
67+
[
68+
'pipelineOperator',
69+
{
70+
proposal: 'minimal',
71+
},
72+
],
73+
[
74+
'recordAndTuple',
75+
{
76+
syntaxType: 'hash',
77+
},
78+
],
79+
'throwExpressions',
80+
'topLevelAwait',
81+
'v8intrinsic',
82+
],
83+
sourceType: 'module',
84+
}),
85+
};

0 commit comments

Comments
 (0)