1- import { Preset , PresetDependencies } from '.'
1+ import { Preset } from '.'
2+ import dedent from 'dedent'
3+ import { stripTypes } from '../simplify'
24import { definePreset } from './util/standard-schema-preset'
35
46export const _eval = definePreset ( { 'comparison?' : '"simplified" | "strict"' } , params => {
@@ -15,17 +17,21 @@ export const _eval = definePreset({'comparison?': '"simplified" | "strict"'}, pa
1517 if ( fnStr . startsWith ( '/*' ) ) fnStr = fnStr . split ( '*/' ) . slice ( 1 ) . join ( '*/' ) . trim ( )
1618 if ( before === fnStr ) break // the problem isn't comments or whitespace, give up
1719 }
20+ const exampleFn = dedent `
21+ const _myFn = () => {
22+ return '// abc123'
23+ }
24+ `
25+ const errorSuffix = `\n\nExample:\n\n${ exampleFn } `
1826 if ( ! fnStr . startsWith ( 'const ' ) ) {
19- throw new Error ( 'Preset function must start with `const ` (e.g. `const _myFn = () => ...`)' )
27+ throw new Error ( 'Preset function must start with `const `.' + errorSuffix )
2028 }
2129 const functionName = fnStr . replace ( 'const ' , '' ) . trim ( ) . split ( / \b / ) [ 0 ]
2230 if ( ! / ^ [ $ A - Z _ a - z ] [ \w $ ] * $ / . test ( functionName ) ) {
23- throw new Error (
24- 'Preset function must have an identifier name (e.g. `const _myFn = () => ...`), got: ' + functionName ,
25- )
31+ throw new Error ( 'Preset function must start with `const ` and have an identifier name.' + errorSuffix )
2632 }
2733
28- const plainJsFnStr = stripTypes ( fnStr , dependencies )
34+ const plainJsFnStr = stripTypes ( fnStr )
2935
3036 const fnBody = [
3137 plainJsFnStr , // declare the function
@@ -54,125 +60,3 @@ export const _eval = definePreset({'comparison?': '"simplified" | "strict"'}, pa
5460
5561 return expected
5662} )
57-
58- export function stripTypes ( typeScriptCode : string , dependencies : PresetDependencies ) {
59- const { babelParser, babelTraverse, babelGenerator} = dependencies
60- const ast = babelParser . parse ( typeScriptCode , {
61- sourceType : 'unambiguous' ,
62- plugins : [ 'typescript' ] ,
63- } )
64-
65- babelTraverse . default ( ast , {
66- // Remove type annotations from function parameters
67- Function ( path ) {
68- if ( path . node . params ) {
69- path . node . params . forEach ( param => {
70- if ( 'typeAnnotation' in param ) {
71- param . typeAnnotation = null
72- }
73- } )
74- }
75- // Remove return type annotation
76- if ( path . node . returnType ) {
77- path . node . returnType = null
78- }
79- } ,
80-
81- // Remove type annotations from variable declarations
82- VariableDeclarator ( path ) {
83- if ( path . node . id && 'typeAnnotation' in path . node . id ) {
84- path . node . id . typeAnnotation = null
85- }
86- } ,
87-
88- // Handle satisfies expressions
89- TSSatisfiesExpression ( path ) {
90- path . replaceWith ( path . node . expression )
91- } ,
92-
93- // Handle type assertions
94- TSAsExpression ( path ) {
95- path . replaceWith ( path . node . expression )
96- } ,
97-
98- TSTypeAssertion ( path ) {
99- path . replaceWith ( path . node . expression )
100- } ,
101-
102- // Remove type-only imports/exports
103- ImportDeclaration ( path ) {
104- if ( path . node . importKind === 'type' ) {
105- path . remove ( )
106- }
107- } ,
108-
109- ExportNamedDeclaration ( path ) {
110- if ( path . node . exportKind === 'type' ) {
111- path . remove ( )
112- }
113- } ,
114-
115- // Remove type declarations entirely
116- TSTypeAliasDeclaration ( path ) {
117- path . remove ( )
118- } ,
119-
120- TSInterfaceDeclaration ( path ) {
121- path . remove ( )
122- } ,
123-
124- TSEnumDeclaration ( path ) {
125- throw new Error (
126- `Can't strip enums. Please don't use them. Found:\n\n${ babelGenerator . default ( path . node , { comments : true } ) . code } ` ,
127- )
128- } ,
129-
130- ClassMethod ( path ) {
131- if ( path . node . kind === 'constructor' ) {
132- path . node . params . forEach ( param => {
133- if ( 'accessibility' in param || 'readonly' in param ) {
134- throw new Error (
135- `Can't strip parameter properties (public/private/protected/readonly). Please don't use them. Found:\n\n${ babelGenerator . default ( path . node , { comments : true } ) . code } ` ,
136- )
137- }
138- } )
139- }
140- } ,
141-
142- TSModuleDeclaration ( path ) {
143- path . remove ( )
144- } ,
145-
146- // Remove non-null assertions (!)
147- TSNonNullExpression ( path ) {
148- path . replaceWith ( path . node . expression )
149- } ,
150-
151- // Remove type parameters from functions/classes
152- TSTypeParameterDeclaration ( path ) {
153- path . remove ( )
154- } ,
155-
156- // Remove type parameters from call expressions
157- CallExpression ( path ) {
158- if ( path . node . typeParameters ) {
159- path . node . typeParameters = null
160- }
161- } ,
162-
163- NewExpression ( path ) {
164- if ( path . node . typeParameters ) {
165- path . node . typeParameters = null
166- }
167- } ,
168-
169- // Remove definite assignment assertions (!)
170- AssignmentPattern ( path ) {
171- if ( path . node . left && 'definite' in path . node . left && path . node . left . definite ) {
172- path . node . left . definite = false
173- }
174- } ,
175- } )
176-
177- return babelGenerator . default ( ast , { comments : true } ) . code
178- }
0 commit comments