Skip to content

Commit

Permalink
chore: Improve logic regarding fast objects (babel#16919)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored Oct 22, 2024
1 parent 876ea3b commit 0a613d6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 84 deletions.
126 changes: 64 additions & 62 deletions packages/babel-parser/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,76 @@ export interface Options {

type OptionsWithDefaults = { [P in keyof Options]-?: Options[P] };

export const defaultOptions: OptionsWithDefaults = {
// Source type ("script" or "module") for different semantics
sourceType: "script",
// Source filename.
sourceFilename: undefined,
// Column (0-based) from which to start counting source. Useful for
// integration with other tools.
startColumn: 0,
// Line (1-based) from which to start counting source. Useful for
// integration with other tools.
startLine: 1,
// When enabled, await at the top level is not considered an
// error.
allowAwaitOutsideFunction: false,
// When enabled, a return at the top level is not considered an
// error.
allowReturnOutsideFunction: false,
// When enabled, new.target outside a function or class is not
// considered an error.
allowNewTargetOutsideFunction: false,
// When enabled, import/export statements are not constrained to
// appearing at the top of the program.
allowImportExportEverywhere: false,
// TODO
allowSuperOutsideMethod: false,
// When enabled, export statements can reference undeclared variables.
allowUndeclaredExports: false,
// An array of plugins to enable
plugins: [],
// TODO
strictMode: null,
// Nodes have their start and end characters offsets recorded in
// `start` and `end` properties (directly on the node, rather than
// the `loc` object, which holds line/column data. To also add a
// [semi-standardized][range] `range` property holding a `[start,
// end]` array with the same numbers, set the `ranges` option to
// `true`.
//
// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
ranges: false,
// Adds all parsed tokens to a `tokens` property on the `File` node
tokens: false,
// Whether to create ImportExpression AST nodes (if false
// `import(foo)` will be parsed as CallExpression(Import, [Identifier(foo)])
createImportExpressions: process.env.BABEL_8_BREAKING ? true : false,
// Whether to create ParenthesizedExpression AST nodes (if false
// the parser sets extra.parenthesized on the expression nodes instead).
createParenthesizedExpressions: false,
// When enabled, errors are attached to the AST instead of being directly thrown.
// Some errors will still throw, because @babel/parser can't always recover.
errorRecovery: false,
// When enabled, comments will be attached to adjacent AST nodes as one of
// `leadingComments`, `trailingComments` and `innerComments`. The comment attachment
// is vital to preserve comments after transform. If you don't print AST back,
// consider set this option to `false` for performance
attachComment: true,
// When enabled, the parser will support Annex B syntax.
// https://tc39.es/ecma262/#sec-additional-ecmascript-features-for-web-browsers
annexB: true,
};
function createDefaultOptions(): OptionsWithDefaults {
return {
// Source type ("script" or "module") for different semantics
sourceType: "script",
// Source filename.
sourceFilename: undefined,
// Column (0-based) from which to start counting source. Useful for
// integration with other tools.
startColumn: 0,
// Line (1-based) from which to start counting source. Useful for
// integration with other tools.
startLine: 1,
// When enabled, await at the top level is not considered an
// error.
allowAwaitOutsideFunction: false,
// When enabled, a return at the top level is not considered an
// error.
allowReturnOutsideFunction: false,
// When enabled, new.target outside a function or class is not
// considered an error.
allowNewTargetOutsideFunction: false,
// When enabled, import/export statements are not constrained to
// appearing at the top of the program.
allowImportExportEverywhere: false,
// TODO
allowSuperOutsideMethod: false,
// When enabled, export statements can reference undeclared variables.
allowUndeclaredExports: false,
// An array of plugins to enable
plugins: [],
// TODO
strictMode: null,
// Nodes have their start and end characters offsets recorded in
// `start` and `end` properties (directly on the node, rather than
// the `loc` object, which holds line/column data. To also add a
// [semi-standardized][range] `range` property holding a `[start,
// end]` array with the same numbers, set the `ranges` option to
// `true`.
//
// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
ranges: false,
// Adds all parsed tokens to a `tokens` property on the `File` node
tokens: false,
// Whether to create ImportExpression AST nodes (if false
// `import(foo)` will be parsed as CallExpression(Import, [Identifier(foo)])
createImportExpressions: process.env.BABEL_8_BREAKING ? true : false,
// Whether to create ParenthesizedExpression AST nodes (if false
// the parser sets extra.parenthesized on the expression nodes instead).
createParenthesizedExpressions: false,
// When enabled, errors are attached to the AST instead of being directly thrown.
// Some errors will still throw, because @babel/parser can't always recover.
errorRecovery: false,
// When enabled, comments will be attached to adjacent AST nodes as one of
// `leadingComments`, `trailingComments` and `innerComments`. The comment attachment
// is vital to preserve comments after transform. If you don't print AST back,
// consider set this option to `false` for performance
attachComment: true,
// When enabled, the parser will support Annex B syntax.
// https://tc39.es/ecma262/#sec-additional-ecmascript-features-for-web-browsers
annexB: true,
};
}

// Interpret and default an options object

export function getOptions(opts?: Options | null): OptionsWithDefaults {
// https://github.com/babel/babel/pull/16918
// `options` is accessed frequently, please make sure it is a fast object.
// `%ToFastProperties` can make it a fast object, but the performance is the same as the slow object.
const options: any = { ...defaultOptions };
const options: any = createDefaultOptions();

if (opts == null) {
return options;
Expand All @@ -105,7 +107,7 @@ export function getOptions(opts?: Options | null): OptionsWithDefaults {
throw new Error("The `annexB` option can only be set to `false`.");
}

for (const key of Object.keys(defaultOptions) as (keyof Options)[]) {
for (const key of Object.keys(options) as (keyof Options)[]) {
if (opts[key] != null) options[key] = opts[key];
}

Expand Down
31 changes: 9 additions & 22 deletions packages/babel-types/src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,24 @@ import { DEPRECATED_ALIASES } from "./deprecated-aliases.ts";
FLIPPED_ALIAS_KEYS[DEPRECATED_ALIASES[deprecatedAlias]];
});

// We do this here, because at this point the visitor keys should be ready and setup
// `%ToFastProperties` no longer improves performance, so we use another approach.
const _VISITOR_KEYS = { ...VISITOR_KEYS };
const _ALIAS_KEYS = { ...ALIAS_KEYS };
const _FLIPPED_ALIAS_KEYS = { ...FLIPPED_ALIAS_KEYS };
const _NODE_FIELDS = { ...NODE_FIELDS };
const _BUILDER_KEYS = { ...BUILDER_KEYS };
const _DEPRECATED_KEYS = { ...DEPRECATED_KEYS };
const _NODE_PARENT_VALIDATIONS = { ...NODE_PARENT_VALIDATIONS };

const _PLACEHOLDERS_ALIAS = { ...PLACEHOLDERS_ALIAS };
const _PLACEHOLDERS_FLIPPED_ALIAS = { ...PLACEHOLDERS_FLIPPED_ALIAS };

const TYPES: Array<string> = [].concat(
Object.keys(VISITOR_KEYS),
Object.keys(FLIPPED_ALIAS_KEYS),
Object.keys(DEPRECATED_KEYS),
);

export {
_VISITOR_KEYS as VISITOR_KEYS,
_ALIAS_KEYS as ALIAS_KEYS,
_FLIPPED_ALIAS_KEYS as FLIPPED_ALIAS_KEYS,
_NODE_FIELDS as NODE_FIELDS,
_BUILDER_KEYS as BUILDER_KEYS,
VISITOR_KEYS,
ALIAS_KEYS,
FLIPPED_ALIAS_KEYS,
NODE_FIELDS,
BUILDER_KEYS,
DEPRECATED_ALIASES,
_DEPRECATED_KEYS as DEPRECATED_KEYS,
_NODE_PARENT_VALIDATIONS as NODE_PARENT_VALIDATIONS,
DEPRECATED_KEYS,
NODE_PARENT_VALIDATIONS,
PLACEHOLDERS,
_PLACEHOLDERS_ALIAS as PLACEHOLDERS_ALIAS,
_PLACEHOLDERS_FLIPPED_ALIAS as PLACEHOLDERS_FLIPPED_ALIAS,
PLACEHOLDERS_ALIAS,
PLACEHOLDERS_FLIPPED_ALIAS,
TYPES,
};

Expand Down

0 comments on commit 0a613d6

Please sign in to comment.