acorn
- Version 8.14.0
- Published
- 547 kB
- No dependencies
- MIT license
Install
npm i acorn
yarn add acorn
pnpm add acorn
Overview
ECMAScript parser
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Variables
variable defaultOptions
const defaultOptions: Options;
variable tokTypes
const tokTypes: { num: TokenType; regexp: TokenType; string: TokenType; name: TokenType; privateId: TokenType; eof: TokenType; bracketL: TokenType; bracketR: TokenType; braceL: TokenType; braceR: TokenType; parenL: TokenType; parenR: TokenType; comma: TokenType; semi: TokenType; colon: TokenType; dot: TokenType; question: TokenType; questionDot: TokenType; arrow: TokenType; template: TokenType; invalidTemplate: TokenType; ellipsis: TokenType; backQuote: TokenType; dollarBraceL: TokenType; eq: TokenType; assign: TokenType; incDec: TokenType; prefix: TokenType; logicalOR: TokenType; logicalAND: TokenType; bitwiseOR: TokenType; bitwiseXOR: TokenType; bitwiseAND: TokenType; equality: TokenType; relational: TokenType; bitShift: TokenType; plusMin: TokenType; modulo: TokenType; star: TokenType; slash: TokenType; starstar: TokenType; coalesce: TokenType; _break: TokenType; _case: TokenType; _catch: TokenType; _continue: TokenType; _debugger: TokenType; _default: TokenType; _do: TokenType; _else: TokenType; _finally: TokenType; _for: TokenType; _function: TokenType; _if: TokenType; _return: TokenType; _switch: TokenType; _throw: TokenType; _try: TokenType; _var: TokenType; _const: TokenType; _while: TokenType; _with: TokenType; _new: TokenType; _this: TokenType; _super: TokenType; _class: TokenType; _extends: TokenType; _export: TokenType; _import: TokenType; _null: TokenType; _true: TokenType; _false: TokenType; _in: TokenType; _instanceof: TokenType; _typeof: TokenType; _void: TokenType; _delete: TokenType;};
variable version
const version: string;
Functions
function getLineInfo
getLineInfo: (input: string, offset: number) => Position;
function parse
parse: (input: string, options: Options) => Program;
function parseExpressionAt
parseExpressionAt: (input: string, pos: number, options: Options) => Expression;
function tokenizer
tokenizer: ( input: string, options: Options) => { getToken(): Token; [Symbol.iterator](): Iterator<Token> };
Classes
class Parser
class Parser {}
constructor
protected constructor(options: Options, input: string, startPos?: number);
property input
input: string;
property options
options: Options;
method extend
static extend: ( ...plugins: ((BaseParser: typeof Parser) => typeof Parser)[]) => typeof Parser;
method parse
static parse: (input: string, options: Options) => Program;
method parseExpressionAt
static parseExpressionAt: ( input: string, pos: number, options: Options) => Expression;
method tokenizer
static tokenizer: ( input: string, options: Options) => { getToken(): Token; [Symbol.iterator](): Iterator<Token> };
class Token
class Token {}
Interfaces
interface AnonymousClassDeclaration
interface AnonymousClassDeclaration extends Class {}
interface AnonymousFunctionDeclaration
interface AnonymousFunctionDeclaration extends Function {}
interface ArrayExpression
interface ArrayExpression extends Node {}
interface ArrayPattern
interface ArrayPattern extends Node {}
interface ArrowFunctionExpression
interface ArrowFunctionExpression extends Function {}
property type
type: 'ArrowFunctionExpression';
interface AssignmentExpression
interface AssignmentExpression extends Node {}
interface AssignmentPattern
interface AssignmentPattern extends Node {}
interface AssignmentProperty
interface AssignmentProperty extends Node {}
interface AwaitExpression
interface AwaitExpression extends Node {}
interface BinaryExpression
interface BinaryExpression extends Node {}
interface BlockStatement
interface BlockStatement extends Node {}
interface BreakStatement
interface BreakStatement extends Node {}
interface CallExpression
interface CallExpression extends Node {}
interface CatchClause
interface CatchClause extends Node {}
interface ChainExpression
interface ChainExpression extends Node {}
property expression
expression: MemberExpression | CallExpression;
property type
type: 'ChainExpression';
interface Class
interface Class extends Node {}
property body
body: ClassBody;
property id
id?: Identifier | null;
property superClass
superClass?: Expression | null;
interface ClassBody
interface ClassBody extends Node {}
interface ClassDeclaration
interface ClassDeclaration extends Class {}
interface ClassExpression
interface ClassExpression extends Class {}
property type
type: 'ClassExpression';
interface Comment
interface Comment {}
interface ConditionalExpression
interface ConditionalExpression extends Node {}
property alternate
alternate: Expression;
property consequent
consequent: Expression;
property test
test: Expression;
property type
type: 'ConditionalExpression';
interface ContinueStatement
interface ContinueStatement extends Node {}
interface DebuggerStatement
interface DebuggerStatement extends Node {}
property type
type: 'DebuggerStatement';
interface DoWhileStatement
interface DoWhileStatement extends Node {}
interface EmptyStatement
interface EmptyStatement extends Node {}
property type
type: 'EmptyStatement';
interface ExportAllDeclaration
interface ExportAllDeclaration extends Node {}
property attributes
attributes: Array<ImportAttribute>;
property exported
exported?: Identifier | Literal | null;
property source
source: Literal;
property type
type: 'ExportAllDeclaration';
interface ExportDefaultDeclaration
interface ExportDefaultDeclaration extends Node {}
property declaration
declaration: | AnonymousFunctionDeclaration | FunctionDeclaration | AnonymousClassDeclaration | ClassDeclaration | Expression;
property type
type: 'ExportDefaultDeclaration';
interface ExportNamedDeclaration
interface ExportNamedDeclaration extends Node {}
property attributes
attributes: Array<ImportAttribute>;
property declaration
declaration?: Declaration | null;
property source
source?: Literal | null;
property specifiers
specifiers: Array<ExportSpecifier>;
property type
type: 'ExportNamedDeclaration';
interface ExportSpecifier
interface ExportSpecifier extends Node {}
interface ExpressionStatement
interface ExpressionStatement extends Node {}
property directive
directive?: string;
property expression
expression: Expression | Literal;
property type
type: 'ExpressionStatement';
interface ForInStatement
interface ForInStatement extends Node {}
interface ForOfStatement
interface ForOfStatement extends Node {}
interface ForStatement
interface ForStatement extends Node {}
interface Function
interface Function extends Node {}
interface FunctionDeclaration
interface FunctionDeclaration extends Function {}
interface FunctionExpression
interface FunctionExpression extends Function {}
interface Identifier
interface Identifier extends Node {}
interface IfStatement
interface IfStatement extends Node {}
property alternate
alternate?: Statement | null;
property consequent
consequent: Statement;
property test
test: Expression;
property type
type: 'IfStatement';
interface ImportAttribute
interface ImportAttribute extends Node {}
interface ImportDeclaration
interface ImportDeclaration extends Node {}
property attributes
attributes: Array<ImportAttribute>;
property source
source: Literal;
property specifiers
specifiers: Array< ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
property type
type: 'ImportDeclaration';
interface ImportDefaultSpecifier
interface ImportDefaultSpecifier extends Node {}
interface ImportExpression
interface ImportExpression extends Node {}
interface ImportNamespaceSpecifier
interface ImportNamespaceSpecifier extends Node {}
interface ImportSpecifier
interface ImportSpecifier extends Node {}
interface LabeledStatement
interface LabeledStatement extends Node {}
interface Literal
interface Literal extends Node {}
interface LogicalExpression
interface LogicalExpression extends Node {}
interface MemberExpression
interface MemberExpression extends Node {}
interface MetaProperty
interface MetaProperty extends Node {}
interface MethodDefinition
interface MethodDefinition extends Node {}
interface NewExpression
interface NewExpression extends Node {}
interface Node
interface Node {}
interface ObjectExpression
interface ObjectExpression extends Node {}
property properties
properties: Array<Property | SpreadElement>;
property type
type: 'ObjectExpression';
interface ObjectPattern
interface ObjectPattern extends Node {}
property properties
properties: Array<AssignmentProperty | RestElement>;
property type
type: 'ObjectPattern';
interface Options
interface Options {}
property allowAwaitOutsideFunction
allowAwaitOutsideFunction?: boolean;
By default,
await
identifiers are allowed to appear at the top-level scope only if ecmaVersion >= 2022. When enabled, await identifiers are allowed to appear at the top-level scope, but they are still not allowed in non-async functions.
property allowHashBang
allowHashBang?: boolean;
When enabled, hashbang directive in the beginning of file is allowed and treated as a line comment. Enabled by default when ecmaVersion >= 2023.
property allowImportExportEverywhere
allowImportExportEverywhere?: boolean;
When enabled, import/export statements are not constrained to appearing at the top of the program, and an import.meta expression in a script isn't considered an error.
property allowReserved
allowReserved?: boolean | 'never';
By default, reserved words are only enforced if ecmaVersion >= 5. Set
allowReserved
to a boolean value to explicitly turn this on an off. When this option has the value "never", reserved words and keywords can also not be used as property names.
property allowReturnOutsideFunction
allowReturnOutsideFunction?: boolean;
When enabled, a return at the top level is not considered an error.
property allowSuperOutsideMethod
allowSuperOutsideMethod?: boolean;
When enabled, super identifiers are not constrained to appearing in methods and do not raise an error when they appear elsewhere.
property checkPrivateFields
checkPrivateFields?: boolean;
By default, the parser will verify that private properties are only used in places where they are valid and have been declared. Set this to false to turn such checks off.
property directSourceFile
directSourceFile?: string;
This value, if given, is stored in every node, whether locations is on or off.
property ecmaVersion
ecmaVersion: ecmaVersion;
ecmaVersion
indicates the ECMAScript version to parse. Can be a number, either in year (2022
) or plain version number (6
) form, or"latest"
(the latest the library supports). This influences support for strict mode, the set of reserved words, and support for new syntax features.
property locations
locations?: boolean;
When
locations
is on,loc
properties holding objects withstart
andend
properties as Position objects will be attached to the nodes.
property onComment
onComment?: | (( isBlock: boolean, text: string, start: number, end: number, startLoc?: Position, endLoc?: Position ) => void) | Comment[];
This takes a export function or an array.
When a export function is passed, Acorn will call that export function with `(block, text, start, end)
parameters whenever a comment is skipped.
block` is a boolean indicating whether this is a block (/* *\/
) comment,text
is the content of the comment, andstart
andend
are character offsets that denote the start and end of the comment. When the locations option is on, two more parameters are passed, the full locations of Position export type of the start and end of the comments.When a array is passed, each found comment of Comment export type is pushed to the array.
Note that you are not allowed to call the parser from the callback—that will corrupt its internal state.
property onInsertedSemicolon
onInsertedSemicolon?: (lastTokEnd: number, lastTokEndLoc?: Position) => void;
a callback that will be called when a semicolon is automatically inserted.
Parameter lastTokEnd
the position of the comma as an offset
Parameter lastTokEndLoc
location if locations is enabled
property onToken
onToken?: ((token: Token) => void) | Token[];
a callback that will cause Acorn to call that export function with object in the same format as tokens returned from
tokenizer().getToken()
. Note that you are not allowed to call the parser from the callback—that will corrupt its internal state.
property onTrailingComma
onTrailingComma?: (lastTokEnd: number, lastTokEndLoc?: Position) => void;
similar to
onInsertedSemicolon
, but for trailing commasParameter lastTokEnd
the position of the comma as an offset
Parameter lastTokEndLoc
location if
locations
is enabled
property preserveParens
preserveParens?: boolean;
When enabled, parenthesized expressions are represented by (non-standard) ParenthesizedExpression nodes
property program
program?: Node;
It is possible to parse multiple files into a single AST by passing the tree produced by parsing the first file as
program
option in subsequent parses. This will add the toplevel forms of the parsed file to theProgram
(top) node of an existing parse tree.
property ranges
ranges?: boolean;
Nodes have their start and end characters offsets recorded in
start
andend
properties (directly on the node, rather than theloc
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 totrue
.
property sourceFile
sourceFile?: string;
When locations is on, you can pass this to record the source file in every node's
loc
object.
property sourceType
sourceType?: 'script' | 'module';
sourceType
indicates the mode the code should be parsed in. Can be either"script"
or"module"
. This influences global strict mode and parsing ofimport
andexport
declarations.
interface ParenthesizedExpression
interface ParenthesizedExpression extends Node {}
property expression
expression: Expression;
property type
type: 'ParenthesizedExpression';
interface Position
interface Position {}
interface PrivateIdentifier
interface PrivateIdentifier extends Node {}
interface Program
interface Program extends Node {}
property body
body: Array<Statement | ModuleDeclaration>;
property sourceType
sourceType: 'script' | 'module';
property type
type: 'Program';
interface Property
interface Property extends Node {}
interface PropertyDefinition
interface PropertyDefinition extends Node {}
interface RestElement
interface RestElement extends Node {}
interface ReturnStatement
interface ReturnStatement extends Node {}
interface SequenceExpression
interface SequenceExpression extends Node {}
property expressions
expressions: Array<Expression>;
property type
type: 'SequenceExpression';
interface SourceLocation
interface SourceLocation {}
interface SpreadElement
interface SpreadElement extends Node {}
interface StaticBlock
interface StaticBlock extends Node {}
interface SwitchCase
interface SwitchCase extends Node {}
property consequent
consequent: Array<Statement>;
property test
test?: Expression | null;
property type
type: 'SwitchCase';
interface SwitchStatement
interface SwitchStatement extends Node {}
property cases
cases: Array<SwitchCase>;
property discriminant
discriminant: Expression;
property type
type: 'SwitchStatement';
interface TaggedTemplateExpression
interface TaggedTemplateExpression extends Node {}
interface TemplateElement
interface TemplateElement extends Node {}
interface TemplateLiteral
interface TemplateLiteral extends Node {}
property expressions
expressions: Array<Expression>;
property quasis
quasis: Array<TemplateElement>;
property type
type: 'TemplateLiteral';
interface ThisExpression
interface ThisExpression extends Node {}
property type
type: 'ThisExpression';
interface ThrowStatement
interface ThrowStatement extends Node {}
interface TryStatement
interface TryStatement extends Node {}
interface UnaryExpression
interface UnaryExpression extends Node {}
interface UpdateExpression
interface UpdateExpression extends Node {}
interface VariableDeclaration
interface VariableDeclaration extends Node {}
property declarations
declarations: Array<VariableDeclarator>;
property kind
kind: 'var' | 'let' | 'const';
property type
type: 'VariableDeclaration';
interface VariableDeclarator
interface VariableDeclarator extends Node {}
interface WhileStatement
interface WhileStatement extends Node {}
interface WithStatement
interface WithStatement extends Node {}
interface YieldExpression
interface YieldExpression extends Node {}
Type Aliases
type AnyNode
type AnyNode = | Statement | Expression | Declaration | ModuleDeclaration | Literal | Program | SwitchCase | CatchClause | Property | Super | SpreadElement | TemplateElement | AssignmentProperty | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern | ClassBody | MethodDefinition | MetaProperty | ImportAttribute | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier | AnonymousFunctionDeclaration | AnonymousClassDeclaration | PropertyDefinition | PrivateIdentifier | StaticBlock | VariableDeclarator;
type AssignmentOperator
type AssignmentOperator = | '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '<<=' | '>>=' | '>>>=' | '|=' | '^=' | '&=' | '**=' | '||=' | '&&=' | '??=';
type BinaryOperator
type BinaryOperator = | '==' | '!=' | '===' | '!==' | '<' | '<=' | '>' | '>=' | '<<' | '>>' | '>>>' | '+' | '-' | '*' | '/' | '%' | '|' | '^' | '&' | 'in' | 'instanceof' | '**';
type Declaration
type Declaration = FunctionDeclaration | VariableDeclaration | ClassDeclaration;
type ecmaVersion
type ecmaVersion = | 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 'latest';
type Expression
type Expression = | Identifier | Literal | ThisExpression | ArrayExpression | ObjectExpression | FunctionExpression | UnaryExpression | UpdateExpression | BinaryExpression | AssignmentExpression | LogicalExpression | MemberExpression | ConditionalExpression | CallExpression | NewExpression | SequenceExpression | ArrowFunctionExpression | YieldExpression | TemplateLiteral | TaggedTemplateExpression | ClassExpression | MetaProperty | AwaitExpression | ChainExpression | ImportExpression | ParenthesizedExpression;
type LogicalOperator
type LogicalOperator = '||' | '&&' | '??';
type ModuleDeclaration
type ModuleDeclaration = | ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration | ExportAllDeclaration;
type Pattern
type Pattern = | Identifier | MemberExpression | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern;
type Statement
type Statement = | ExpressionStatement | BlockStatement | EmptyStatement | DebuggerStatement | WithStatement | ReturnStatement | LabeledStatement | BreakStatement | ContinueStatement | IfStatement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement | DoWhileStatement | ForStatement | ForInStatement | ForOfStatement | Declaration;
type UnaryOperator
type UnaryOperator = '-' | '+' | '!' | '~' | 'typeof' | 'void' | 'delete';
type UpdateOperator
type UpdateOperator = '++' | '--';
Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/acorn
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/acorn)
- HTML<a href="https://www.jsdocs.io/package/acorn"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 6680 ms. - Missing or incorrect documentation? Open an issue for this package.