Skip to content

Commit

Permalink
Improve keyword detection in TSModuleDeclaration (#15925)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jan 16, 2024
1 parent 70c9c56 commit 10762ee
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 34 deletions.
32 changes: 1 addition & 31 deletions src/language-js/print/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import assert from "node:assert";

import {
group,
hardline,
Expand All @@ -13,6 +11,7 @@ import { printDanglingComments } from "../../main/comments/print.js";
import isNonEmptyArray from "../../utils/is-non-empty-array.js";
import UnexpectedNodeError from "../../utils/unexpected-node-error.js";
import { hasSameLoc, locEnd, locStart } from "../loc.js";
import getTextWithoutComments from "../utils/get-text-without-comments.js";
import {
CommentCheckFlags,
createTypeCheckFunction,
Expand Down Expand Up @@ -249,35 +248,6 @@ function shouldPrintSpecifiers(node, options) {
return text.trimEnd().endsWith("from");
}

function getTextWithoutComments(options, start, end) {
let text = options.originalText.slice(start, end);

for (const comment of options[Symbol.for("comments")]) {
const commentStart = locStart(comment);
// Comments are sorted, we can escape if the comment is after the range
if (commentStart > end) {
break;
}

const commentEnd = locEnd(comment);
if (commentEnd < start) {
continue;
}

const commentLength = commentEnd - commentStart;
text =
text.slice(0, commentStart - start) +
" ".repeat(commentLength) +
text.slice(commentEnd - start);
}

if (process.env.NODE_ENV !== "production") {
assert(text.length === end - start);
}

return text;
}

function getImportAttributesKeyword(node, options) {
// Babel parser add this property to indicate the keyword is `assert`
if (node.extra?.deprecatedAssertSyntax) {
Expand Down
7 changes: 4 additions & 3 deletions src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../document/builders.js";
import UnexpectedNodeError from "../../utils/unexpected-node-error.js";
import { locStart } from "../loc.js";
import getTextWithoutComments from "../utils/get-text-without-comments.js";
import {
isArrayOrTupleExpression,
isObjectOrRecordExpression,
Expand Down Expand Up @@ -311,9 +312,9 @@ function printTypescript(path, options, print) {
node.kind ??
// TODO: Use `node.kind` when babel update AST
(isStringLiteral(node.id) ||
/(?:^|\s)module(?:\s|$)/.test(
options.originalText.slice(locStart(node), locStart(node.id)),
)
getTextWithoutComments(options, locStart(node), locStart(node.id))
.trim()
.endsWith("module")
? "module"
: "namespace");
parts.push(kind, " ");
Expand Down
34 changes: 34 additions & 0 deletions src/language-js/utils/get-text-without-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from "node:assert";

import { locEnd, locStart } from "../loc.js";

function getTextWithoutComments(options, start, end) {
let text = options.originalText.slice(start, end);

for (const comment of options[Symbol.for("comments")]) {
const commentStart = locStart(comment);
// Comments are sorted, we can escape if the comment is after the range
if (commentStart > end) {
break;
}

const commentEnd = locEnd(comment);
if (commentEnd < start) {
continue;
}

const commentLength = commentEnd - commentStart;
text =
text.slice(0, commentStart - start) +
" ".repeat(commentLength) +
text.slice(commentEnd - start);
}

if (process.env.NODE_ENV !== "production") {
assert(text.length === end - start);
}

return text;
}

export default getTextWithoutComments;
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ namespace X {
}
}
namespace /* module */ X {}
module /* namespace */ X {}
module /* namespace */ "x" {}
=====================================output=====================================
module X {}
Expand Down Expand Up @@ -113,6 +117,10 @@ namespace X {
}
}
namespace /* module */ X {}
module /* namespace */ X {}
module /* namespace */ "x" {}
================================================================================
`;

Expand Down
4 changes: 4 additions & 0 deletions tests/format/typescript/module/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ namespace X {
const x = 1;
}
}

namespace /* module */ X {}
module /* namespace */ X {}
module /* namespace */ "x" {}

0 comments on commit 10762ee

Please sign in to comment.