X Tutup
Skip to content

Commit aaf9190

Browse files
authored
Fix comments after directive (#14081)
1 parent 9e09a78 commit aaf9190

File tree

7 files changed

+672
-27
lines changed

7 files changed

+672
-27
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#### Fix comments after directive (#14081 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
"use strict" /* comment */;
7+
8+
// Prettier stable (with other js parsers except `babel`)
9+
Error: Comment "comment" was not printed. Please report this error!
10+
11+
// Prettier main
12+
<Same as input>
13+
```

src/language-js/print/literal.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
const { printString, printNumber } = require("../../common/util.js");
33
const { replaceTextEndOfLine } = require("../../document/doc-utils.js");
4+
const { printDirective } = require("./misc.js");
45

56
function printLiteral(path, options /*, print*/) {
67
const node = path.getNode();
@@ -41,14 +42,25 @@ function printLiteral(path, options /*, print*/) {
4142
}
4243

4344
if (typeof value === "string") {
44-
return replaceTextEndOfLine(printString(node.raw, options));
45+
return isDirective(path)
46+
? printDirective(node.raw, options)
47+
: replaceTextEndOfLine(printString(node.raw, options));
4548
}
4649

4750
return String(value);
4851
}
4952
}
5053
}
5154

55+
function isDirective(path) {
56+
if (path.getName() !== "expression") {
57+
return;
58+
}
59+
60+
const parent = path.getParentNode();
61+
return parent.type === "ExpressionStatement" && parent.directive;
62+
}
63+
5264
function printBigInt(raw) {
5365
return raw.toLowerCase();
5466
}

src/language-js/print/misc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ function printRestSpread(path, options, print) {
9393
return ["...", print("argument"), printTypeAnnotation(path, options, print)];
9494
}
9595

96+
function printDirective(rawText, options) {
97+
const rawContent = rawText.slice(1, -1);
98+
99+
// Check for the alternate quote, to determine if we're allowed to swap
100+
// the quotes on a DirectiveLiteral.
101+
if (rawContent.includes('"') || rawContent.includes("'")) {
102+
return rawText;
103+
}
104+
105+
const enclosingQuote = options.singleQuote ? "'" : '"';
106+
107+
// Directives are exact code unit sequences, which means that you can't
108+
// change the escape sequences they use.
109+
// See https://github.com/prettier/prettier/issues/1555
110+
// and https://tc39.github.io/ecma262/#directive-prologue
111+
return enclosingQuote + rawContent + enclosingQuote;
112+
}
113+
96114
module.exports = {
97115
printOptionalToken,
98116
printDefiniteToken,
@@ -102,4 +120,5 @@ module.exports = {
102120
printTypeAnnotation,
103121
printRestSpread,
104122
adjustClause,
123+
printDirective,
105124
};

src/language-js/printer-estree.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const {
2323
isLineComment,
2424
isNextLineEmpty,
2525
needsHardlineAfterDanglingComment,
26-
rawText,
2726
hasIgnoreComment,
2827
isCallExpression,
2928
isMemberExpression,
@@ -47,6 +46,7 @@ const {
4746
adjustClause,
4847
printRestSpread,
4948
printDefiniteToken,
49+
printDirective,
5050
} = require("./print/misc.js");
5151
const {
5252
printImportDeclaration,
@@ -215,11 +215,6 @@ function printPathNoParens(path, options, print, args) {
215215
case "EmptyStatement":
216216
return "";
217217
case "ExpressionStatement": {
218-
// Detect Flow and TypeScript directives
219-
if (node.directive) {
220-
return [printDirective(node.expression, options), semi];
221-
}
222-
223218
if (
224219
options.parser === "__vue_event_binding" ||
225220
options.parser === "__vue_ts_event_binding"
@@ -429,7 +424,7 @@ function printPathNoParens(path, options, print, args) {
429424
case "Directive":
430425
return [print("value"), semi]; // Babel 6
431426
case "DirectiveLiteral":
432-
return printDirective(node, options);
427+
return printDirective(node.extra.raw, options);
433428
case "UnaryExpression":
434429
parts.push(node.operator);
435430

@@ -813,25 +808,6 @@ function printPathNoParens(path, options, print, args) {
813808
}
814809
}
815810

816-
function printDirective(node, options) {
817-
const raw = rawText(node);
818-
const rawContent = raw.slice(1, -1);
819-
820-
// Check for the alternate quote, to determine if we're allowed to swap
821-
// the quotes on a DirectiveLiteral.
822-
if (rawContent.includes('"') || rawContent.includes("'")) {
823-
return raw;
824-
}
825-
826-
const enclosingQuote = options.singleQuote ? "'" : '"';
827-
828-
// Directives are exact code unit sequences, which means that you can't
829-
// change the escape sequences they use.
830-
// See https://github.com/prettier/prettier/issues/1555
831-
// and https://tc39.github.io/ecma262/#directive-prologue
832-
return enclosingQuote + rawContent + enclosingQuote;
833-
}
834-
835811
function canAttachComment(node) {
836812
return (
837813
node.type &&

0 commit comments

Comments
 (0)
X Tutup