Skip to content

Commit

Permalink
fix(core): file parsing handling esnext correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed May 17, 2023
1 parent a40f49d commit d415077
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
31 changes: 28 additions & 3 deletions packages/core/src/generators/mutator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Parser } from 'acorn';
import acorn, { Parser } from 'acorn';
import chalk from 'chalk';
import fs from 'fs-extra';
import {
GeneratorMutator,
GeneratorMutatorParsingInfo,
NormalizedMutator,
Tsconfig,
TsConfigTarget,
} from '../types';
import { createLogger, getFileInfo, loadFile, pascal, upath } from '../utils';

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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') {
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,24 @@ export interface Tsconfig {
allowSyntheticDefaultImports?: boolean;
exactOptionalPropertyTypes?: boolean;
paths?: Record<string, string[]>;
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<string, string>;
devDependencies?: Record<string, string>;
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
? [
Expand Down
2 changes: 1 addition & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d415077

Please sign in to comment.