///
import { fixturePath, getFirstNodeOfType } from "@local/testkit";
import { AST_NODE_TYPES as AST, type TSESTree } from "@typescript-eslint/types";
import { describe, expect, it } from "vitest";
import {
is,
isArrayPatternElementDefault,
isClass,
isConditionalOrLoop,
isDirective,
isExpression,
isFunction,
isFunctionOrClass,
isIdentifier,
isJSX,
isJSXElement,
isJSXElementOrFragment,
isJSXFragment,
isJSXTagNameExpression,
isObjectPatternElementDefault,
isOneOf,
isProperty,
isPropertyOrMethod,
isTypeAssertionExpression,
isTypeExpression,
} from "./check";
describe("is", () => {
it("should return true when the node matches the given type", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(is(AST.Identifier)(node)).toBe(true);
});
it("should return false when the node does not match the given type", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Literal);
expect(is(AST.Identifier)(node)).toBe(false);
});
});
describe("isOneOf", () => {
it("should return true when the node matches one of the given types", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Literal);
expect(isOneOf([AST.Identifier, AST.Literal])(node)).toBe(true);
});
it("should return false when the node matches none of the given types", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isOneOf([AST.Literal, AST.CallExpression])(node)).toBe(false);
});
});
describe("isIdentifier", () => {
it("should return true for an Identifier node", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isIdentifier(node)).toBe(true);
});
it("should return false for a non-Identifier node", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Literal);
expect(isIdentifier(node)).toBe(false);
});
it("should return true when the identifier name matches", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isIdentifier(node, "a")).toBe(true);
});
it("should return false when the identifier name does not match", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isIdentifier(node, "b")).toBe(false);
});
});
describe("isDirective", () => {
it("should return true for a string literal expression statement", () => {
const node = getFirstNodeOfType('"use strict";', AST.ExpressionStatement);
expect(isDirective(node)).toBe(true);
});
it("should return false for a non-directive expression statement when no directive name is provided", () => {
const node = getFirstNodeOfType("foo();", AST.ExpressionStatement);
expect(isDirective(node)).toBe(false);
});
it("should return false for a non-directive expression statement when a directive name is provided", () => {
const node = getFirstNodeOfType("foo();", AST.ExpressionStatement);
expect(isDirective(node, "use strict")).toBe(false);
});
it("should return true when the directive name matches", () => {
const node = getFirstNodeOfType('"use strict";', AST.ExpressionStatement);
expect(isDirective(node, "use strict")).toBe(true);
});
it("should return false when the directive name does not match", () => {
const node = getFirstNodeOfType('"use strict";', AST.ExpressionStatement);
expect(isDirective(node, "use asm")).toBe(false);
});
});
describe("isArrayPatternElementDefault", () => {
it("should return true for an array pattern element default", () => {
const node = getFirstNodeOfType("const [a = 1] = arr;", AST.AssignmentPattern);
expect(isArrayPatternElementDefault(node)).toBe(true);
});
it("should return true for a direct destructuring assignment element default", () => {
const node = getFirstNodeOfType("[a = 1] = arr;", AST.AssignmentPattern);
expect(isArrayPatternElementDefault(node)).toBe(true);
});
it("should return false for an object pattern element default", () => {
const node = getFirstNodeOfType("const { a = 1 } = obj;", AST.AssignmentPattern);
expect(isArrayPatternElementDefault(node)).toBe(false);
});
it("should return false for a non-AssignmentPattern array pattern element", () => {
const node = getFirstNodeOfType("const [a] = arr;", AST.Identifier);
expect(isArrayPatternElementDefault(node)).toBe(false);
});
});
describe("isObjectPatternElementDefault", () => {
it.each([
["const { a = 1 } = obj;", "variable declaration"],
["function f({ a = 1 }) {}", "parameter"],
["({ a = 1 } = obj);", "destructuring assignment"],
["const [{ a = 1 }] = arr;", "object pattern nested in an array pattern"],
])("should return true for %s (%s)", (code) => {
const node = getFirstNodeOfType(code, AST.AssignmentPattern);
expect(isObjectPatternElementDefault(node)).toBe(true);
});
it("should return false for an array pattern element default", () => {
const node = getFirstNodeOfType("const [a = 1] = arr;", AST.AssignmentPattern);
expect(isObjectPatternElementDefault(node)).toBe(false);
});
it("should return false for a non-default object pattern property value", () => {
const node = getFirstNodeOfType("const { a } = obj;", AST.Identifier);
expect(isObjectPatternElementDefault(node)).toBe(false);
});
});
describe("isExpression", () => {
it("should return true for a JavaScript expression", () => {
const node = getFirstNodeOfType("foo();", AST.CallExpression);
expect(isExpression(node)).toBe(true);
});
it("should return true for a JSX expression", () => {
const node = getFirstNodeOfType("", AST.JSXElement);
expect(isExpression(node)).toBe(true);
});
it("should return true for a TypeScript expression", () => {
const node = getFirstNodeOfType("const a = 1 as number;", AST.TSAsExpression);
expect(isExpression(node)).toBe(true);
});
it("should return true for a pattern included in TSESTree.Expression", () => {
const node = getFirstNodeOfType("const [a] = value;", AST.ArrayPattern);
expect(isExpression(node)).toBe(true);
});
it("should return false for a non-expression node", () => {
const node = getFirstNodeOfType("foo();", AST.ExpressionStatement);
expect(isExpression(node)).toBe(false);
});
});
describe("isConditionalOrLoop", () => {
it("should return true for conditional and logical expressions", () => {
const conditional = getFirstNodeOfType("a ? b : c;", AST.ConditionalExpression);
const logical = getFirstNodeOfType("a && b;", AST.LogicalExpression);
expect(isConditionalOrLoop(conditional)).toBe(true);
expect(isConditionalOrLoop(logical)).toBe(true);
});
it("should return true for conditional statements", () => {
const ifStatement = getFirstNodeOfType("if (a) {}", AST.IfStatement);
const switchStatement = getFirstNodeOfType("switch (a) { default: break; }", AST.SwitchStatement);
expect(isConditionalOrLoop(ifStatement)).toBe(true);
expect(isConditionalOrLoop(switchStatement)).toBe(true);
});
it.each([
[AST.WhileStatement, "while (a) {}"],
[AST.ForStatement, "for (;;) {}"],
[AST.ForInStatement, "for (const key in object) {}"],
[AST.ForOfStatement, "for (const value of object) {}"],
[AST.DoWhileStatement, "do {} while (a);"],
])("should return true for %s", (type, code) => {
const node = getFirstNodeOfType(code, type);
expect(isConditionalOrLoop(node)).toBe(true);
});
it("should return false for other control flow statements", () => {
const node = getFirstNodeOfType("function f() { return; }", AST.ReturnStatement);
expect(isConditionalOrLoop(node)).toBe(false);
});
});
describe("isClass", () => {
it("should return true for a ClassDeclaration", () => {
const node = getFirstNodeOfType("class A {}", AST.ClassDeclaration);
expect(isClass(node)).toBe(true);
});
it("should return true for a ClassExpression", () => {
const node = getFirstNodeOfType("const A = class {}", AST.ClassExpression);
expect(isClass(node)).toBe(true);
});
it("should return false for a non-class node", () => {
const node = getFirstNodeOfType("function f() {}", AST.FunctionDeclaration);
expect(isClass(node)).toBe(false);
});
});
describe("isFunction", () => {
it("should return true for a FunctionDeclaration", () => {
const node = getFirstNodeOfType("function f() {}", AST.FunctionDeclaration);
expect(isFunction(node)).toBe(true);
});
it("should return true for a FunctionExpression", () => {
const node = getFirstNodeOfType("const f = function() {}", AST.FunctionExpression);
expect(isFunction(node)).toBe(true);
});
it("should return true for an ArrowFunctionExpression", () => {
const node = getFirstNodeOfType("const f = () => {}", AST.ArrowFunctionExpression);
expect(isFunction(node)).toBe(true);
});
it("should return false for a non-function node", () => {
const node = getFirstNodeOfType("class A {}", AST.ClassDeclaration);
expect(isFunction(node)).toBe(false);
});
});
describe("isFunctionOrClass", () => {
it("should return true for a ClassDeclaration", () => {
const node = getFirstNodeOfType("class A {}", AST.ClassDeclaration);
expect(isFunctionOrClass(node)).toBe(true);
});
it("should return true for a ClassExpression", () => {
const node = getFirstNodeOfType("const A = class {}", AST.ClassExpression);
expect(isFunctionOrClass(node)).toBe(true);
});
it("should return true for a FunctionDeclaration", () => {
const node = getFirstNodeOfType("function f() {}", AST.FunctionDeclaration);
expect(isFunctionOrClass(node)).toBe(true);
});
it("should return true for a FunctionExpression", () => {
const node = getFirstNodeOfType("const f = function() {}", AST.FunctionExpression);
expect(isFunctionOrClass(node)).toBe(true);
});
it("should return true for an ArrowFunctionExpression", () => {
const node = getFirstNodeOfType("const f = () => {}", AST.ArrowFunctionExpression);
expect(isFunctionOrClass(node)).toBe(true);
});
it("should return false for a non-class non-function node", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isFunctionOrClass(node)).toBe(false);
});
});
describe("isProperty", () => {
it("should return true for a PropertyDefinition", () => {
const node = getFirstNodeOfType("class A { x = 1; }", AST.PropertyDefinition);
expect(isProperty(node)).toBe(true);
});
it("should return true for a TSPropertySignature", () => {
const node = getFirstNodeOfType("interface A { x: number; }", AST.TSPropertySignature);
expect(isProperty(node)).toBe(true);
});
it("should return true for a TSIndexSignature", () => {
const node = getFirstNodeOfType("interface A { [key: string]: number; }", AST.TSIndexSignature);
expect(isProperty(node)).toBe(true);
});
it("should return true for a TSParameterProperty", () => {
const node = getFirstNodeOfType("class A { constructor(private x: number) {} }", AST.TSParameterProperty);
expect(isProperty(node)).toBe(true);
});
it("should return false for a MethodDefinition", () => {
const node = getFirstNodeOfType("class A { m() {} }", AST.MethodDefinition);
expect(isProperty(node)).toBe(false);
});
});
describe("isPropertyOrMethod", () => {
it("should return true for a PropertyDefinition", () => {
const node = getFirstNodeOfType("class A { x = 1; }", AST.PropertyDefinition);
expect(isPropertyOrMethod(node)).toBe(true);
});
it("should return true for a MethodDefinition", () => {
const node = getFirstNodeOfType("class A { m() {} }", AST.MethodDefinition);
expect(isPropertyOrMethod(node)).toBe(true);
});
it("should return false for a TSPropertySignature", () => {
const node = getFirstNodeOfType("interface A { x: number; }", AST.TSPropertySignature);
expect(isPropertyOrMethod(node)).toBe(false);
});
});
describe("isJSX", () => {
it("should return true for JSX nodes", () => {
const node = getFirstNodeOfType("", AST.JSXElement);
expect(isJSX(node)).toBe(true);
});
it("should return false for non-JSX nodes", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isJSX(node)).toBe(false);
});
});
describe("isJSXElement", () => {
it("should return true for a JSXElement", () => {
const node = getFirstNodeOfType("", AST.JSXElement);
expect(isJSXElement(node)).toBe(true);
});
it("should return false for a JSXFragment", () => {
const node = getFirstNodeOfType("<>>", AST.JSXFragment);
expect(isJSXElement(node)).toBe(false);
});
});
describe("isJSXFragment", () => {
it("should return true for a JSXFragment", () => {
const node = getFirstNodeOfType("<>>", AST.JSXFragment);
expect(isJSXFragment(node)).toBe(true);
});
it("should return false for a JSXElement", () => {
const node = getFirstNodeOfType("", AST.JSXElement);
expect(isJSXFragment(node)).toBe(false);
});
});
describe("isJSXElementOrFragment", () => {
it("should return true for a JSXElement", () => {
const node = getFirstNodeOfType("", AST.JSXElement);
expect(isJSXElementOrFragment(node)).toBe(true);
});
it("should return true for a JSXFragment", () => {
const node = getFirstNodeOfType("<>>", AST.JSXFragment);
expect(isJSXElementOrFragment(node)).toBe(true);
});
});
describe("isJSXTagNameExpression", () => {
it("should return true for a JSXIdentifier", () => {
const node = getFirstNodeOfType("", AST.JSXIdentifier);
expect(isJSXTagNameExpression(node)).toBe(true);
});
it("should return true for a JSXMemberExpression", () => {
const node = getFirstNodeOfType("", AST.JSXMemberExpression);
expect(isJSXTagNameExpression(node)).toBe(true);
});
it("should return true for a JSXNamespacedName", () => {
const node = getFirstNodeOfType("", AST.JSXNamespacedName);
expect(isJSXTagNameExpression(node)).toBe(true);
});
it("should return false for a JSXAttribute", () => {
const node = getFirstNodeOfType('', AST.JSXAttribute);
expect(isJSXTagNameExpression(node)).toBe(false);
});
});
describe("isTypeExpression", () => {
it("should return true for a TSAsExpression", () => {
const node = getFirstNodeOfType("const a = 1 as number;", AST.TSAsExpression);
expect(isTypeExpression(node)).toBe(true);
});
it("should return true for a TSTypeAssertion", () => {
const node = getFirstNodeOfType("const a = 1;", AST.TSTypeAssertion, {
filePath: fixturePath("estree.ts"),
jsx: false,
});
expect(isTypeExpression(node)).toBe(true);
});
it("should return true for a TSNonNullExpression", () => {
const node = getFirstNodeOfType("const a = b!;", AST.TSNonNullExpression);
expect(isTypeExpression(node)).toBe(true);
});
it("should return true for a TSSatisfiesExpression", () => {
const node = getFirstNodeOfType("const a = 1 satisfies number;", AST.TSSatisfiesExpression);
expect(isTypeExpression(node)).toBe(true);
});
it("should return true for a TSInstantiationExpression", () => {
const node = getFirstNodeOfType("const a = foo;", AST.TSInstantiationExpression);
expect(isTypeExpression(node)).toBe(true);
});
it("should return false for a non-type-expression node", () => {
const node = getFirstNodeOfType("const a = 1;", AST.Identifier);
expect(isTypeExpression(node)).toBe(false);
});
});
describe("isTypeAssertionExpression", () => {
it("should return true for a TSAsExpression", () => {
const node = getFirstNodeOfType("const a = 1 as number;", AST.TSAsExpression);
expect(isTypeAssertionExpression(node)).toBe(true);
});
it("should return true for a TSTypeAssertion", () => {
const node = getFirstNodeOfType("const a = 1;", AST.TSTypeAssertion, {
filePath: fixturePath("estree.ts"),
jsx: false,
});
expect(isTypeAssertionExpression(node)).toBe(true);
});
it("should return true for a TSNonNullExpression", () => {
const node = getFirstNodeOfType("const a = b!;", AST.TSNonNullExpression);
expect(isTypeAssertionExpression(node)).toBe(true);
});
it("should return true for a TSSatisfiesExpression", () => {
const node = getFirstNodeOfType("const a = 1 satisfies number;", AST.TSSatisfiesExpression);
expect(isTypeAssertionExpression(node)).toBe(true);
});
it("should return false for a TSInstantiationExpression", () => {
const node = getFirstNodeOfType("const a = foo;", AST.TSInstantiationExpression);
expect(isTypeAssertionExpression(node)).toBe(false);
});
});