Skip to content

Commit 67487b3

Browse files
Remove default acorn options + other fixes (#4786)
* 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 <[email protected]>
1 parent 6a29327 commit 67487b3

6 files changed

Lines changed: 16 additions & 26 deletions

File tree

.eslintrc.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ module.exports = {
5050
}
5151
],
5252
parser: '@typescript-eslint/parser',
53-
parserOptions: {
54-
ecmaVersion: 2018,
55-
sourceType: 'module'
56-
},
5753
plugins: ['@typescript-eslint'],
5854
rules: {
5955
'@typescript-eslint/consistent-type-assertions': [

src/Bundle.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ export default class Bundle {
142142
if ('code' in file) {
143143
try {
144144
this.graph.contextParse(file.code, {
145-
allowHashBang: true,
146145
ecmaVersion: 'latest'
147146
});
148147
} catch (error_: any) {

src/Module.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,12 @@ export default class Module {
771771
this.transformDependencies = transformDependencies;
772772
this.customTransformCache = customTransformCache;
773773
this.updateOptions(moduleOptions);
774-
const moduleAst = ast || this.tryParse();
774+
const moduleAst = ast ?? this.tryParse();
775775

776776
timeEnd('generate ast', 3);
777777
timeStart('analyze ast', 3);
778778

779-
this.resolvedIds = resolvedIds || Object.create(null);
779+
this.resolvedIds = resolvedIds ?? Object.create(null);
780780

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

913-
tryParse(): acorn.Node {
914-
try {
915-
return this.graph.contextParse(this.info.code!);
916-
} catch (error_: any) {
917-
return this.error(errorParseError(error_, this.id), error_.pos);
918-
}
919-
}
920-
921913
updateOptions({
922914
meta,
923915
moduleSideEffects,
@@ -1259,6 +1251,14 @@ export default class Module {
12591251
this.options.onwarn(errorShimmedExport(this.id, name));
12601252
this.exports.set(name, MISSING_EXPORT_SHIM_DESCRIPTION);
12611253
}
1254+
1255+
private tryParse(): acorn.Node {
1256+
try {
1257+
return this.graph.contextParse(this.info.code!);
1258+
} catch (error_: any) {
1259+
return this.error(errorParseError(error_, this.id), error_.pos);
1260+
}
1261+
}
12621262
}
12631263

12641264
// if there is a cyclic import in the reexport chain, we should not

src/utils/PluginDriver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export class PluginDriver {
306306
replaceContext?: ReplaceContext | null
307307
): Promise<unknown> {
308308
// We always filter for plugins that support the hook before running it
309-
const hook = plugin[hookName]!;
309+
const hook = plugin[hookName];
310310
const handler = typeof hook === 'object' ? hook.handler : hook;
311311

312312
let context = this.pluginContexts.get(plugin)!;

src/utils/options/normalizeInputOptions.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ const getOnwarn = (config: InputOptions): NormalizedInputOptions['onwarn'] => {
9696
};
9797

9898
const getAcorn = (config: InputOptions): acorn.Options => ({
99-
allowAwaitOutsideFunction: true,
10099
ecmaVersion: 'latest',
101-
preserveParens: false,
102100
sourceType: 'module',
103101
...config.acorn
104102
});
@@ -208,19 +206,18 @@ const getModuleContext = (
208206
config: InputOptions,
209207
context: string
210208
): NormalizedInputOptions['moduleContext'] => {
211-
const configModuleContext = config.moduleContext as
212-
| ((id: string) => string | null | undefined)
213-
| { [id: string]: string }
214-
| undefined;
209+
const configModuleContext = config.moduleContext;
215210
if (typeof configModuleContext === 'function') {
216211
return id => configModuleContext(id) ?? context;
217212
}
218213
if (configModuleContext) {
219-
const contextByModuleId = Object.create(null);
214+
const contextByModuleId: {
215+
[key: string]: string;
216+
} = Object.create(null);
220217
for (const [key, moduleContext] of Object.entries(configModuleContext)) {
221218
contextByModuleId[resolve(key)] = moduleContext;
222219
}
223-
return id => contextByModuleId[id] || context;
220+
return id => contextByModuleId[id] ?? context;
224221
}
225222
return () => context;
226223
};

test/function/samples/options-hook/_config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ module.exports = {
1010
buildStart(options) {
1111
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), {
1212
acorn: {
13-
allowAwaitOutsideFunction: true,
1413
ecmaVersion: 'latest',
15-
preserveParens: false,
1614
sourceType: 'module'
1715
},
1816
acornInjectPlugins: [null],

0 commit comments

Comments
 (0)