Skip to content

Commit 49e624f

Browse files
fix: improve error message for falsy parsed JS AST (#19458)
* fix: improve error message for falsy parsed JS AST * Update lib/languages/js/source-code/source-code.js Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update tests * use TypeError, not Error --------- Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
1 parent 06b596d commit 49e624f

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

lib/languages/js/source-code/source-code.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,24 @@ const CODE_PATH_EVENTS = [
5858
* @private
5959
*/
6060
function validate(ast) {
61+
if (!ast) {
62+
throw new TypeError(`Unexpected empty AST. (${ast})`);
63+
}
64+
6165
if (!ast.tokens) {
62-
throw new Error("AST is missing the tokens array.");
66+
throw new TypeError("AST is missing the tokens array.");
6367
}
6468

6569
if (!ast.comments) {
66-
throw new Error("AST is missing the comments array.");
70+
throw new TypeError("AST is missing the comments array.");
6771
}
6872

6973
if (!ast.loc) {
70-
throw new Error("AST is missing location information.");
74+
throw new TypeError("AST is missing location information.");
7175
}
7276

7377
if (!ast.range) {
74-
throw new Error("AST is missing range information");
78+
throw new TypeError("AST is missing range information");
7579
}
7680
}
7781

@@ -204,16 +208,16 @@ function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
204208
if (
205209
currentToken.range[1] !== nextToken.range[0] ||
206210

207-
/*
208-
* For backward compatibility, check spaces in JSXText.
209-
* https://github.com/eslint/eslint/issues/12614
210-
*/
211-
(
212-
checkInsideOfJSXText &&
213-
nextToken !== finalToken &&
214-
nextToken.type === "JSXText" &&
215-
/\s/u.test(nextToken.value)
216-
)
211+
/*
212+
* For backward compatibility, check spaces in JSXText.
213+
* https://github.com/eslint/eslint/issues/12614
214+
*/
215+
(
216+
checkInsideOfJSXText &&
217+
nextToken !== finalToken &&
218+
nextToken.type === "JSXText" &&
219+
/\s/u.test(nextToken.value)
220+
)
217221
) {
218222
return true;
219223
}

tests/lib/languages/js/source-code/source-code.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ describe("SourceCode", () => {
9090
assert.strictEqual(sourceCode.lines[1], "bar;");
9191
});
9292

93+
it("should throw an error when called with a false AST", () => {
94+
95+
assert.throws(
96+
() => new SourceCode("foo;", false),
97+
/Unexpected empty AST\. \(false\)/u
98+
);
99+
100+
});
101+
102+
it("should throw an error when called with a null AST", () => {
103+
104+
assert.throws(
105+
() => new SourceCode("foo;", null),
106+
/Unexpected empty AST\. \(null\)/u
107+
);
108+
109+
});
110+
111+
it("should throw an error when called with a undefined AST", () => {
112+
113+
assert.throws(
114+
() => new SourceCode("foo;", void 0),
115+
/Unexpected empty AST\. \(undefined\)/u
116+
);
117+
118+
});
119+
93120
it("should throw an error when called with an AST that's missing tokens", () => {
94121

95122
assert.throws(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy