Skip to content

Commit 24d1358

Browse files
srijan-paulmdjermanovicbmish
authored andcommitted
docs: add jsdoc types to espree
Co-authored-by: Milos Djermanovic <[email protected]> Co-authored-by: Bryan Mishkin <[email protected]>
1 parent b578a66 commit 24d1358

7 files changed

+113
-18
lines changed

espree.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ import espreeVersion from "./lib/version.js";
6363
import * as visitorKeys from "eslint-visitor-keys";
6464
import { getLatestEcmaVersion, getSupportedEcmaVersions } from "./lib/options.js";
6565

66+
/**
67+
* @typedef {import("acorn")} acorn
68+
* @typedef {import("./lib/options").ParserOptions} ParserOptions
69+
* @typedef {import("./lib/token-translator").EsprimaToken} EsprimaToken
70+
* @typedef {import("./lib/token-translator").TokenRange} TokenRange
71+
*/
72+
6673

6774
// To initialize lazily.
6875
const parsers = {
@@ -101,8 +108,8 @@ const parsers = {
101108
/**
102109
* Tokenizes the given code.
103110
* @param {string} code The code to tokenize.
104-
* @param {Object} options Options defining how to tokenize.
105-
* @returns {Token[]} An array of tokens.
111+
* @param {ParserOptions} [options] Options defining how to tokenize.
112+
* @returns {EsprimaToken[]} An array of tokens.
106113
* @throws {SyntaxError} If the input code is invalid.
107114
* @private
108115
*/
@@ -124,8 +131,8 @@ export function tokenize(code, options) {
124131
/**
125132
* Parses the given code.
126133
* @param {string} code The code to tokenize.
127-
* @param {Object} options Options defining how to tokenize.
128-
* @returns {ASTNode} The "Program" AST node.
134+
* @param {ParserOptions} options Options defining how to tokenize.
135+
* @returns {acorn.Node} The "Program" AST node.
129136
* @throws {SyntaxError} If the input code is invalid.
130137
*/
131138
export function parse(code, options) {

lib/espree.js

+31-8
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@ import TokenTranslator from "./token-translator.js";
33
import { normalizeOptions } from "./options.js";
44

55

6+
/**
7+
* @typedef {import("acorn")} acorn
8+
* @typedef {import("./token-translator").Range} Range
9+
*/
10+
611
const STATE = Symbol("espree's internal state");
712
const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
813

14+
/**
15+
* @typedef {Object} EsprimaComment
16+
* @property {"Block"|"Line"} type Type of the comment, can either be "Block" (multiline) or "Line" (single line).
17+
* @property {string} text Contents of the comment.
18+
* @property {number|undefined} start Start column of a comment.
19+
* @property {number|undefined} end End column of the comment.
20+
* @property {Range|undefined} range The [start, end] range of a comment.
21+
* @property {acorn.Position} startLoc Start location of the comment.
22+
* @property {acorn.Position} endLoc End location of the comment.
23+
*/
924

1025
/**
1126
* Converts an Acorn comment to a Esprima comment.
@@ -15,7 +30,7 @@ const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
1530
* @param {int} end The index at which the comment ends.
1631
* @param {Location} startLoc The location at which the comment starts.
1732
* @param {Location} endLoc The location at which the comment ends.
18-
* @returns {Object} The comment object.
33+
* @returns {EsprimaComment} The comment object.
1934
* @private
2035
*/
2136
function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) {
@@ -40,7 +55,12 @@ function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc,
4055
return comment;
4156
}
4257

43-
export default () => Parser => {
58+
/**
59+
* Takes an acorn Parser class and returns a new Parser extending from it.
60+
* @param {typeof acorn.Parser} Parser A base acorn parser class.
61+
* @returns {typeof acorn.Parser} An espree parser extending the base acorn parser.
62+
*/
63+
function extendAcornParser(Parser) {
4464
const tokTypes = Object.assign({}, Parser.acorn.tokTypes);
4565

4666
if (Parser.acornJsx) {
@@ -232,7 +252,7 @@ export default () => Parser => {
232252

233253
/**
234254
* Overwrites the default raise method to throw Esprima-style errors.
235-
* @param {int} pos The position of the error.
255+
* @param {number} pos The position of the error.
236256
* @param {string} message The error message.
237257
* @throws {SyntaxError} A syntax error.
238258
* @returns {void}
@@ -249,7 +269,7 @@ export default () => Parser => {
249269

250270
/**
251271
* Overwrites the default raise method to throw Esprima-style errors.
252-
* @param {int} pos The position of the error.
272+
* @param {number} pos The position of the error.
253273
* @param {string} message The error message.
254274
* @throws {SyntaxError} A syntax error.
255275
* @returns {void}
@@ -260,7 +280,7 @@ export default () => Parser => {
260280

261281
/**
262282
* Overwrites the default unexpected method to throw Esprima-style errors.
263-
* @param {int} pos The position of the error.
283+
* @param {number} pos The position of the error.
264284
* @throws {SyntaxError} A syntax error.
265285
* @returns {void}
266286
*/
@@ -305,8 +325,8 @@ export default () => Parser => {
305325

306326
/**
307327
* Performs last-minute Esprima-specific compatibility checks and fixes.
308-
* @param {ASTNode} result The node to check.
309-
* @returns {ASTNode} The finished node.
328+
* @param {acorn.Node} result The node to check.
329+
* @returns {acorn.Node} The finished node.
310330
*/
311331
[ESPRIMA_FINISH_NODE](result) {
312332

@@ -325,4 +345,7 @@ export default () => Parser => {
325345
return result;
326346
}
327347
};
328-
};
348+
349+
}
350+
351+
export default () => extendAcornParser;

lib/features.js

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
// Public
1515
//------------------------------------------------------------------------------
1616

17+
/**
18+
* @typedef {Object} EcmaFeatures
19+
* @property {boolean} [jsx]
20+
* @property {boolean} [globalReturn]
21+
* @property {boolean} [impliedStrict]
22+
*/
23+
24+
/**
25+
* @type {EcmaFeatures}
26+
*/
1727
export default {
1828

1929
// React JSX parsing

lib/options.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,25 @@ function normalizeSourceType(sourceType = "script") {
8181
throw new Error("Invalid sourceType.");
8282
}
8383

84+
/**
85+
* @typedef {import("./features").EcmaFeatures} EcmaFeatures
86+
*/
87+
88+
/**
89+
* @typedef {Object} ParserOptions
90+
* @property {boolean} [range] Whether to include the range information for each node.
91+
* @property {boolean} [loc] Whether to include the location information for every node.
92+
* @property {boolean} [comment] Whether to include an array of all comments
93+
* @property {boolean} [tokens] Whether to include an array of all tokens
94+
* @property {number|"latest"} [ecmaVersion] The ECMAScript version to use (between 3 and 13, or 2015 and 2022, or "latest").
95+
* @property {boolean} [allowReserved] Only allowed when `ecmaVersion` is set to 3.
96+
* @property {"script"|"module"|"commonjs"} [sourceType] The kind of the source string being parsed.
97+
* @property {EcmaFeatures} [ecmaFeatures] The additional features to enable.
98+
*/
99+
84100
/**
85101
* Normalize parserOptions
86-
* @param {Object} options the parser options to normalize
102+
* @param {ParserOptions} options the parser options to normalize
87103
* @throws {Error} throw an error if found invalid option.
88104
* @returns {Object} normalized options
89105
*/

lib/token-translator.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414
// Private
1515
//------------------------------------------------------------------------------
1616

17+
/**
18+
* @typedef {import("acorn")} acorn
19+
*/
20+
21+
/**
22+
* @typedef {Object} Location
23+
* @property {acorn.Position} start The start position.
24+
* @property {acorn.Position} end The end position.
25+
*/
26+
27+
/**
28+
* @typedef {[number, number]} Range
29+
*/
30+
31+
/**
32+
* @typedef {Object} EsprimaToken
33+
* @property {string} type The type of this token.
34+
* @property {string} value The string content of the token.
35+
* @property {Location|undefined} loc Location in source text.
36+
* @property {number|undefined} start start column.
37+
* @property {number|undefined} end end column.
38+
* @property {Range|undefined} range [start, end] range
39+
*/
1740

1841
// Esprima Token Types
1942
const Token = {
@@ -34,7 +57,7 @@ const Token = {
3457

3558
/**
3659
* Converts part of a template into an Esprima token.
37-
* @param {AcornToken[]} tokens The Acorn tokens representing the template.
60+
* @param {acorn.Token[]} tokens The Acorn tokens representing the template.
3861
* @param {string} code The source code.
3962
* @returns {EsprimaToken} The Esprima equivalent of the template token.
4063
* @private
@@ -94,7 +117,7 @@ TokenTranslator.prototype = {
94117
* Translates a single Esprima token to a single Acorn token. This may be
95118
* inaccurate due to how templates are handled differently in Esprima and
96119
* Acorn, but should be accurate for all other tokens.
97-
* @param {AcornToken} token The Acorn token to translate.
120+
* @param {acorn.Token} token The Acorn token to translate.
98121
* @param {Object} extra Espree extra object.
99122
* @returns {EsprimaToken} The Esprima version of the token.
100123
*/
@@ -174,7 +197,7 @@ TokenTranslator.prototype = {
174197

175198
/**
176199
* Function to call during Acorn's onToken handler.
177-
* @param {AcornToken} token The Acorn token.
200+
* @param {acorn.Token} token The Acorn token.
178201
* @param {Object} extra The Espree extra object.
179202
* @returns {void}
180203
*/

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"files": [
2121
"lib",
2222
"dist/espree.cjs",
23+
"dist/espree.d.ts",
2324
"espree.js"
2425
],
26+
"types": "dist/espree.d.ts",
2527
"engines": {
2628
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
2729
},
@@ -40,6 +42,7 @@
4042
"@rollup/plugin-json": "^4.1.0",
4143
"@rollup/plugin-node-resolve": "^11.2.0",
4244
"c8": "^7.11.0",
45+
"@types/acorn": "^4.0.6",
4346
"chai": "^4.3.4",
4447
"eslint": "^7.22.0",
4548
"eslint-config-eslint": "^7.0.0",
@@ -52,7 +55,9 @@
5255
"mocha": "^8.3.1",
5356
"npm-run-all": "^4.1.5",
5457
"rollup": "^2.41.2",
55-
"shelljs": "^0.3.0"
58+
"shelljs": "^0.3.0",
59+
"typescript": "^4.5.4",
60+
"unicode-6.3.0": "^0.7.5"
5661
},
5762
"keywords": [
5863
"ast",
@@ -69,7 +74,7 @@
6974
"test": "npm-run-all -p unit lint",
7075
"lint": "eslint \"*.?(c)js\" lib/ tests/lib/",
7176
"fixlint": "npm run lint -- --fix",
72-
"build": "rollup -c rollup.config.js",
77+
"build": "rollup -c rollup.config.js && tsc --build",
7378
"update-version": "node tools/update-version.js",
7479
"pretest": "npm run build",
7580
"prepublishOnly": "npm run update-version && npm run build",

tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"include": ["lib/**/*", "espree.js"],
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"declaration": true,
6+
"emitDeclarationOnly": true,
7+
"outDir": "dist",
8+
"declarationMap": true,
9+
"strict": true
10+
}
11+
}

0 commit comments

Comments
 (0)