Skip to content

Commit d415077

Browse files
committedMay 17, 2023
fix(core): file parsing handling esnext correctly
1 parent a40f49d commit d415077

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed
 

‎packages/core/src/generators/mutator.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Parser } from 'acorn';
1+
import acorn, { Parser } from 'acorn';
22
import chalk from 'chalk';
33
import fs from 'fs-extra';
44
import {
55
GeneratorMutator,
66
GeneratorMutatorParsingInfo,
77
NormalizedMutator,
88
Tsconfig,
9+
TsConfigTarget,
910
} from '../types';
1011
import { createLogger, getFileInfo, loadFile, pascal, upath } from '../utils';
1112

@@ -69,7 +70,12 @@ export const generateMutator = async ({
6970

7071
if (file) {
7172
const mutatorInfoName = isDefault ? 'default' : mutator.name!;
72-
const mutatorInfo = parseFile(file, mutatorInfoName);
73+
74+
const mutatorInfo = parseFile(
75+
file,
76+
mutatorInfoName,
77+
getEcmaVersion(tsconfig?.compilerOptions?.target),
78+
);
7379

7480
if (!mutatorInfo) {
7581
createLogger().error(
@@ -122,12 +128,31 @@ export const generateMutator = async ({
122128
}
123129
};
124130

131+
const getEcmaVersion = (
132+
target?: TsConfigTarget,
133+
): acorn.ecmaVersion | undefined => {
134+
if (!target) {
135+
return;
136+
}
137+
138+
if (target.toLowerCase() === 'esnext') {
139+
return 'latest';
140+
}
141+
142+
try {
143+
return Number(target.toLowerCase().replace('es', '')) as acorn.ecmaVersion;
144+
} catch {
145+
return;
146+
}
147+
};
148+
125149
const parseFile = (
126150
file: string,
127151
name: string,
152+
ecmaVersion: acorn.ecmaVersion = 6,
128153
): GeneratorMutatorParsingInfo | undefined => {
129154
try {
130-
const ast = Parser.parse(file, { ecmaVersion: 6 }) as any;
155+
const ast = Parser.parse(file, { ecmaVersion }) as any;
131156

132157
const node = ast?.body?.find((childNode: any) => {
133158
if (childNode.type === 'ExpressionStatement') {

‎packages/core/src/types.ts

+15
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,24 @@ export interface Tsconfig {
410410
allowSyntheticDefaultImports?: boolean;
411411
exactOptionalPropertyTypes?: boolean;
412412
paths?: Record<string, string[]>;
413+
target?: TsConfigTarget;
413414
};
414415
}
415416

417+
export type TsConfigTarget =
418+
| 'es3'
419+
| 'es5'
420+
| 'es6'
421+
| 'es2015'
422+
| 'es2016'
423+
| 'es2017'
424+
| 'es2018'
425+
| 'es2019'
426+
| 'es2020'
427+
| 'es2021'
428+
| 'es2022'
429+
| 'esnext'; // https://www.typescriptlang.org/tsconfig#target
430+
416431
export interface PackageJson {
417432
dependencies?: Record<string, string>;
418433
devDependencies?: Record<string, string>;

‎packages/core/src/utils/file.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,13 @@ async function bundleFile(
210210
format: mjs ? 'esm' : 'cjs',
211211
sourcemap: 'inline',
212212
metafile: true,
213-
target: 'es6',
214-
minifyWhitespace: true,
213+
target: compilerOptions?.target || 'es6',
214+
minify: false,
215+
minifyIdentifiers: false,
216+
minifySyntax: false,
217+
minifyWhitespace: false,
218+
treeShaking: false,
219+
keepNames: false,
215220
plugins: [
216221
...(alias || compilerOptions?.paths
217222
? [

‎tests/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"include": ["generated/**/*", "regressions/**/*"],
44
"compilerOptions": {
55
"skipLibCheck": true,
6-
"target": "es5",
6+
"target": "es6",
77
"module": "commonjs",
88
"lib": ["es2019", "dom", "es2016.array.include", "es2017.object"],
99
"declaration": true,

1 commit comments

Comments
 (1)

vercel[bot] commented on May 17, 2023

@vercel[bot]
Please sign in to comment.