Refactor export declaration print (#14546)
This commit is contained in:
1 parent
21727ccf37
commit
addc340928
5 files changed
+110
-153
No files matched your search
@@ -20,6 +20,12 @@ function printClassMemberDecorators(path, options, print) {
|
||||
}
|
||||
|
||||
function printDecoratorsBeforeExport(path, options, print) {
|
||||
// Only print decorators here if they were written before the export,
|
||||
// otherwise they are printed by the node.declaration
|
||||
if (!hasDecoratorsBeforeExport(path.node)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Export declarations are responsible for printing any decorators
|
||||
// that logically apply to node.declaration.
|
||||
return [
|
||||
|
||||
@@ -27,7 +27,7 @@ import {
|
||||
} from "./type-annotation.js";
|
||||
import { printInterface } from "./interface.js";
|
||||
import { printTypeParameter, printTypeParameters } from "./type-parameters.js";
|
||||
import { printExportDeclaration, printExportAllDeclaration } from "./module.js";
|
||||
import { printExportDeclaration } from "./module.js";
|
||||
import { printArray } from "./array.js";
|
||||
import { printObject } from "./object.js";
|
||||
import { printPropertyKey } from "./property.js";
|
||||
@@ -77,9 +77,8 @@ function printFlow(path, options, print) {
|
||||
semi,
|
||||
];
|
||||
case "DeclareExportDeclaration":
|
||||
return printExportDeclaration(path, options, print);
|
||||
case "DeclareExportAllDeclaration":
|
||||
return printExportAllDeclaration(path, options, print);
|
||||
return printExportDeclaration(path, options, print);
|
||||
case "DeclareOpaqueType":
|
||||
case "OpaqueType":
|
||||
return printOpaqueType(path, options, print);
|
||||
|
||||
+90
-123
@@ -18,12 +18,10 @@ import {
|
||||
needsHardlineAfterDanglingComment,
|
||||
isStringLiteral,
|
||||
rawText,
|
||||
createTypeCheckFunction,
|
||||
} from "../utils/index.js";
|
||||
import { locStart, hasSameLoc } from "../loc.js";
|
||||
import {
|
||||
hasDecoratorsBeforeExport,
|
||||
printDecoratorsBeforeExport,
|
||||
} from "./decorators.js";
|
||||
import { printDecoratorsBeforeExport } from "./decorators.js";
|
||||
import { printDeclareToken } from "./misc.js";
|
||||
|
||||
/**
|
||||
@@ -32,56 +30,41 @@ import { printDeclareToken } from "./misc.js";
|
||||
|
||||
function printImportDeclaration(path, options, print) {
|
||||
const { node } = path;
|
||||
const semi = options.semi ? ";" : "";
|
||||
/** @type{Doc[]} */
|
||||
const parts = [];
|
||||
|
||||
const { importKind } = node;
|
||||
|
||||
parts.push("import");
|
||||
|
||||
if (node.module) {
|
||||
parts.push(" module");
|
||||
}
|
||||
|
||||
if (importKind && importKind !== "value") {
|
||||
parts.push(" ", importKind);
|
||||
}
|
||||
|
||||
parts.push(
|
||||
return [
|
||||
"import",
|
||||
node.module ? " module" : "",
|
||||
printImportKind(node),
|
||||
printModuleSpecifiers(path, options, print),
|
||||
printModuleSource(path, options, print),
|
||||
printImportAssertions(path, options, print),
|
||||
semi
|
||||
);
|
||||
|
||||
return parts;
|
||||
options.semi ? ";" : "",
|
||||
];
|
||||
}
|
||||
|
||||
const isDefaultExport = (node) =>
|
||||
node.type === "ExportDefaultDeclaration" ||
|
||||
(node.type === "DeclareExportDeclaration" && node.default);
|
||||
|
||||
/*
|
||||
- `ExportDefaultDeclaration`
|
||||
- `ExportNamedDeclaration`
|
||||
- `DeclareExportDeclaration`(flow)
|
||||
- `ExportAllDeclaration`
|
||||
- `DeclareExportAllDeclaration`(flow)
|
||||
*/
|
||||
function printExportDeclaration(path, options, print) {
|
||||
const { node } = path;
|
||||
|
||||
/** @type{Doc[]} */
|
||||
const parts = [];
|
||||
const parts = [
|
||||
printDecoratorsBeforeExport(path, options, print),
|
||||
printDeclareToken(path),
|
||||
"export",
|
||||
isDefaultExport(node) ? " default" : "",
|
||||
];
|
||||
|
||||
// Only print decorators here if they were written before the export,
|
||||
// otherwise they are printed by the node.declaration
|
||||
if (hasDecoratorsBeforeExport(node)) {
|
||||
parts.push(printDecoratorsBeforeExport(path, options, print));
|
||||
}
|
||||
|
||||
const { type, exportKind, declaration } = node;
|
||||
|
||||
parts.push(printDeclareToken(path), "export");
|
||||
|
||||
const isDefaultExport = node.default || type === "ExportDefaultDeclaration";
|
||||
if (isDefaultExport) {
|
||||
parts.push(" default");
|
||||
}
|
||||
const { declaration, exported } = node;
|
||||
|
||||
if (hasComment(node, CommentCheckFlags.Dangling)) {
|
||||
parts.push(" ", printDanglingComments(path, options));
|
||||
@@ -94,77 +77,64 @@ function printExportDeclaration(path, options, print) {
|
||||
if (declaration) {
|
||||
parts.push(" ", print("declaration"));
|
||||
} else {
|
||||
parts.push(printExportKind(node));
|
||||
|
||||
if (
|
||||
node.type === "ExportAllDeclaration" ||
|
||||
node.type === "DeclareExportAllDeclaration"
|
||||
) {
|
||||
parts.push(" *");
|
||||
if (exported) {
|
||||
parts.push(" as ", print("exported"));
|
||||
}
|
||||
} else {
|
||||
parts.push(printModuleSpecifiers(path, options, print));
|
||||
}
|
||||
|
||||
parts.push(
|
||||
exportKind === "type" ? " type" : "",
|
||||
printModuleSpecifiers(path, options, print),
|
||||
printModuleSource(path, options, print),
|
||||
printImportAssertions(path, options, print)
|
||||
);
|
||||
}
|
||||
|
||||
if (shouldExportDeclarationPrintSemi(node, options)) {
|
||||
parts.push(";");
|
||||
}
|
||||
parts.push(printSemicolonAfterExportDeclaration(node, options));
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
/*
|
||||
- `ExportAllDeclaration`
|
||||
- `DeclareExportAllDeclaration`(flow)
|
||||
*/
|
||||
function printExportAllDeclaration(path, options, print) {
|
||||
const { node } = path;
|
||||
const semi = options.semi ? ";" : "";
|
||||
/** @type{Doc[]} */
|
||||
const parts = [printDeclareToken(path), "export"];
|
||||
|
||||
const { exportKind, exported } = node;
|
||||
|
||||
if (exportKind === "type") {
|
||||
parts.push(" type");
|
||||
}
|
||||
|
||||
parts.push(" *");
|
||||
|
||||
if (exported) {
|
||||
parts.push(" as ", print("exported"));
|
||||
}
|
||||
|
||||
parts.push(
|
||||
printModuleSource(path, options, print),
|
||||
printImportAssertions(path, options, print),
|
||||
semi
|
||||
);
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
function shouldExportDeclarationPrintSemi(node, options) {
|
||||
if (!options.semi) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { type, declaration } = node;
|
||||
const isDefaultExport = node.default || type === "ExportDefaultDeclaration";
|
||||
if (!declaration) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const { type: declarationType } = declaration;
|
||||
const shouldOmitSemicolon = createTypeCheckFunction([
|
||||
"ClassDeclaration",
|
||||
"FunctionDeclaration",
|
||||
"TSInterfaceDeclaration",
|
||||
"DeclareClass",
|
||||
"DeclareFunction",
|
||||
"TSDeclareFunction",
|
||||
"EnumDeclaration",
|
||||
]);
|
||||
function printSemicolonAfterExportDeclaration(node, options) {
|
||||
if (
|
||||
isDefaultExport &&
|
||||
declarationType !== "ClassDeclaration" &&
|
||||
declarationType !== "FunctionDeclaration" &&
|
||||
declarationType !== "TSInterfaceDeclaration" &&
|
||||
declarationType !== "DeclareClass" &&
|
||||
declarationType !== "DeclareFunction" &&
|
||||
declarationType !== "TSDeclareFunction" &&
|
||||
declarationType !== "EnumDeclaration"
|
||||
options.semi &&
|
||||
(!node.declaration ||
|
||||
(isDefaultExport(node) && !shouldOmitSemicolon(node.declaration)))
|
||||
) {
|
||||
return true;
|
||||
return ";";
|
||||
}
|
||||
return false;
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
function printImportOrExportKind(kind, spaceBeforeKind = true) {
|
||||
return kind && kind !== "value"
|
||||
? `${spaceBeforeKind ? " " : ""}${kind}${spaceBeforeKind ? "" : " "}`
|
||||
: "";
|
||||
}
|
||||
|
||||
function printImportKind(node, spaceBeforeKind) {
|
||||
return printImportOrExportKind(node.importKind, spaceBeforeKind);
|
||||
}
|
||||
|
||||
function printExportKind(node) {
|
||||
return printImportOrExportKind(node.exportKind);
|
||||
}
|
||||
|
||||
function printModuleSource(path, options, print) {
|
||||
@@ -278,36 +248,26 @@ function shouldNotPrintSpecifiers(node, options) {
|
||||
|
||||
function printImportAssertions(path, options, print) {
|
||||
const { node } = path;
|
||||
if (isNonEmptyArray(node.assertions)) {
|
||||
return [
|
||||
" assert {",
|
||||
options.bracketSpacing ? " " : "",
|
||||
join(", ", path.map(print, "assertions")),
|
||||
options.bracketSpacing ? " " : "",
|
||||
"}",
|
||||
];
|
||||
if (!isNonEmptyArray(node.assertions)) {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
|
||||
return [
|
||||
" assert {",
|
||||
options.bracketSpacing ? " " : "",
|
||||
join(", ", path.map(print, "assertions")),
|
||||
options.bracketSpacing ? " " : "",
|
||||
"}",
|
||||
];
|
||||
}
|
||||
|
||||
function printModuleSpecifier(path, options, print) {
|
||||
const { node } = path;
|
||||
|
||||
const { type } = node;
|
||||
|
||||
/** @type {Doc[]} */
|
||||
const parts = [];
|
||||
|
||||
/** @type {"type" | "typeof" | "value"} */
|
||||
const kind = type === "ImportSpecifier" ? node.importKind : node.exportKind;
|
||||
|
||||
if (kind && kind !== "value") {
|
||||
parts.push(kind, " ");
|
||||
}
|
||||
|
||||
const isImport = type.startsWith("Import");
|
||||
const leftSideProperty = isImport ? "imported" : "local";
|
||||
const rightSideProperty = isImport ? "local" : "exported";
|
||||
const isImportSpecifier = type.startsWith("Import");
|
||||
const leftSideProperty = isImportSpecifier ? "imported" : "local";
|
||||
const rightSideProperty = isImportSpecifier ? "local" : "exported";
|
||||
const leftSideNode = node[leftSideProperty];
|
||||
const rightSideNode = node[rightSideProperty];
|
||||
let left = "";
|
||||
@@ -325,8 +285,15 @@ function printModuleSpecifier(path, options, print) {
|
||||
right = print(rightSideProperty);
|
||||
}
|
||||
|
||||
parts.push(left, left && right ? " as " : "", right);
|
||||
return parts;
|
||||
return [
|
||||
printImportOrExportKind(
|
||||
type === "ImportSpecifier" ? node.importKind : node.exportKind,
|
||||
/* spaceBeforeKind */ false
|
||||
),
|
||||
left,
|
||||
left && right ? " as " : "",
|
||||
right,
|
||||
];
|
||||
}
|
||||
|
||||
function isShorthandSpecifier(specifier) {
|
||||
@@ -369,6 +336,6 @@ function isShorthandSpecifier(specifier) {
|
||||
export {
|
||||
printImportDeclaration,
|
||||
printExportDeclaration,
|
||||
printExportAllDeclaration,
|
||||
printModuleSpecifier,
|
||||
printImportKind,
|
||||
};
|
||||
@@ -56,6 +56,7 @@ import {
|
||||
printTypeQuery,
|
||||
} from "./type-annotation.js";
|
||||
import { printEnumDeclaration, printEnumMember } from "./enum.js";
|
||||
import { printImportKind } from "./module.js";
|
||||
|
||||
function printTypescript(path, options, print) {
|
||||
const { node } = path;
|
||||
@@ -302,13 +303,7 @@ function printTypescript(path, options, print) {
|
||||
return group(parts);
|
||||
}
|
||||
case "TSNamespaceExportDeclaration":
|
||||
parts.push("export as namespace ", print("id"));
|
||||
|
||||
if (options.semi) {
|
||||
parts.push(";");
|
||||
}
|
||||
|
||||
return group(parts);
|
||||
return ["export as namespace ", print("id"), options.semi ? ";" : ""];
|
||||
case "TSEnumDeclaration":
|
||||
return printEnumDeclaration(path, print, options);
|
||||
|
||||
@@ -316,23 +311,15 @@ function printTypescript(path, options, print) {
|
||||
return printEnumMember(path, print);
|
||||
|
||||
case "TSImportEqualsDeclaration":
|
||||
if (node.isExport) {
|
||||
parts.push("export ");
|
||||
}
|
||||
|
||||
parts.push("import ");
|
||||
|
||||
if (node.importKind && node.importKind !== "value") {
|
||||
parts.push(node.importKind, " ");
|
||||
}
|
||||
|
||||
parts.push(print("id"), " = ", print("moduleReference"));
|
||||
|
||||
if (options.semi) {
|
||||
parts.push(";");
|
||||
}
|
||||
|
||||
return group(parts);
|
||||
return [
|
||||
node.isExport ? "export " : "",
|
||||
"import ",
|
||||
printImportKind(node, /* spaceBeforeKind */ false),
|
||||
print("id"),
|
||||
" = ",
|
||||
print("moduleReference"),
|
||||
options.semi ? ";" : "",
|
||||
];
|
||||
case "TSExternalModuleReference":
|
||||
return ["require(", print("expression"), ")"];
|
||||
case "TSModuleDeclaration": {
|
||||
|
||||
@@ -56,7 +56,6 @@ import {
|
||||
import {
|
||||
printImportDeclaration,
|
||||
printExportDeclaration,
|
||||
printExportAllDeclaration,
|
||||
printModuleSpecifier,
|
||||
} from "./print/module.js";
|
||||
import { printTernary } from "./print/ternary.js";
|
||||
@@ -339,9 +338,8 @@ function printPathNoParens(path, options, print, args) {
|
||||
|
||||
case "ExportDefaultDeclaration":
|
||||
case "ExportNamedDeclaration":
|
||||
return printExportDeclaration(path, options, print);
|
||||
case "ExportAllDeclaration":
|
||||
return printExportAllDeclaration(path, options, print);
|
||||
return printExportDeclaration(path, options, print);
|
||||
case "ImportDeclaration":
|
||||
return printImportDeclaration(path, options, print);
|
||||
case "ImportSpecifier":
|
||||
|
||||
Reference in new issue
Block a user