Skip to content

Commit b19e680

Browse files
committed
Split AST misc.ts into format.ts and location.ts
1 parent 46b0c80 commit b19e680

3 files changed

Lines changed: 33 additions & 35 deletions

File tree

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,7 @@ import type { TSESTree } from "@typescript-eslint/types";
22
import { AST_NODE_TYPES as AST } from "@typescript-eslint/types";
33
import { delimiterCase, replace, toLowerCase } from "string-ts";
44

5-
import { isJSX, isOneOf } from "./is";
6-
7-
/**
8-
* Check if a node is multiline
9-
* @param node The AST node to check
10-
* @returns `true` if the node is multiline
11-
*/
12-
export function isMultiLine(node: TSESTree.Node) {
13-
return node.loc.start.line !== node.loc.end.line;
14-
}
15-
16-
/**
17-
* Check if a node is a line break
18-
* @param node The AST node to check
19-
* @returns boolean
20-
*/
21-
export function isLineBreak(node: TSESTree.Node) {
22-
return isOneOf([AST.Literal, AST.JSXText])(node)
23-
&& typeof node.value === "string"
24-
&& node.value.trim() === ""
25-
&& isMultiLine(node);
26-
}
27-
28-
/**
29-
* Get the type of a literal value
30-
* @param input The literal value
31-
* @returns The type of the literal value
32-
*/
33-
function getLiteralValueType(input: bigint | boolean | null | number | string | symbol) {
34-
// eslint-disable-next-line function-rule-2/function-rule
35-
if (input === null) return "null";
36-
return typeof input;
37-
}
5+
import { isJSX } from "./is";
386

397
/**
408
* Convert a node type to a delimiter format string
@@ -47,7 +15,11 @@ export function toDelimiterFormat(node: TSESTree.Node, delimiter = " ") {
4715
if ("regex" in node) {
4816
return "RegExp literal";
4917
}
50-
return `${getLiteralValueType(node.value)} literal` as const;
18+
// eslint-disable-next-line function-rule-2/function-rule
19+
if (node.value === null) {
20+
return "null literal" as const;
21+
}
22+
return `${typeof node.value} literal` as const;
5123
}
5224
if (isJSX(node)) {
5325
return `JSX ${toLowerCase(delimiterCase(replace(node.type, "JSX", ""), delimiter))}` as const;

packages/utilities/ast/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from "./expression-base";
77
export * from "./expression-is";
88
export * from "./expression-nested";
99
export * from "./file-directive";
10+
export * from "./format";
1011
export * from "./function-directive";
1112
export * from "./function-id";
1213
export * from "./function-init-path";
@@ -15,7 +16,7 @@ export * from "./identifier-is";
1516
export * from "./identifier-name";
1617
export * from "./is";
1718
export * from "./literal";
18-
export * from "./misc";
19+
export * from "./location";
1920
export * from "./process-env-node-env";
2021
export * from "./property-name";
2122
export * from "./selectors";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { TSESTree } from "@typescript-eslint/types";
2+
import { AST_NODE_TYPES as AST } from "@typescript-eslint/types";
3+
4+
import { isOneOf } from "./is";
5+
6+
/**
7+
* Check if a node spans multiple lines
8+
* @param node The AST node to check
9+
* @returns `true` if the node spans multiple lines, `false` otherwise
10+
*/
11+
export function isMultiLine(node: TSESTree.Node) {
12+
return node.loc.start.line !== node.loc.end.line;
13+
}
14+
15+
/**
16+
* Check if a node is a line break (whitespace spanning multiple lines)
17+
* @param node The AST node to check
18+
* @returns `true` if the node is a line break, `false` otherwise
19+
*/
20+
export function isLineBreak(node: TSESTree.Node) {
21+
return isOneOf([AST.Literal, AST.JSXText])(node)
22+
&& typeof node.value === "string"
23+
&& node.value.trim() === ""
24+
&& isMultiLine(node);
25+
}

0 commit comments

Comments
 (0)