Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve keyword detection in TSModuleDeclaration #15925

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only happens with parser: 'babel-ts', not going to add changelog for it.

Prettier 3.2.2
Playground link

--parser babel-ts

Input:

namespace /* module */ X {}
module /* namespace */ X {}

Output:

module /* module */ 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" {}