Refactor Enum print (#17497)
This commit is contained in:
1 parent
230bba53a9
commit
3c1475b1e7
8 files changed
+64
-55
No files matched your search
@@ -189,11 +189,6 @@ function clean(original, cloned) {
|
||||
cloned.type = "TSNonNullExpression";
|
||||
cloned.expression.type = "ChainExpression";
|
||||
}
|
||||
|
||||
// `@typescript-eslint/typescript-estree` v8
|
||||
if (original.type === "TSEnumDeclaration") {
|
||||
delete cloned.body;
|
||||
}
|
||||
}
|
||||
|
||||
clean.ignoredProperties = ignoredProperties;
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert";
|
||||
import { locEnd, locStart } from "../../loc.js";
|
||||
import createTypeCheckFunction from "../../utils/create-type-check-function.js";
|
||||
import getRaw from "../../utils/get-raw.js";
|
||||
import getTextWithoutComments from "../../utils/get-text-without-comments.js";
|
||||
import isBlockComment from "../../utils/is-block-comment.js";
|
||||
import isIndentableBlockComment from "../../utils/is-indentable-block-comment.js";
|
||||
import isLineComment from "../../utils/is-line-comment.js";
|
||||
@@ -173,6 +174,30 @@ function postprocess(ast, options) {
|
||||
delete node.typeParameter;
|
||||
}
|
||||
break;
|
||||
|
||||
// Remove this when update `@babel/parser` to v8
|
||||
// https://github.com/typescript-eslint/typescript-eslint/pull/8920
|
||||
case "TSEnumDeclaration":
|
||||
if (!node.body) {
|
||||
const idEnd = locEnd(node.id);
|
||||
const { members } = node;
|
||||
const textWithoutComments = getTextWithoutComments(
|
||||
{
|
||||
originalText: text,
|
||||
[Symbol.for("comments")]: ast.comments,
|
||||
},
|
||||
idEnd,
|
||||
members[0] ? locStart(members[0]) : locEnd(node),
|
||||
);
|
||||
const start = idEnd + textWithoutComments.indexOf("{");
|
||||
node.body = {
|
||||
type: "TSEnumBody",
|
||||
members,
|
||||
range: [start, locEnd(node)],
|
||||
};
|
||||
delete node.members;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* c8 ignore next 3 */
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
import { printDeclareToken } from "./misc.js";
|
||||
import { printObject as printEnumMembers } from "./object.js";
|
||||
|
||||
/*
|
||||
- `EnumBooleanBody`(flow)
|
||||
- `EnumNumberBody`(flow)
|
||||
- `EnumBigIntBody`(flow)
|
||||
- `EnumStringBody`(flow)
|
||||
- `EnumSymbolBody`(flow)
|
||||
- `TSEnumBody`(TypeScript)
|
||||
*/
|
||||
function printEnumBody(path, options, print) {
|
||||
return printEnumMembers(path, options, print);
|
||||
}
|
||||
|
||||
/*
|
||||
- `EnumBooleanMember`(flow)
|
||||
- `EnumNumberMember`(flow)
|
||||
@@ -37,46 +49,12 @@ function printEnumMember(path, print) {
|
||||
return [idDoc, " = ", initializerDoc];
|
||||
}
|
||||
|
||||
/*
|
||||
- `EnumBooleanBody`(flow)
|
||||
- `EnumNumberBody`(flow)
|
||||
- `EnumBigIntBody`(flow)
|
||||
- `EnumStringBody`(flow)
|
||||
- `EnumSymbolBody`(flow)
|
||||
*/
|
||||
function printEnumBody(path, options, print) {
|
||||
const { node } = path;
|
||||
let type;
|
||||
|
||||
if (node.type === "EnumSymbolBody" || node.explicitType) {
|
||||
switch (node.type) {
|
||||
case "EnumBooleanBody":
|
||||
type = "boolean";
|
||||
break;
|
||||
case "EnumNumberBody":
|
||||
type = "number";
|
||||
break;
|
||||
case "EnumBigIntBody":
|
||||
type = "bigint";
|
||||
break;
|
||||
case "EnumStringBody":
|
||||
type = "string";
|
||||
break;
|
||||
case "EnumSymbolBody":
|
||||
type = "symbol";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [type ? `of ${type} ` : "", printEnumMembers(path, options, print)];
|
||||
}
|
||||
|
||||
/*
|
||||
- `DeclareEnum`(flow)
|
||||
- `EnumDeclaration`(flow)
|
||||
- `TSEnumDeclaration`(TypeScript)
|
||||
*/
|
||||
function printEnumDeclaration(path, options, print) {
|
||||
function printEnumDeclaration(path, print) {
|
||||
const { node } = path;
|
||||
return [
|
||||
printDeclareToken(path),
|
||||
@@ -84,9 +62,7 @@ function printEnumDeclaration(path, options, print) {
|
||||
"enum ",
|
||||
print("id"),
|
||||
" ",
|
||||
node.type === "TSEnumDeclaration"
|
||||
? printEnumMembers(path, options, print)
|
||||
: print("body"),
|
||||
print("body"),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -159,14 +159,26 @@ function printFlow(path, options, print) {
|
||||
|
||||
case "DeclareEnum":
|
||||
case "EnumDeclaration":
|
||||
return printEnumDeclaration(path, options, print);
|
||||
return printEnumDeclaration(path, print);
|
||||
|
||||
case "EnumBooleanBody":
|
||||
case "EnumNumberBody":
|
||||
case "EnumBigIntBody":
|
||||
case "EnumStringBody":
|
||||
case "EnumSymbolBody":
|
||||
return printEnumBody(path, options, print);
|
||||
return [
|
||||
node.type === "EnumSymbolBody" || node.explicitType
|
||||
? `of ${node.type
|
||||
.slice(
|
||||
// `Enum`
|
||||
4,
|
||||
// `Body`
|
||||
-4,
|
||||
)
|
||||
.toLowerCase()} `
|
||||
: "",
|
||||
printEnumBody(path, options, print),
|
||||
];
|
||||
|
||||
case "EnumBooleanMember":
|
||||
case "EnumNumberMember":
|
||||
|
||||
@@ -32,7 +32,7 @@ function printObject(path, options, print) {
|
||||
|
||||
const isTypeAnnotation = node.type === "ObjectTypeAnnotation";
|
||||
const isEnumBody =
|
||||
node.type === "TSEnumDeclaration" ||
|
||||
node.type === "TSEnumBody" ||
|
||||
node.type === "EnumBooleanBody" ||
|
||||
node.type === "EnumNumberBody" ||
|
||||
node.type === "EnumBigIntBody" ||
|
||||
|
||||
@@ -18,7 +18,11 @@ import { printBlock } from "./block.js";
|
||||
import printCallArguments from "./call-arguments.js";
|
||||
import { printBinaryCastExpression } from "./cast-expression.js";
|
||||
import { printClassMethod, printClassProperty } from "./class.js";
|
||||
import { printEnumDeclaration, printEnumMember } from "./enum.js";
|
||||
import {
|
||||
printEnumBody,
|
||||
printEnumDeclaration,
|
||||
printEnumMember,
|
||||
} from "./enum.js";
|
||||
import { printFunction, printMethodValue } from "./function.js";
|
||||
import {
|
||||
printFunctionParameters,
|
||||
@@ -274,8 +278,9 @@ function printTypescript(path, options, print) {
|
||||
case "TSNamespaceExportDeclaration":
|
||||
return ["export as namespace ", print("id"), options.semi ? ";" : ""];
|
||||
case "TSEnumDeclaration":
|
||||
return printEnumDeclaration(path, options, print);
|
||||
|
||||
return printEnumDeclaration(path, print);
|
||||
case "TSEnumBody":
|
||||
return printEnumBody(path, options, print);
|
||||
case "TSEnumMember":
|
||||
return printEnumMember(path, print);
|
||||
|
||||
|
||||
@@ -63,11 +63,7 @@ const excludeKeys = {
|
||||
ImportDeclaration: ["assertions"],
|
||||
ImportExpression: ["attributes"],
|
||||
TSMappedType: ["typeParameter"],
|
||||
|
||||
// `body` added in `@typescript-eslint/typescript-estree` v8
|
||||
// https://github.com/typescript-eslint/typescript-eslint/pull/8920
|
||||
// TODO: Use the new AST properties instead
|
||||
TSEnumDeclaration: ["body"],
|
||||
TSEnumDeclaration: ["members"],
|
||||
};
|
||||
|
||||
const excludeNodeTypes = new Set([
|
||||
|
||||
@@ -818,8 +818,8 @@ exports[`visitor keys estree 1`] = `
|
||||
"members",
|
||||
],
|
||||
"TSEnumDeclaration": [
|
||||
"body",
|
||||
"id",
|
||||
"members",
|
||||
],
|
||||
"TSEnumMember": [
|
||||
"id",
|
||||
|
||||
Reference in new issue
Block a user