Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
607b37e
feat: typescript
brettz9 Oct 27, 2025
a924a79
Update packages/espree/package.json
brettz9 Dec 1, 2025
d85ab78
rename ParserOptions -> Options
brettz9 Dec 1, 2025
5c31699
rename ecmaVersion -> EcmaVersion; remove unused imports
brettz9 Dec 1, 2025
4988665
cast lastTemplateToken as (defined) acorn.Token
brettz9 Dec 1, 2025
9276b01
add tsd tests and inspired fixes (making arguments optional)
brettz9 Dec 1, 2025
c62f3a1
add further public exports
brettz9 Dec 1, 2025
b0ce158
tsd fixes
brettz9 Dec 1, 2025
1a75b30
Update packages/espree/package.json
brettz9 Dec 1, 2025
5af5b76
assume loc and range are defined
brettz9 Dec 1, 2025
a405d61
liberalize version string type
brettz9 Dec 1, 2025
7742d81
remove unused import
brettz9 Dec 1, 2025
7996a56
moving definitions to main export file; attempt to avoid lib folder
brettz9 Dec 1, 2025
233d7d8
Revert "moving definitions to main export file; attempt to avoid lib …
brettz9 Dec 3, 2025
086b897
inline BaseEsprimaToken
brettz9 Dec 3, 2025
b2350a0
delete dist/lib at end of build
brettz9 Dec 3, 2025
6a09b5c
check dist file types; make eslint-visitor-keys explicit to avoid bro…
brettz9 Dec 3, 2025
d73b6b0
remove dist/lib with evaluated script
brettz9 Dec 4, 2025
8a72a35
revert to stable acorn-jsx
brettz9 Dec 4, 2025
281e211
inline acorn-jsx types
brettz9 Dec 4, 2025
a12f55f
Update packages/espree/lib/espree.js
brettz9 Dec 8, 2025
1ee7bbf
switch from Integer to number
brettz9 Dec 9, 2025
6c50b1b
liberalize version
brettz9 Dec 23, 2025
942c70b
Update packages/espree/tsconfig.json
brettz9 Dec 23, 2025
9188d5d
Update packages/espree/tsconfig-cjs.json
brettz9 Dec 23, 2025
1f834d5
use bare import specifier
brettz9 Dec 23, 2025
277b720
set `getLatestEcmaVersion` to last `const` value in union
brettz9 Dec 23, 2025
50565bc
narrow parse return type to acorn.Program
brettz9 Dec 23, 2025
7b20583
reuse existing import for types
brettz9 Dec 23, 2025
81bca5f
double quotes
brettz9 Dec 24, 2025
46ff304
Update packages/espree/lib/espree.js
brettz9 Dec 25, 2025
a664f3f
Update packages/espree/espree.js
brettz9 Dec 25, 2025
8cd7135
Update packages/espree/lib/options.js
brettz9 Dec 25, 2025
33ac707
use `@import`
brettz9 Dec 25, 2025
2d9e38a
Update packages/espree/espree.js
brettz9 Dec 26, 2025
edf4744
normalizedEcmaVersion -> NormalizedEcmaVersion
brettz9 Dec 27, 2025
2b9725d
infer _R -> unknown[]
brettz9 Dec 27, 2025
d4a27f9
move JSDoc types after imports
brettz9 Dec 27, 2025
bcef526
use EcmaVersion internally to options.js function
brettz9 Dec 27, 2025
e58590e
revert EcmaVersion values and add need for updating to CONTRIBUTING
brettz9 Dec 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/espree/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Our full contribution guidelines can be found at:
# How to upgrade `acorn` to support new syntax

1. `npm install acorn@latest`
1. If a new `ecmaVersion` value is added, update `SUPPORTED_VERSIONS` constant in `lib/options.js` and tests in `tests/lib/supported-ecmaversions.js`.
1. If a new `ecmaVersion` value is added, update the `SUPPORTED_VERSIONS` constant in `lib/options.js`, the tests in `tests/lib/supported-ecmaversions.js`, and the `EcmaVersion` type.
1. If new token types are added, update `lib/token-translator.js` file to translate the tokens.
1. Add tests in `tests/fixtures/ecma-version/<ecma-vesion>/`.
- Add a directory named the new syntax name.
Expand Down
2 changes: 2 additions & 0 deletions packages/espree/espree.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as espree from "./espree.js";
export = espree;
138 changes: 128 additions & 10 deletions packages/espree/espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,145 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


import * as acorn from "acorn";
import jsx from "acorn-jsx";
import espree from "./lib/espree.js";
import * as visitorKeys from "eslint-visitor-keys";
import { getLatestEcmaVersion, getSupportedEcmaVersions } from "./lib/options.js";

/**
* @import { EspreeParserCtor, EspreeParserJsxCtor } from "./lib/types.js";
*/

// ----------------------------------------------------------------------------
// Types exported from file
// ----------------------------------------------------------------------------
/**
* @typedef {3|5|6|7|8|9|10|11|12|13|14|15|16|17|2015|2016|2017|2018|2019|2020|2021|2022|2023|2024|2025|2026|'latest'} EcmaVersion
*/

