Skip to content

Commit 10762ee

Browse files
authored
Improve keyword detection in TSModuleDeclaration (#15925)
1 parent 70c9c56 commit 10762ee

File tree

5 files changed

+51
-34
lines changed

5 files changed

+51
-34
lines changed

src/language-js/print/module.js

+1-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import assert from "node:assert";
2-
31
import {
42
group,
53
hardline,
@@ -13,6 +11,7 @@ import { printDanglingComments } from "../../main/comments/print.js";
1311
import isNonEmptyArray from "../../utils/is-non-empty-array.js";
1412
import UnexpectedNodeError from "../../utils/unexpected-node-error.js";
1513
import { hasSameLoc, locEnd, locStart } from "../loc.js";
14+
import getTextWithoutComments from "../utils/get-text-without-comments.js";
1615
import {
1716
CommentCheckFlags,
1817
createTypeCheckFunction,
@@ -249,35 +248,6 @@ function shouldPrintSpecifiers(node, options) {
249248
return text.trimEnd().endsWith("from");
250249
}
251250

252-
function getTextWithoutComments(options, start, end) {
253-
let text = options.originalText.slice(start, end);
254-
255-
for (const comment of options[Symbol.for("comments")]) {
256-
const commentStart = locStart(comment);
257-
// Comments are sorted, we can escape if the comment is after the range
258-
if (commentStart > end) {
259-
break;
260-
}
261-
262-
const commentEnd = locEnd(comment);
263-
if (commentEnd < start) {
264-
continue;
265-
}
266-
267-
const commentLength = commentEnd - commentStart;
268-
text =
269-
text.slice(0, commentStart - start) +
270-
" ".repeat(commentLength) +
271-
text.slice(commentEnd - start);
272-
}
273-
274-
if (process.env.NODE_ENV !== "production") {
275-
assert(text.length === end - start);
276-
}
277-
278-
return text;
279-
}
280-
281251
function getImportAttributesKeyword(node, options) {
282252
// Babel parser add this property to indicate the keyword is `assert`
283253
if (node.extra?.deprecatedAssertSyntax) {

src/language-js/print/typescript.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "../../document/builders.js";
99
import UnexpectedNodeError from "../../utils/unexpected-node-error.js";
1010
import { locStart } from "../loc.js";
11+
import getTextWithoutComments from "../utils/get-text-without-comments.js";
1112
import {
1213
isArrayOrTupleExpression,
1314
isObjectOrRecordExpression,
@@ -311,9 +312,9 @@ function printTypescript(path, options, print) {
311312
node.kind ??
312313
// TODO: Use `node.kind` when babel update AST
313314
(isStringLiteral(node.id) ||
314-
/(?:^|\s)module(?:\s|$)/.test(
315-
options.originalText.slice(locStart(node), locStart(node.id)),
316-
)
315+
getTextWithoutComments(options, locStart(node), locStart(node.id))
316+
.trim()
317+
.endsWith("module")
317318
? "module"
318319
: "namespace");
319320
parts.push(kind, " ");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import assert from "node:assert";
2+
3+
import { locEnd, locStart } from "../loc.js";
4+
5+
function getTextWithoutComments(options, start, end) {
6+
let text = options.originalText.slice(start, end);
7+
8+
for (const comment of options[Symbol.for("comments")]) {
9+
const commentStart = locStart(comment);
10+
// Comments are sorted, we can escape if the comment is after the range
11+
if (commentStart > end) {
12+
break;
13+
}
14+
15+
const commentEnd = locEnd(comment);
16+
if (commentEnd < start) {
17+
continue;
18+
}
19+
20+
const commentLength = commentEnd - commentStart;
21+
text =
22+
text.slice(0, commentStart - start) +
23+
" ".repeat(commentLength) +
24+
text.slice(commentEnd - start);
25+
}
26+
27+
if (process.env.NODE_ENV !== "production") {
28+
assert(text.length === end - start);
29+
}
30+
31+
return text;
32+
}
33+
34+
export default getTextWithoutComments;

tests/format/typescript/module/__snapshots__/jsfmt.spec.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ namespace X {
8080
}
8181
}
8282
83+
namespace /* module */ X {}
84+
module /* namespace */ X {}
85+
module /* namespace */ "x" {}
86+
8387
=====================================output=====================================
8488
module X {}
8589
@@ -113,6 +117,10 @@ namespace X {
113117
}
114118
}
115119
120+
namespace /* module */ X {}
121+
module /* namespace */ X {}
122+
module /* namespace */ "x" {}
123+
116124
================================================================================
117125
`;
118126

tests/format/typescript/module/keyword.ts

+4
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ namespace X {
2929
const x = 1;
3030
}
3131
}
32+
33+
namespace /* module */ X {}
34+
module /* namespace */ X {}
35+
module /* namespace */ "x" {}

0 commit comments

Comments
 (0)