Skip to content

Commit

Permalink
update dependencies (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyab authored Sep 18, 2024
1 parent a270488 commit b664704
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 50 deletions.
8 changes: 0 additions & 8 deletions .eslintrc.cjs

This file was deleted.

Binary file modified bun.lockb
Binary file not shown.
8 changes: 8 additions & 0 deletions config/eslint/language-options.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function languageOptions(dir) {
return {
parserOptions: {
projectService: true,
tsConfigDirName: dir,
},
};
}
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import { languageOptions } from "./config/eslint/language-options.mjs";

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
// @ts-expect-error fixme: resolve dirname
{ languageOptions: languageOptions(import.meta.dirname) },
{ ignores: ["tests/**/testdata/", "dist/"] },
{ files: ["**/*.mjs"], ...tseslint.configs.disableTypeChecked },
);
8 changes: 6 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { printers as estreePrinters } from "prettier/plugins/estree";
import { preprocess } from "./lib/preprocess";
import type { AST, SupportOption } from "prettier";
import type { ParserOptions, SupportOption } from "prettier";

const originalPreprocess = estreePrinters.estree.preprocess ?? ((x: AST) => x);
type AST = unknown;

const originalPreprocess: (x: AST, opts: ParserOptions<AST>) => AST =
estreePrinters.estree.preprocess ?? ((x: AST) => x);

estreePrinters.estree.preprocess = (x, options) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
preprocess(originalPreprocess(x, options), options);

