Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function member in type alias / interface shouldn't be considerd as method #41

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
51 changes: 14 additions & 37 deletions lib/comparator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import bt from "@babel/types";
import { C, Comparator } from "./comparator";
import { select } from "./select";
Expand All @@ -19,34 +18,24 @@ export function comparator(options: Partial<Options>): Comparator<MemberNode> {
const alpha = options.sortMembersAlphabetically === true;
return C.chain<MemberNode>(
// signature
C.capture(
node(MemberTypes.TSIndexSignature),
C.by(functionSignature, C.defer),
),
C.capture(node(MemberTypes.TSIndexSignature), C.nop),

// field
C.capture(
select
.or(
select.and(
node(MemberTypes.TSPropertySignature),
select.not(functionSignature),
),
)
.or(
select.and(
select
.or(node(MemberTypes.PropertyDefinition))
.or(node(MemberTypes.TSAbstractPropertyDefinition))
.or(
select.and(
bt.isNode,
select.or(bt.isClassProperty).or(bt.isClassPrivateProperty),
),
select.or(node(MemberTypes.TSPropertySignature)).or(
select.and(
select
.or(node(MemberTypes.PropertyDefinition))
.or(node(MemberTypes.TSAbstractPropertyDefinition))
.or(
select.and(
bt.isNode,
select.or(bt.isClassProperty).or(bt.isClassPrivateProperty),
),
($) => !($.value && functionExpressions.includes($.value.type)),
),
),
($) => !($.value && functionExpressions.includes($.value.type)),
),
),
C.chain(
classMember(),
C.by(decorated, C.prefer),
Expand Down Expand Up @@ -103,9 +92,7 @@ export function comparator(options: Partial<Options>): Comparator<MemberNode> {
$.value != null && functionExpressions.includes($.value.type),
),
)
.or(
select.and(node(MemberTypes.TSPropertySignature), functionSignature),
),
.or(node(MemberTypes.TSPropertySignature)),
C.chain(
methodKind(),
classMember(),
Expand All @@ -118,16 +105,6 @@ export function comparator(options: Partial<Options>): Comparator<MemberNode> {
);
}

function functionSignature(
node: MemberNode<
AST_NODE_TYPES.TSPropertySignature | AST_NODE_TYPES.TSIndexSignature
>,
): boolean {
return (
node.typeAnnotation?.typeAnnotation.type === AST_NODE_TYPES.TSFunctionType
);
}

function node<K extends MemberType>(key: K) {
return function (node: MemberNode): node is MemberNode<K> {
return node.type === key;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"check": "bun test && tsc && eslint ."
},
"devDependencies": {
"@types/eslint": "^8.56.9",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"bun-types": "latest",
Expand Down
112 changes: 98 additions & 14 deletions tests/format/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ type Y = {
};

type Z = {
edge: () => void;
dog: unknown;
"change-value": () => void;
"big-value": number;
"another-one": 0;
edge: () => void;
"change-value": () => void;
};

interface A {
Expand Down Expand Up @@ -119,6 +119,33 @@ function deco(a: unknown, b: unknown): void {}
"
`;

exports[`format {} issue-40-function-member.ts 1`] = `
"type Link = {
foo: () => void;
text: string;
onClick: () => void;
};

type Literal = {
func: () => void;
value: number;
method(): void;
};

interface Interface {
func: () => void;
value: number;
method(): void;
}

class Class {
value = 0;
method(): void {}
func = () => 0;
}
"
`;

exports[`format {} issue-34-literal-keys.js 1`] = `
"class A {
[fruit] = 1;
Expand Down Expand Up @@ -187,6 +214,7 @@ interface F {
b: 0;
[\`e\`]: 0;
[\`f\`]: 0;
j: () => 0;
new (): F;
new (a: 0): F;
new (a: 0, b: 0): F;
Expand All @@ -197,24 +225,24 @@ interface F {
}

interface G {
[d: symbol]: 0;
[c: number]: () => 0;
[d: symbol]: 0;
a: 0;
}
"
`;

exports[`format {} type-alias.ts 1`] = `
"type A = {
[t: string]: unknown;
[u: number]: () => number;
[t: string]: unknown;
y: number;
x: () => () => number;
[\`s\`]: number;
r?: number;
new (a: 0, b: 1): A;
get w(): number;
z(): number;
x: () => () => number;
v?(): number;
};
"
Expand Down Expand Up @@ -274,8 +302,8 @@ type Y = {
type Z = {
"another-one": 0;
"big-value": number;
dog: unknown;
"change-value": () => void;
dog: unknown;
edge: () => void;
};

Expand Down Expand Up @@ -364,6 +392,33 @@ function deco(a: unknown, b: unknown): void {}
"
`;

exports[`format {"sortMembersAlphabetically":true} issue-40-function-member.ts 1`] = `
"type Link = {
foo: () => void;
onClick: () => void;
text: string;
};

type Literal = {
func: () => void;
value: number;
method(): void;
};

interface Interface {
func: () => void;
value: number;
method(): void;
}

class Class {
value = 0;
func = () => 0;
method(): void {}
}
"
`;

exports[`format {"sortMembersAlphabetically":true} issue-34-literal-keys.js 1`] = `
"class A {
["apple"] = 1;
Expand Down Expand Up @@ -430,6 +485,7 @@ interface F {
[c: number]: 0;
[d: symbol]: 0;
b: 0;
j: () => 0;
[\`e\`]: 0;
[\`f\`]: 0;
new (): F;
Expand All @@ -442,24 +498,24 @@ interface F {
}

interface G {
[d: symbol]: 0;
[c: number]: () => 0;
[d: symbol]: 0;
a: 0;
}
"
`;

exports[`format {"sortMembersAlphabetically":true} type-alias.ts 1`] = `
"type A = {
[t: string]: unknown;
[u: number]: () => number;
[t: string]: unknown;
r?: number;
x: () => () => number;
y: number;
[\`s\`]: number;
new (a: 0, b: 1): A;
get w(): number;
v?(): number;
x: () => () => number;
z(): number;
};
"
Expand Down Expand Up @@ -517,11 +573,11 @@ type Y = {
};

type Z = {
edge: () => void;
dog: unknown;
"change-value": () => void;
"big-value": number;
"another-one": 0;
edge: () => void;
"change-value": () => void;
};

interface A {
Expand Down Expand Up @@ -609,6 +665,33 @@ function deco(a: unknown, b: unknown): void {}
"
`;

exports[`format {"sortMembersAlphabetically":false} issue-40-function-member.ts 1`] = `
"type Link = {
foo: () => void;
text: string;
onClick: () => void;
};

type Literal = {
func: () => void;
value: number;
method(): void;
};

interface Interface {
func: () => void;
value: number;
method(): void;
}

class Class {
value = 0;
method(): void {}
func = () => 0;
}
"
`;

exports[`format {"sortMembersAlphabetically":false} issue-34-literal-keys.js 1`] = `
"class A {
[fruit] = 1;
Expand Down Expand Up @@ -677,6 +760,7 @@ interface F {
b: 0;
[\`e\`]: 0;
[\`f\`]: 0;
j: () => 0;
new (): F;
new (a: 0): F;
new (a: 0, b: 0): F;
Expand All @@ -687,24 +771,24 @@ interface F {
}

interface G {
[d: symbol]: 0;
[c: number]: () => 0;
[d: symbol]: 0;
a: 0;
}
"
`;

exports[`format {"sortMembersAlphabetically":false} type-alias.ts 1`] = `
"type A = {
[t: string]: unknown;
[u: number]: () => number;
[t: string]: unknown;
y: number;
x: () => () => number;
[\`s\`]: number;
r?: number;
new (a: 0, b: 1): A;
get w(): number;
z(): number;
x: () => () => number;
v?(): number;
};
"
Expand Down
30 changes: 30 additions & 0 deletions tests/format/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, test, expect } from "bun:test";
import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { format } from "prettier";
import { ESLint } from "eslint";

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

Expand Down Expand Up @@ -72,5 +73,34 @@ describe("format", () => {
});
});
});

describe("compatible with eslint-typescript", () => {
const eslint = new ESLint({
overrideConfig: {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [],
rules: {
"@typescript-eslint/member-ordering": "error",
},
},
useEslintrc: false,
});

test.each(filenames)("%s", async (name) => {
const path = join(dir, name);
const code = await readFile(path, "utf-8");
const result = await format(code, {
...opts,
filepath: path,
plugins,
});

const lintResults = await eslint.lintText(result);

expect(lintResults).toHaveLength(1);
expect(lintResults[0].messages).toBeEmpty();
});
});
});
});
1 change: 1 addition & 0 deletions tests/format/testdata/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ interface F {
[`g`](): 0;
set h(_: 0);
get i(): 0;
j: () => 0;
}

interface G {
Expand Down
Loading
Loading