Skip to content

Commit 44a41e0

Browse files
committed
docs: indicate return of EspreeTokens from tokenize
1 parent 968d5d9 commit 44a41e0

8 files changed

+60
-54
lines changed

dist/espree.d.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Tokenizes the given code.
33
* @param {string} code The code to tokenize.
44
* @param {ParserOptions} options Options defining how to tokenize.
5-
* @returns {import('acorn').Token[]} An array of tokens.
5+
* @returns {EspreeTokens} An array of tokens.
66
* @throws {import('./lib/espree').EnhancedSyntaxError} If the input code is invalid.
77
* @private
88
*/
9-
export function tokenize(code: string, options: ParserOptions): import('acorn').Token[];
9+
export function tokenize(code: string, options: ParserOptions): EspreeTokens;
1010
/**
1111
* Parses the given code.
1212
* @param {string} code The code to tokenize.
@@ -23,6 +23,11 @@ export const Syntax: {
2323
export const latestEcmaVersion: number;
2424
export const supportedEcmaVersions: number[];
2525
export type ecmaVersion = 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 'latest';
26+
export type EspreeToken = import('./lib/token-translator').EsprimaToken;
27+
export type EspreeComment = import('./lib/espree').EsprimaComment;
28+
export type EspreeTokens = {
29+
comments?: EspreeComment[];
30+
} & EspreeToken[];
2631
export type ParserOptions = {
2732
allowReserved?: boolean;
2833
ecmaVersion?: ecmaVersion;

dist/espree.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

dist/lib/espree.d.ts

+16-27
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,17 @@ export class EspreeParser extends acorn.Parser {
99
constructor(opts: import('../espree').ParserOptions | null, code: string | object);
1010
/**
1111
* Returns Espree tokens.
12-
* @returns {{comments?: {type: string; value: string; range?: [number, number]; start?: number; end?: number; loc?: {start: import('acorn').Position | undefined; end: import('acorn').Position | undefined}}[]} & import('acorn').Token[] | null} Espree tokens
12+
* @returns {import('../espree').EspreeTokens | null} Espree tokens
1313
*/
14-
tokenize(): {
15-
comments?: {
16-
type: string;
17-
value: string;
18-
range?: [number, number];
19-
start?: number;
20-
end?: number;
21-
loc?: {
22-
start: import('acorn').Position | undefined;
23-
end: import('acorn').Position | undefined;
24-
};
25-
}[];
26-
} & import('acorn').Token[] | null;
14+
tokenize(): import('../espree').EspreeTokens | null;
2715
/**
2816
* Parses.
29-
* @returns {{sourceType?: "script" | "module" | "commonjs"; comments?: {type: string; value: string; range?: [number, number]; start?: number; end?: number; loc?: {start: import('acorn').Position | undefined; end: import('acorn').Position | undefined}}[]; tokens?: import('acorn').Token[]; body: import('acorn').Node[]} & import('acorn').Node} The program Node
17+
* @returns {{sourceType?: "script" | "module" | "commonjs"; comments?: EsprimaComment[]; tokens?: import('../espree').EspreeTokens; body: import('acorn').Node[]} & import('acorn').Node} The program Node
3018
*/
3119
parse(): {
3220
sourceType?: "script" | "module" | "commonjs";
33-
comments?: {
34-
type: string;
35-
value: string;
36-
range?: [number, number];
37-
start?: number;
38-
end?: number;
39-
loc?: {
40-
start: import('acorn').Position | undefined;
41-
end: import('acorn').Position | undefined;
42-
};
43-
}[];
44-
tokens?: import('acorn').Token[];
21+
comments?: EsprimaComment[];
22+
tokens?: import('../espree').EspreeTokens;
4523
body: import('acorn').Node[];
4624
} & import('acorn').Node;
4725
/**
@@ -71,5 +49,16 @@ export type EnhancedSyntaxError = {
7149
export type EnhancedTokTypes = {
7250
jsxAttrValueToken?: import('acorn').TokenType;
7351
} & typeof import('acorn-jsx').tokTypes;
52+
export type EsprimaComment = {
53+
type: string;
54+
value: string;
55+
range?: [number, number];
56+
start?: number;
57+
end?: number;
58+
loc?: {
59+
start: import('acorn').Position | undefined;
60+
end: import('acorn').Position | undefined;
61+
};
62+
};
7463
import * as acorn from "acorn";
7564
//# sourceMappingURL=espree.d.ts.map

dist/lib/espree.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

dist/lib/token-translator.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

espree.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@
6363
* @typedef {3|5|6|7|8|9|10|11|12|13|2015|2016|2017|2018|2019|2020|2021|2022|'latest'} ecmaVersion
6464
*/
6565

