Skip to content

Commit

Permalink
Merge branch 'main' into ts-4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg authored Jun 15, 2023
2 parents bc61fbe + e11775e commit 589e073
Show file tree
Hide file tree
Showing 9 changed files with 822 additions and 32 deletions.
52 changes: 52 additions & 0 deletions src/comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import ts from "typescript";
import { describe, expect, it, vitest } from "vitest";

import { forEachComment } from "./comments";
import { createNodeAndSourceFile } from "./test/utils";
import { isTsVersionAtLeast } from "./utils";

describe("forEachComment", () => {
it("does not call the callback when the source has no comments", () => {
const { node, sourceFile } = createNodeAndSourceFile("let value;");
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).not.toHaveBeenCalled();
});

if (isTsVersionAtLeast(4, 3)) {
it("calls the callback when the source has a leading comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
// hello world
let value;
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
})
);
});

it("calls the callback when the source has a trailing comment", () => {
const { node, sourceFile } = createNodeAndSourceFile(`
let value; // hello world
`);
const callback = vitest.fn();

forEachComment(node, callback, sourceFile);

expect(callback).toHaveBeenCalledWith(
sourceFile.getFullText(),
expect.objectContaining({
kind: ts.SyntaxKind.SingleLineCommentTrivia,
})
);
});
}
});
93 changes: 78 additions & 15 deletions src/nodes/typeGuards/compound.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ import { describe, expect, it } from "vitest";
import { createNode } from "../../test/utils";
import {
isConstAssertionExpression,
isIterationStatement,
isNamedDeclarationWithName,
isNamespaceDeclaration,
isNumericOrStringLikeLiteral,
isSuperElementAccessExpression,
isSuperPropertyAccessExpression,
} from "./compound";

describe("isNumericOrStringLikeLiteral", () => {
it.each([
[false, "an identifier", ts.factory.createIdentifier("abc")],
[
true,
"a no substitution template literal",
ts.factory.createNoSubstitutionTemplateLiteral("abc"),
],
[true, "a numeric literal", ts.factory.createNumericLiteral("123")],
[true, "a string literal", ts.factory.createStringLiteral("abc")],
])("returns %j when given %s", (expected, _, node) => {
expect(isNumericOrStringLikeLiteral(node)).toBe(expected);
});
});

describe("isConstAssertionExpression", () => {
it.each([
[
Expand Down Expand Up @@ -65,3 +55,76 @@ describe("isConstAssertionExpression", () => {
expect(isConstAssertionExpression(createNode(node))).toBe(expected);
});
});

describe("isIterationStatement", () => {
it.each([
[true, "do {} while (0)"],
[true, "for (;;) {}"],
[true, "for (const _ in {}) {}"],
[true, "for (const _ of []) {}"],
[true, "while (0) {}"],
[false, "let _"],
])("returns %j when given %s", (expected, code) => {
expect(isIterationStatement(createNode(code))).toBe(expected);
});
});

describe("isNamedDeclarationWithName", () => {
it.each([
[true, "class HasName {}"],
[false, "const _ = class {}"],
[false, "let _"],
])("returns %j when given %s", (expected, code) => {
expect(isNamedDeclarationWithName(createNode<ts.Declaration>(code))).toBe(
expected
);
});
});

describe("isNamespaceDeclaration", () => {
it.each([
[true, "namespace A {}"],
[true, "namespace A.B {}"],
[false, "let _"],
])("returns %j when given %s", (expected, code) => {
expect(isNamespaceDeclaration(createNode(code))).toBe(expected);
});
});

describe("isNumericOrStringLikeLiteral", () => {
it.each([
[false, "an identifier", ts.factory.createIdentifier("abc")],
[
true,
"a no substitution template literal",
ts.factory.createNoSubstitutionTemplateLiteral("abc"),
],
[true, "a numeric literal", ts.factory.createNumericLiteral("123")],
[true, "a string literal", ts.factory.createStringLiteral("abc")],
])("returns %j when given %s", (expected, _, node) => {
expect(isNumericOrStringLikeLiteral(node)).toBe(expected);
});
});

describe("isSuperElementAccessExpression", () => {
it.each([
[true, "super['property']"],
[false, "super.property"],
[false, "super.method()"],
[false, "super()"],
[false, "other()"],
])("returns %j when given %s", (expected, code) => {
expect(isSuperElementAccessExpression(createNode(code))).toBe(expected);
});
});

describe("isSuperPropertyAccessExpression", () => {
it.each([
[true, "super.property"],
[false, "super.method()"],
[false, "super()"],
[false, "other()"],
])("returns %j when given %s", (expected, code) => {
expect(isSuperPropertyAccessExpression(createNode(code))).toBe(expected);
});
});
6 changes: 3 additions & 3 deletions src/nodes/typeGuards/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export function isIterationStatement(
node: ts.Node
): node is ts.IterationStatement {
switch (node.kind) {
case ts.SyntaxKind.ForStatement:
case ts.SyntaxKind.ForOfStatement:
case ts.SyntaxKind.DoStatement:
case ts.SyntaxKind.ForInStatement:
case ts.SyntaxKind.ForOfStatement:
case ts.SyntaxKind.ForStatement:
case ts.SyntaxKind.WhileStatement:
case ts.SyntaxKind.DoStatement:
return true;
default:
return false;
Expand Down
Loading

0 comments on commit 589e073

Please sign in to comment.