Skip to content

Commit

Permalink
Remove default acorn options + other fixes (#4786)
Browse files Browse the repository at this point in the history
* Remove default acorn options

* Make tryParse private

* Add type

* Remove type assertion

* Remove non-null assertion operator

* Use more nullish coalescing

* Remove unused eslint parser options

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Jan 5, 2023
1 parent 6a29327 commit 67487b3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ module.exports = {
}
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/consistent-type-assertions': [
Expand Down
1 change: 0 additions & 1 deletion src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export default class Bundle {
if ('code' in file) {
try {
this.graph.contextParse(file.code, {
allowHashBang: true,
ecmaVersion: 'latest'
});
} catch (error_: any) {
Expand Down
20 changes: 10 additions & 10 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,12 +771,12 @@ export default class Module {
this.transformDependencies = transformDependencies;
this.customTransformCache = customTransformCache;
this.updateOptions(moduleOptions);
const moduleAst = ast || this.tryParse();
const moduleAst = ast ?? this.tryParse();

timeEnd('generate ast', 3);
timeStart('analyze ast', 3);

this.resolvedIds = resolvedIds || Object.create(null);
this.resolvedIds = resolvedIds ?? Object.create(null);

// By default, `id` is the file name. Custom resolvers and loaders
// can change that, but it makes sense to use it for the source file name
Expand Down Expand Up @@ -910,14 +910,6 @@ export default class Module {
return null;
}

tryParse(): acorn.Node {
try {
return this.graph.contextParse(this.info.code!);
} catch (error_: any) {
return this.error(errorParseError(error_, this.id), error_.pos);
}
}

updateOptions({
meta,
moduleSideEffects,
Expand Down Expand Up @@ -1259,6 +1251,14 @@ export default class Module {
this.options.onwarn(errorShimmedExport(this.id, name));
this.exports.set(name, MISSING_EXPORT_SHIM_DESCRIPTION);
}

private tryParse(): acorn.Node {
try {
return this.graph.contextParse(this.info.code!);
} catch (error_: any) {
return this.error(errorParseError(error_, this.id), error_.pos);
}
}
}

// if there is a cyclic import in the reexport chain, we should not
Expand Down
2 changes: 1 addition & 1 deletion src/utils/PluginDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export class PluginDriver {
replaceContext?: ReplaceContext | null
): Promise<unknown> {
// We always filter for plugins that support the hook before running it
const hook = plugin[hookName]!;
const hook = plugin[hookName];
const handler = typeof hook === 'object' ? hook.handler : hook;

let context = this.pluginContexts.get(plugin)!;
Expand Down
13 changes: 5 additions & 8 deletions src/utils/options/normalizeInputOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ const getOnwarn = (config: InputOptions): NormalizedInputOptions['onwarn'] => {
};

const getAcorn = (config: InputOptions): acorn.Options => ({
allowAwaitOutsideFunction: true,
ecmaVersion: 'latest',
preserveParens: false,
sourceType: 'module',
...config.acorn
});
Expand Down Expand Up @@ -208,19 +206,18 @@ const getModuleContext = (
config: InputOptions,
context: string
): NormalizedInputOptions['moduleContext'] => {
const configModuleContext = config.moduleContext as
| ((id: string) => string | null | undefined)
| { [id: string]: string }
| undefined;
const configModuleContext = config.moduleContext;
if (typeof configModuleContext === 'function') {
return id => configModuleContext(id) ?? context;
}
if (configModuleContext) {
const contextByModuleId = Object.create(null);
const contextByModuleId: {
[key: string]: string;
} = Object.create(null);
for (const [key, moduleContext] of Object.entries(configModuleContext)) {
contextByModuleId[resolve(key)] = moduleContext;
}
return id => contextByModuleId[id] || context;
return id => contextByModuleId[id] ?? context;
}
return () => context;
};
Expand Down
2 changes: 0 additions & 2 deletions test/function/samples/options-hook/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ module.exports = {
buildStart(options) {
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), {
acorn: {
allowAwaitOutsideFunction: true,
ecmaVersion: 'latest',
preserveParens: false,
sourceType: 'module'
},
acornInjectPlugins: [null],
Expand Down

0 comments on commit 67487b3

Please sign in to comment.