Skip to content

Commit

Permalink
Improve keyword detection in TSModuleDeclaration (prettier#15925)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Feb 17, 2024
1 parent 587b2a9 commit 7e88db3
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
@@ -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
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
@@ -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;
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
Expand Up @@ -29,3 +29,7 @@ namespace X {
const x = 1;
}
}

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

0 comments on commit 7e88db3

Please sign in to comment.