Skip to content

Commit 0029030

Browse files
committed
refactor(ast): rename isKindOfLiteral to isLiteral and improve type support
1 parent c8d2b30 commit 0029030

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed

.pkgs/eslint-plugin-local/src/utils/is-initialized-from-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function isInitializedFromSource(
3333
// check for: `variable = require('source')` or `variable = require('source').variable`
3434
const args = getRequireExpressionArguments(init);
3535
const arg0 = args?.[0];
36-
if (arg0 == null || !AST.isKindOfLiteral(arg0, "string")) {
36+
if (arg0 == null || !AST.isLiteral(arg0, "string")) {
3737
return false;
3838
}
3939
// check for: `require('source')` or `require('source/...')`

packages/core/src/utils/is-initialized-from-react.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function isInitializedFromReact(
4949
// check for: `variable = require('react')` or `variable = require('react').variable`
5050
const args = getRequireExpressionArguments(init);
5151
const arg0 = args?.[0];
52-
if (arg0 == null || !AST.isKindOfLiteral(arg0, "string")) {
52+
if (arg0 == null || !AST.isLiteral(arg0, "string")) {
5353
return false;
5454
}
5555
// check for: `require('react')` or `require('react/...')`

packages/utilities/ast/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export * from "./get-top-level-identifier";
1313
export * from "./is";
1414
export * from "./is-array-map-call";
1515
export * from "./is-empty-function";
16-
export * from "./is-kind-of-literal";
1716
export * from "./is-line-break";
17+
export * from "./is-literal";
1818
export * from "./is-multi-line";
1919
export * from "./is-node-equal";
2020
export * from "./is-this-expression";

packages/utilities/ast/src/is-kind-of-literal.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable local/prefer-eqeq-nullish-comparison */
2+
import type { TSESTree } from "@typescript-eslint/types";
3+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
4+
5+
type LiteralType =
6+
| "boolean"
7+
| "null"
8+
| "number"
9+
| "regexp"
10+
| "string";
11+
12+
export function isLiteral(node: TSESTree.Node): node is TSESTree.Literal;
13+
export function isLiteral(node: TSESTree.Node, type: "boolean"): node is TSESTree.BooleanLiteral;
14+
export function isLiteral(node: TSESTree.Node, type: "null"): node is TSESTree.NullLiteral;
15+
export function isLiteral(node: TSESTree.Node, type: "number"): node is TSESTree.NumberLiteral;
16+
export function isLiteral(node: TSESTree.Node, type: "regexp"): node is TSESTree.RegExpLiteral;
17+
export function isLiteral(node: TSESTree.Node, type: "string"): node is TSESTree.StringLiteral;
18+
export function isLiteral(node: TSESTree.Node, type?: LiteralType) {
19+
if (node.type !== T.Literal) return false;
20+
if (type == null) return true;
21+
switch (type) {
22+
case "boolean":
23+
return typeof node.value === "boolean";
24+
case "null":
25+
return node.value === null;
26+
case "number":
27+
return typeof node.value === "number";
28+
case "regexp":
29+
return "regex" in node;
30+
case "string":
31+
return typeof node.value === "string";
32+
}
33+
}

0 commit comments

Comments
 (0)