export const options = {
Expand Down
2 changes: 0 additions & 2 deletions lib/ast/member-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export const MemberTypes = Object.fromEntries(
export type MemberType = (typeof MemberLikeNodeTypesArray)[number];

export type MemberNode<K extends MemberType = MemberType> = (
| never // avoid prettier bug

// babel-ast
| TSESTree.PropertyDefinition
| TSESTree.MethodDefinition
Expand Down
2 changes: 2 additions & 0 deletions lib/ast/member-like.typeckeck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
} from "./member-like";

(function _() {
/* eslint-disable @typescript-eslint/no-unused-expressions */
check<MemberType, NodeTypes>;
check<MemberNode["type"], MemberType>;
check<MemberType, MemberNode["type"]>;
check<typeof MemberLikeNodeTypesArray, readonly MemberType[]>;
check<MemberType, (typeof MemberLikeNodeTypesArray)[number]>;
/* eslint-enable @typescript-eslint/no-unused-expressions */

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function check<S extends T, T>(_a: T, _b: S): never {
Expand Down
1 change: 1 addition & 0 deletions lib/ast/name-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function nameOf(n: Node): string | null {
// babel nodes
switch (true) {
case isPrivateName(n.key):
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (n.key.id.type !== AST_NODE_TYPES.Identifier) return null;
return n.key.id.name;
case isStringLiteral(n.key):
Expand Down
20 changes: 11 additions & 9 deletions lib/comparator/comparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ export type Comparator<A> = (a: A, b: A) => number;

export const C = {
property<T, K extends keyof T>(
this: void,
key: K,
comp: Comparator<T[K]>,
): Comparator<T> {
return (a, b) => {
return comp(a[key], b[key]);
};
},
by<T, U>(fn: ($: T) => U, comp: Comparator<U>): Comparator<T> {
by<T, U>(this: void, fn: ($: T) => U, comp: Comparator<U>): Comparator<T> {
return (a, b) => {
return comp(fn(a), fn(b));
};
},
chain<T>(...comps: Comparator<T>[]): Comparator<T> {
chain<T>(this: void, ...comps: Comparator<T>[]): Comparator<T> {
return (a, b) => {
for (const comp of comps) {
const res = comp(a, b);
Expand All @@ -30,30 +31,30 @@ export const C = {
return Order.Equal;
};
},
nop(): Order {
nop(this: void,): Order {
return Order.Equal;
},
defer(a: boolean, b: boolean): Order {
defer(this: void, a: boolean, b: boolean): Order {
if (!!a === !!b) return Order.Equal;
if (a) return Order.Greater;
return Order.Less;
},
prefer(a: boolean, b: boolean): Order {
prefer(this: void, a: boolean, b: boolean): Order {
if (!!a === !!b) return Order.Equal;
if (a) return Order.Less;
return Order.Greater;
},
string(a: string, b: string): Order {
string(this: void, a: string, b: string): Order {
if (a < b) return Order.Less;
if (a > b) return Order.Greater;
return Order.Equal;
},
number(a: number, b: number): Order {
number(this: void, a: number, b: number): Order {
if (a < b) return Order.Less;
if (a > b) return Order.Greater;
return Order.Equal;
},
maybe<T>(comp: Comparator<T>): Comparator<T | undefined | null> {
maybe<T>(this: void, comp: Comparator<T>): Comparator<T | undefined | null> {
return (a, b) => {
if (a == null) {
if (b == null) return Order.Equal;
Expand All @@ -63,10 +64,11 @@ export const C = {
return comp(a, b);
};
},
reverse<T>(comp: Comparator<T>): Comparator<T> {
reverse<T>(this: void, comp: Comparator<T>): Comparator<T> {
return (a, b) => comp(b, a);
},
capture<T, U extends T>(
this: void,
pred: (a: T) => a is U,
comp: Comparator<U>,
): Comparator<T> {
Expand Down
16 changes: 8 additions & 8 deletions lib/comparator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export type Options = {

export function comparator(options: Partial<Options>): Comparator<MemberNode> {
const alpha = options.sortMembersAlphabetically === true;
const keepGettersAndSettersTogether = options.keepGettersAndSettersTogether ?? false;
const keepGettersAndSettersTogether =
options.keepGettersAndSettersTogether ?? false;
return C.chain<MemberNode>(
// signature
C.capture(node(MemberTypes.TSIndexSignature), C.nop),
Expand Down Expand Up @@ -50,14 +51,13 @@ export function comparator(options: Partial<Options>): Comparator<MemberNode> {
// constructor signature for interface
// constructor in class is handled as method
C.capture(
select
.or(node(MemberTypes.TSConstructSignatureDeclaration))
.or(
select.and(
node(MemberTypes.MethodDefinition),
($) => $.key.type === "Identifier" && $.key.name === "constructor",
),
select.or(node(MemberTypes.TSConstructSignatureDeclaration)).or(
select.and(
node(MemberTypes.MethodDefinition),
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
($) => $.key.type === "Identifier" && $.key.name === "constructor",
),
),
C.by(($) => {
if ($.type !== MemberTypes.TSConstructSignatureDeclaration) return 0;
return (
Expand Down
4 changes: 2 additions & 2 deletions lib/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export function visit<T extends Node>(
),
) as Set<keyof T>;
for (const key of keys) {
const k = key as keyof T;
const k = key satisfies keyof T;
const child = updatedNode[k];
if (Array.isArray(child)) {
updatedNode[k] = child.map((c) => visit(c, modifier)) as T[typeof k];
updatedNode[k] = (child).map((c: Node) => visit(c, modifier)) as T[typeof k];
continue;
}
updatedNode[k] = visit(updatedNode[k] as Node, modifier) as T[typeof k];
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
"check": "bun test && tsc && eslint ."
},
"devDependencies": {
"@types/eslint": "^8.56.9",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@eslint/js": "^9.9.0",
"@types/eslint": "^9.6.0",
"@types/eslint__js": "^8.42.3",
"bun-types": "latest",
"eslint": "^8.54.0",
"typescript": "^5.3.3"
"eslint": "^9.9.0",
"typescript": "^5.3.3",
"typescript-eslint": "^8.6.0"
},
"peerDependencies": {
"prettier": "^3.0.0"
},
"dependencies": {
"@babel/types": "^7.1.0",
"@typescript-eslint/types": "^7.1.0",
"@typescript-eslint/visitor-keys": "^7.1.0"
"@typescript-eslint/types": "^8.2.0",
"@typescript-eslint/visitor-keys": "^8.2.0"
},
"version": "0.2.0"
}
14 changes: 9 additions & 5 deletions tests/format/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { format } from "prettier";
import { ESLint } from "eslint";
import parser from "@typescript-eslint/parser";
import plugin from "@typescript-eslint/eslint-plugin";

const plugins = ["./index.ts"];

Expand Down Expand Up @@ -50,7 +52,7 @@ describe("format", () => {
describe("TypeScript", () => {
const skipFile = new Set(["issue-34-literal-keys.js"]);
const parsers = ["typescript", "babel-ts"];
describe.each(parsers)("%s", async (parser) => {
describe.each(parsers)("%s", (parser) => {
test.each(filenames.filter((n) => !skipFile.has(n)))(
"%s",
async (name) => {
Expand All @@ -77,14 +79,16 @@ describe("format", () => {
describe("compatible with eslint-typescript", () => {
const eslint = new ESLint({
overrideConfig: {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [],
languageOptions: { parser },
plugins: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
"@typescript-eslint": plugin as any,
},
rules: {
"@typescript-eslint/member-ordering": "error",
},
},
useEslintrc: false,
overrideConfigFile: true,
});

test.each(filenames)("%s", async (name) => {
Expand Down
16 changes: 10 additions & 6 deletions tests/keep-getters-and-setters-together/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { format } from "prettier";
import { ESLint } from "eslint";
import parser from "@typescript-eslint/parser";
import plugin from "@typescript-eslint/eslint-plugin";

const plugins = ["./index.ts"];

Expand All @@ -12,10 +14,10 @@ const files = await readdir(dir);
describe("@typescript-eslint/keep-getters-and-setters-together", () => {
describe.each([true, false])(
"keepGettersAndSettersTogether: %j",
async (keepGettersAndSettersTogether) => {
(keepGettersAndSettersTogether) => {
describe.each([true, false])(
"sortMembersAlphabetically: %j",
async (sortMembersAlphabetically) => {
(sortMembersAlphabetically) => {
test.each(files)("%s", async (name) => {
const opts = {
keepGettersAndSettersTogether,
Expand Down Expand Up @@ -57,14 +59,16 @@ describe("@typescript-eslint/keep-getters-and-setters-together", () => {
test.each(files)("no conflict: %s", async (name) => {
const eslint = new ESLint({
overrideConfig: {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [],
languageOptions: { parser },
plugins: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment
"@typescript-eslint": plugin as any,
},
rules: {
"@typescript-eslint/adjacent-overload-signatures": "error",
},
},
useEslintrc: false,
overrideConfigFile: true,
});
const opts = {
keepGettersAndSettersTogether: true,
Expand Down
2 changes: 1 addition & 1 deletion tests/skip-sort-for-subclass-of/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const files = await readdir(dir);

describe("react/sort-comp", () => {
const supers = [[[]], [["React.Component"]], [["Component"]], [["Promise"]]];
describe.each(supers)("%j", async (sp) => {
describe.each(supers)("%j", (sp) => {
test.each(files.filter((f) => f.startsWith("react")))(
"%s",
async (name) => {
Expand Down

0 comments on commit b664704

Please sign in to comment.