diff --git a/src/language-js/print/assignment.js b/src/language-js/print/assignment.js index 7907c6763..e3686efb3 100644 --- a/src/language-js/print/assignment.js +++ b/src/language-js/print/assignment.js @@ -12,7 +12,6 @@ import { hasLeadingOwnLineComment, isBinaryish, isStringLiteral, - isLiteral, isNumericLiteral, isCallExpression, isMemberExpression, @@ -25,6 +24,7 @@ import { } from "../utils/index.js"; import { shouldInlineLogicalExpression } from "./binaryish.js"; import { printCallExpression } from "./call-expression.js"; +import { isLiteral } from "./literal.js"; function printAssignment( path, diff --git a/src/language-js/print/literal.js b/src/language-js/print/literal.js index 2bc3be42e..3b61e61d7 100644 --- a/src/language-js/print/literal.js +++ b/src/language-js/print/literal.js @@ -1,7 +1,11 @@ import printString from "../../utils/print-string.js"; import printNumber from "../../utils/print-number.js"; import { replaceEndOfLine } from "../../document/utils.js"; -import { printDirective } from "./misc.js"; +import { createTypeCheckFunction } from "../utils/index.js"; + +/** + * @typedef {import("../types/estree.js").Node} Node + */ function printLiteral(path, options /*, print*/) { const { node } = path; @@ -21,6 +25,8 @@ function printLiteral(path, options /*, print*/) { return String(node.value); case "DecimalLiteral": return printNumber(node.value) + "m"; + case "DirectiveLiteral": + return printDirective(node.extra.raw, options); case "Literal": { if (node.regex) { return printRegex(node.regex); @@ -68,4 +74,39 @@ function printRegex({ pattern, flags }) { return `/${pattern}/${flags}`; } -export { printLiteral, printBigInt }; +function printDirective(rawText, options) { + const rawContent = rawText.slice(1, -1); + + // Check for the alternate quote, to determine if we're allowed to swap + // the quotes on a DirectiveLiteral. + if (rawContent.includes('"') || rawContent.includes("'")) { + return rawText; + } + + const enclosingQuote = options.singleQuote ? "'" : '"'; + + // Directives are exact code unit sequences, which means that you can't + // change the escape sequences they use. + // See https://github.com/prettier/prettier/issues/1555 + // and https://tc39.github.io/ecma262/#directive-prologue + return enclosingQuote + rawContent + enclosingQuote; +} + +/** + * @param {Node} node + * @returns {boolean} + */ +const isLiteral = createTypeCheckFunction([ + "Literal", + // Babel, flow uses `BigIntLiteral` too + "BigIntLiteral", + "BooleanLiteral", + "DecimalLiteral", + "DirectiveLiteral", + "NullLiteral", + "NumericLiteral", + "RegExpLiteral", + "StringLiteral", +]); + +export { printLiteral, printBigInt, isLiteral }; diff --git a/src/language-js/print/misc.js b/src/language-js/print/misc.js index 6cfe49417..abfc6d658 100644 --- a/src/language-js/print/misc.js +++ b/src/language-js/print/misc.js @@ -119,24 +119,6 @@ function printRestSpread(path, print) { return ["...", print("argument"), printTypeAnnotationProperty(path, print)]; } -function printDirective(rawText, options) { - const rawContent = rawText.slice(1, -1); - - // Check for the alternate quote, to determine if we're allowed to swap - // the quotes on a DirectiveLiteral. - if (rawContent.includes('"') || rawContent.includes("'")) { - return rawText; - } - - const enclosingQuote = options.singleQuote ? "'" : '"'; - - // Directives are exact code unit sequences, which means that you can't - // change the escape sequences they use. - // See https://github.com/prettier/prettier/issues/1555 - // and https://tc39.github.io/ecma262/#directive-prologue - return enclosingQuote + rawContent + enclosingQuote; -} - function printTypeScriptAccessibilityToken(node) { return node.accessibility ? node.accessibility + " " : ""; } @@ -150,6 +132,5 @@ export { printBindExpressionCallee, printRestSpread, adjustClause, - printDirective, printTypeScriptAccessibilityToken, }; diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index dbe0269fe..6bd115d10 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -47,7 +47,6 @@ import { adjustClause, printRestSpread, printDefiniteToken, - printDirective, printDeclareToken, } from "./print/misc.js"; import { @@ -432,8 +431,6 @@ function printPathNoParens(path, options, print, args) { return "super"; case "Directive": return [print("value"), semi]; // Babel 6 - case "DirectiveLiteral": - return printDirective(node.extra.raw, options); case "UnaryExpression": parts.push(node.operator); diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index dfee8e933..c846e720f 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -144,25 +144,6 @@ const isExportDeclaration = createTypeCheckFunction([ "DeclareExportAllDeclaration", ]); -/** - * @param {Node} node - * @returns {boolean} - */ -const isLiteral = createTypeCheckFunction([ - "BooleanLiteral", - "DirectiveLiteral", - "Literal", - "NullLiteral", - "NumericLiteral", - "BigIntLiteral", - "DecimalLiteral", - "RegExpLiteral", - "StringLiteral", - "TemplateLiteral", - "TSTypeLiteral", - "JSXText", -]); - /** * @param {Node} node * @returns {boolean} @@ -1247,7 +1228,6 @@ export { isFunctionOrArrowExpression, isGetterOrSetter, isJsxElement, - isLiteral, isLongCurriedCallExpression, isSimpleCallArgument, isMemberish,