Skip to content

Commit a1d2f7a

Browse files
committed
add tsd tests and inspired fixes (making arguments optional)
1 parent c2df838 commit a1d2f7a

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

packages/espree/espree.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ const parsers = {
176176

177177
/**
178178
* Gets the parser object based on the supplied options.
179-
* @param {Options} options The parser options.
179+
* @param {Options} [options] The parser options.
180180
* @returns {EspreeParserJsxCtor|EspreeParserCtor} Regular or JSX Acorn parser
181181
*/
182182
get(options) {
@@ -197,7 +197,7 @@ const parsers = {
197197
/**
198198
* Tokenizes the given code.
199199
* @param {string} code The code to tokenize.
200-
* @param {Options} options Options defining how to tokenize.
200+
* @param {Options} [options] Options defining how to tokenize.
201201
* @returns {EspreeTokens} An array of tokens.
202202
* @throws {EnhancedSyntaxError} If the input code is invalid.
203203
* @private
@@ -220,7 +220,7 @@ export function tokenize(code, options) {
220220
/**
221221
* Parses the given code.
222222
* @param {string} code The code to tokenize.
223-
* @param {Options} options Options defining how to tokenize.
223+
* @param {Options} [options] Options defining how to tokenize.
224224
* @returns {acorn.Node} The "Program" AST node.
225225
* @throws {EnhancedSyntaxError} If the input code is invalid.
226226
*/

packages/espree/lib/espree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default () => {
159159
return class Espree extends Parser {
160160

161161
/**
162-
* @param {Options | null} opts The parser options
162+
* @param {Options | null | undefined} opts The parser options
163163
* @param {string | object} code The code which will be converted to a string.
164164
*/
165165
constructor(opts, code) {

packages/espree/lib/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
/* eslint-disable jsdoc/valid-types -- Bug in older versions */
2929
/**
3030
* @typedef {{
31-
* new (opts: Options | null, code: string | object): EspreeParser
31+
* new (opts: Options | null | undefined, code: string | object): EspreeParser
3232
* } & Pick<typeof acorn.Parser, keyof typeof acorn.Parser>} EspreeParserCtor
3333
*/
3434
/**
3535
* @typedef {{
36-
* new (opts: Options | null, code: string | object): EspreeParser
36+
* new (opts: Options | null | undefined, code: string | object): EspreeParser
3737
* } & Pick<AcornJsxParserCtor, keyof AcornJsxParserCtor>} EspreeParserJsxCtor
3838
*/
3939

packages/espree/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@rollup/plugin-node-resolve": "^15.3.0",
4949
"rollup": "^2.79.1",
5050
"shelljs": "^0.8.5",
51+
"tsd": "^0.33.0",
5152
"typescript": "^5.9.3"
5253
},
5354
"keywords": [
@@ -67,7 +68,8 @@
6768
"build:update-version": "node tools/update-version.js",
6869
"prepublishOnly": "npm run build:update-version && npm run build",
6970
"pretest": "npm run build",
70-
"test": "npm run test:cjs && npm run test:esm",
71+
"test": "npm run test:tsd && npm run test:cjs && npm run test:esm",
72+
"test:tsd": "tsd --typings dist/espree.d.ts",
7173
"test:cjs": "mocha --color --reporter progress --timeout 30000 \"tests/**/*.test.cjs\"",
7274
"test:esm": "c8 mocha --color --reporter progress --timeout 30000 \"tests/**/*.test.js\""
7375
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { expectType } from 'tsd';
2+
3+
import * as espree from "../espree.js";
4+
import type { Options, EspreeTokens } from "../espree.js";
5+
import type {VisitorKeys} from "../../eslint-visitor-keys/lib/index.js";
6+
7+
const ast = espree.parse("let foo = \"bar\"");
8+
expectType<acorn.Node>(ast);
9+
10+
const ast_option = espree.parse("let foo = \"bar\"", { ecmaVersion: 6 });
11+
expectType<acorn.Node>(ast_option);
12+
13+
const tokens = espree.tokenize("let foo = \"bar\"");
14+
expectType<EspreeTokens>(tokens);
15+
16+
const tokens_option = espree.tokenize("let foo = \"bar\"", { ecmaVersion: 6 });
17+
expectType<EspreeTokens>(tokens_option);
18+
19+
const version = espree.version;
20+
expectType<string>(version);
21+
22+
const visitor_keys = espree.VisitorKeys;
23+
expectType<VisitorKeys>(visitor_keys);
24+
25+
const latest_ecma = espree.latestEcmaVersion;
26+
expectType<number>(latest_ecma);
27+
28+
const supported_ecma = espree.supportedEcmaVersions;
29+
expectType<number[]>(supported_ecma);
30+
31+
const full_options: Options = {
32+
range: false,
33+
loc: false,
34+
comment: false,
35+
tokens: false,
36+
ecmaVersion: 3,
37+
allowReserved: true,
38+
sourceType: "script",
39+
ecmaFeatures: {
40+
jsx: false,
41+
globalReturn: false,
42+
impliedStrict: false,
43+
},
44+
};
45+
expectType<Options>(full_options);
46+
47+
const empty_options: Options = {};
48+
expectType<Options>(empty_options);
49+
50+
const latest_options: Options = {
51+
ecmaVersion: 16,
52+
};
53+
expectType<Options>(latest_options);

0 commit comments

Comments
 (0)