/**
* @typedef {{
* type: string;
* value: any;
* start?: number;
* end?: number;
* loc?: acorn.SourceLocation;
* range?: [number, number];
* regex?: {flags: string, pattern: string};
* }} EspreeToken
*/

/**
* @typedef {{
* type: "Block" | "Hashbang" | "Line",
* value: string,
* range?: [number, number],
* start?: number,
* end?: number,
* loc?: {
* start: acorn.Position | undefined,
* end: acorn.Position | undefined
* }
* }} EspreeComment
*/

/**
* @typedef {{
* comments?: EspreeComment[]
* } & EspreeToken[]} EspreeTokens
*/

/**
* `allowReserved` is as in `acorn.Options`
*
* `ecmaVersion` currently as in `acorn.Options` though optional
*
* `sourceType` as in `acorn.Options` but also allows `commonjs`
*
* `ecmaFeatures`, `range`, `loc`, `tokens` are not in `acorn.Options`
*
* `comment` is not in `acorn.Options` and doesn't err without it, but is used
*/
/**
* @typedef {{
* allowReserved?: boolean,
* ecmaVersion?: EcmaVersion,
* sourceType?: "script"|"module"|"commonjs",
* ecmaFeatures?: {
* jsx?: boolean,
* globalReturn?: boolean,
* impliedStrict?: boolean
* },
* range?: boolean,
* loc?: boolean,
* tokens?: boolean,
* comment?: boolean,
* }} Options
*/

// To initialize lazily.
const parsers = {

/** @type {EspreeParserCtor|null} */
_regular: null,

/** @type {EspreeParserJsxCtor|null} */
_jsx: null,

/**
* Returns regular Parser
* @returns {EspreeParserCtor} Regular Acorn parser
*/
get regular() {
if (this._regular === null) {
this._regular = acorn.Parser.extend(espree());
const espreeParserFactory = /** @type {unknown} */ (espree());

this._regular = /** @type {EspreeParserCtor} */ (
// Without conversion, types are incompatible, as
// acorn's has a protected constructor
/** @type {unknown} */
(acorn.Parser.extend(
/**
* @type {(
* BaseParser: typeof acorn.Parser
* ) => typeof acorn.Parser}
*/ (espreeParserFactory)
))
);
}
return this._regular;
},

/**
* Returns JSX Parser
* @returns {EspreeParserJsxCtor} JSX Acorn parser
*/
get jsx() {
if (this._jsx === null) {
this._jsx = acorn.Parser.extend(jsx(), espree());
const espreeParserFactory = /** @type {unknown} */ (espree());
const jsxFactory = jsx();

this._jsx = /** @type {EspreeParserJsxCtor} */ (
// Without conversion, types are incompatible, as
// acorn's has a protected constructor
/** @type {unknown} */
(acorn.Parser.extend(
jsxFactory,

/** @type {(BaseParser: typeof acorn.Parser) => typeof acorn.Parser} */
(espreeParserFactory)
))
);
}
return this._jsx;
},

/**
* Gets the parser object based on the supplied options.
* @param {Options} [options] The parser options.
* @returns {EspreeParserJsxCtor|EspreeParserCtor} Regular or JSX Acorn parser
*/
get(options) {
const useJsx = Boolean(
options &&
Expand All @@ -100,9 +212,9 @@ const parsers = {
/**
* Tokenizes the given code.
* @param {string} code The code to tokenize.
* @param {Object} options Options defining how to tokenize.
* @returns {Token[]} An array of tokens.
* @throws {SyntaxError} If the input code is invalid.
* @param {Options} [options] Options defining how to tokenize.
* @returns {EspreeTokens} An array of tokens.
* @throws {EnhancedSyntaxError} If the input code is invalid.
* @private
*/
export function tokenize(code, options) {
Expand All @@ -113,7 +225,7 @@ export function tokenize(code, options) {
options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign -- stylistic choice
}

return new Parser(options, code).tokenize();
return /** @type {EspreeTokens} */ (new Parser(options, code).tokenize());
}

//------------------------------------------------------------------------------
Expand All @@ -123,9 +235,9 @@ export function tokenize(code, options) {
/**
* Parses the given code.
* @param {string} code The code to tokenize.
* @param {Object} options Options defining how to tokenize.
* @returns {ASTNode} The "Program" AST node.
* @throws {SyntaxError} If the input code is invalid.
* @param {Options} [options] Options defining how to tokenize.
* @returns {acorn.Program} The "Program" AST node.
* @throws {EnhancedSyntaxError} If the input code is invalid.
*/
export function parse(code, options) {
const Parser = parsers.get(options);
Expand All @@ -137,10 +249,14 @@ export function parse(code, options) {
// Public
//------------------------------------------------------------------------------

/** @type {string} */
export const version = "11.0.0"; // x-release-please-version
export const name = "espree";

/* istanbul ignore next */
/**
* @type {visitorKeys.VisitorKeys}
*/
export const VisitorKeys = (function() {
return visitorKeys.KEYS;
}());
Expand All @@ -149,6 +265,8 @@ export const VisitorKeys = (function() {
/* istanbul ignore next */
export const Syntax = (function() {
let key,

/** @type {Record<string,string>} */
types = {};

if (typeof Object.create === "function") {
Expand Down
Loading