From d415077b4b186949c2b833127398f77748f32cfa Mon Sep 17 00:00:00 2001 From: anymaniax Date: Wed, 17 May 2023 11:52:49 +0200 Subject: [PATCH] fix(core): file parsing handling esnext correctly --- packages/core/src/generators/mutator.ts | 31 ++++++++++++++++++++++--- packages/core/src/types.ts | 15 ++++++++++++ packages/core/src/utils/file.ts | 9 +++++-- tests/tsconfig.json | 2 +- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/packages/core/src/generators/mutator.ts b/packages/core/src/generators/mutator.ts index 5586af9cf..a765775b2 100644 --- a/packages/core/src/generators/mutator.ts +++ b/packages/core/src/generators/mutator.ts @@ -1,4 +1,4 @@ -import { Parser } from 'acorn'; +import acorn, { Parser } from 'acorn'; import chalk from 'chalk'; import fs from 'fs-extra'; import { @@ -6,6 +6,7 @@ import { GeneratorMutatorParsingInfo, NormalizedMutator, Tsconfig, + TsConfigTarget, } from '../types'; import { createLogger, getFileInfo, loadFile, pascal, upath } from '../utils'; @@ -69,7 +70,12 @@ export const generateMutator = async ({ if (file) { const mutatorInfoName = isDefault ? 'default' : mutator.name!; - const mutatorInfo = parseFile(file, mutatorInfoName); + + const mutatorInfo = parseFile( + file, + mutatorInfoName, + getEcmaVersion(tsconfig?.compilerOptions?.target), + ); if (!mutatorInfo) { createLogger().error( @@ -122,12 +128,31 @@ export const generateMutator = async ({ } }; +const getEcmaVersion = ( + target?: TsConfigTarget, +): acorn.ecmaVersion | undefined => { + if (!target) { + return; + } + + if (target.toLowerCase() === 'esnext') { + return 'latest'; + } + + try { + return Number(target.toLowerCase().replace('es', '')) as acorn.ecmaVersion; + } catch { + return; + } +}; + const parseFile = ( file: string, name: string, + ecmaVersion: acorn.ecmaVersion = 6, ): GeneratorMutatorParsingInfo | undefined => { try { - const ast = Parser.parse(file, { ecmaVersion: 6 }) as any; + const ast = Parser.parse(file, { ecmaVersion }) as any; const node = ast?.body?.find((childNode: any) => { if (childNode.type === 'ExpressionStatement') { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index df33b9d4d..fa7bb80cd 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -410,9 +410,24 @@ export interface Tsconfig { allowSyntheticDefaultImports?: boolean; exactOptionalPropertyTypes?: boolean; paths?: Record; + target?: TsConfigTarget; }; } +export type TsConfigTarget = + | 'es3' + | 'es5' + | 'es6' + | 'es2015' + | 'es2016' + | 'es2017' + | 'es2018' + | 'es2019' + | 'es2020' + | 'es2021' + | 'es2022' + | 'esnext'; // https://www.typescriptlang.org/tsconfig#target + export interface PackageJson { dependencies?: Record; devDependencies?: Record; diff --git a/packages/core/src/utils/file.ts b/packages/core/src/utils/file.ts index 8a4dee98a..8f1fb2588 100644 --- a/packages/core/src/utils/file.ts +++ b/packages/core/src/utils/file.ts @@ -210,8 +210,13 @@ async function bundleFile( format: mjs ? 'esm' : 'cjs', sourcemap: 'inline', metafile: true, - target: 'es6', - minifyWhitespace: true, + target: compilerOptions?.target || 'es6', + minify: false, + minifyIdentifiers: false, + minifySyntax: false, + minifyWhitespace: false, + treeShaking: false, + keepNames: false, plugins: [ ...(alias || compilerOptions?.paths ? [ diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 292075ace..38ff6822c 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -3,7 +3,7 @@ "include": ["generated/**/*", "regressions/**/*"], "compilerOptions": { "skipLibCheck": true, - "target": "es5", + "target": "es6", "module": "commonjs", "lib": ["es2019", "dom", "es2016.array.include", "es2017.object"], "declaration": true,