Refactor literal related code (#14312)
This commit is contained in:
1 parent
282a4019c7
commit
222d84f9ed
5 files changed
+44
-45
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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 };
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user