66+
/**
67+
* @typedef {import('./lib/token-translator').EsprimaToken} EspreeToken
68+
*/
69+
70+
/**
71+
* @typedef {import('./lib/espree').EsprimaComment} EspreeComment
72+
*/
73+
74+
/**
75+
* @typedef {{
76+
* comments?: EspreeComment[]
77+
* } & EspreeToken[]} EspreeTokens
78+
*/
79+
6680
/**
6781
* `jsx.Options` gives us 2 optional properties, so extend it
6882
*
@@ -182,7 +196,7 @@ const parsers = {
182196
* Tokenizes the given code.
183197
* @param {string} code The code to tokenize.
184198
* @param {ParserOptions} options Options defining how to tokenize.
185-
* @returns {acorn.Token[]} An array of tokens.
199+
* @returns {EspreeTokens} An array of tokens.
186200
* @throws {EnhancedSyntaxError} If the input code is invalid.
187201
* @private
188202
*/
@@ -194,7 +208,7 @@ export function tokenize(code, options) {
194208
options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign
195209
}
196210

197-
return /** @type {acorn.Token[]} */ (new Parser(options, code).tokenize());
211+
return /** @type {EspreeTokens} */ (new Parser(options, code).tokenize());
198212
}
199213

200214
//------------------------------------------------------------------------------

lib/espree.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
5151
*/
5252

5353
/**
54-
* First three properties as in `acorn.Comment`; next two as in `acorn.Comment`
55-
* but optional. Last is different as has to allow `undefined`
56-
*/
57-
/**
58-
* @local
59-
*
6054
* @typedef {{
6155
* type: string,
6256
* value: string,
@@ -68,10 +62,16 @@ const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
6862
* end: acorn.Position | undefined
6963
* }
7064
* }} EsprimaComment
65+
*/
66+
67+
/**
68+
* First two properties as in `acorn.Comment`; next two as in `acorn.Comment`
69+
* but optional. Last is different as has to allow `undefined`
70+
*/
71+
/**
72+
* @local
7173
*
72-
* @typedef {{
73-
* comments?: EsprimaComment[]
74-
* } & acorn.Token[]} EspreeTokens
74+
* @typedef {import('../espree').EspreeTokens} EspreeTokens
7575
*
7676
* @typedef {{
7777
* tail?: boolean
@@ -98,7 +98,7 @@ const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
9898
* @typedef {{
9999
* sourceType?: "script"|"module"|"commonjs";
100100
* comments?: EsprimaComment[];
101-
* tokens?: acorn.Token[];
101+
* tokens?: EspreeTokens;
102102
* body: acorn.Node[];
103103
* } & acorn.Node} EsprimaProgramNode
104104
*/

lib/token-translator.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,21 @@
4444
* }} BaseEsprimaToken
4545
*
4646
* @typedef {{
47-
* type: string;
48-
* } & BaseEsprimaToken} EsprimaToken
49-
*
50-
* @typedef {{
51-
* type: string | acorn.TokenType;
52-
* } & BaseEsprimaToken} EsprimaTokenFlexible
53-
*
54-
* @typedef {{
5547
* jsxAttrValueToken: boolean;
5648
* ecmaVersion: ecmaVersion;
5749
* }} ExtraNoTokens
5850
*
5951
* @typedef {{
60-
* tokens: EsprimaTokenFlexible[]
52+
* tokens: EsprimaToken[]
6153
* } & ExtraNoTokens} Extra
6254
*/
6355

56+
/**
57+
* @typedef {{
58+
* type: string;
59+
* } & BaseEsprimaToken} EsprimaToken
60+
*/
61+
6462
//------------------------------------------------------------------------------
6563
// Requirements
6664
//------------------------------------------------------------------------------
@@ -148,7 +146,7 @@ class TokenTranslator {
148146
}
149147

150148
/**
151-
* Translates a single Esprima token to a single Acorn token. This may be
149+
* Translates a single Acorn token to a single Esprima token. This may be
152150
* inaccurate due to how templates are handled differently in Esprima and
153151
* Acorn, but should be accurate for all other tokens.
154152
* @param {acorn.Token} token The Acorn token to translate.

0 commit comments

Comments
 (0)