Skip to content

Commit

Permalink
refactor: add utility functions for type expressions and refactor 'no…
Browse files Browse the repository at this point in the history
…-duplicate-key'
  • Loading branch information
Rel1cx committed Dec 29, 2024
1 parent 23c17ad commit 0edd720
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,62 @@ ruleTester.run(RULE_NAME, rule, {
},
],
},
{
code: /* tsx */ `
const MyComponent1 = (() => null)!;
const MyComponent2 = (() => null)!!;
const MyComponent3 = (() => null)!!! as A;
const MyComponent4 = (() => null)!!! satisfies A;
const MyComponent5 = (() => null)!!! as A satisfies B;
`,
errors: [
{
messageId: "functionComponent",
data: {
name: "MyComponent1",
forwardRef: false,
hookCalls: 0,
memo: false,
},
},
{
messageId: "functionComponent",
data: {
name: "MyComponent2",
forwardRef: false,
hookCalls: 0,
memo: false,
},
},
{
messageId: "functionComponent",
data: {
name: "MyComponent3",
forwardRef: false,
hookCalls: 0,
memo: false,
},
},
{
messageId: "functionComponent",
data: {
name: "MyComponent4",
forwardRef: false,
hookCalls: 0,
memo: false,
},
},
{
messageId: "functionComponent",
data: {
name: "MyComponent5",
forwardRef: false,
hookCalls: 0,
memo: false,
},
},
],
},
],
valid: [
...allFunctions,
Expand All @@ -1375,25 +1431,21 @@ ruleTester.run(RULE_NAME, rule, {
"const results = allSettled.mapLike((x) => (x.status === 'fulfilled' ? format(x.value) : null))",
/* tsx */ `
export const action = (() => {
// ^?
return null;
});
`,
/* tsx */ `
export const action = (() => {
// ^?
return null;
}) as ActionFUnction;
`,
/* tsx */ `
export const action = (() => {
// ^?
return null;
}) satisfies ActionFUnction;
`,
/* tsx */ `
export const action = (() => {
// ^?
return null;
}) as ActionFUnction satisfies ActionFUnction;
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const RULE_FEATURES = [
export type MessageID = CamelCase<typeof RULE_NAME>;

function getName(node: TSESTree.Expression | TSESTree.PrivateIdentifier): O.Option<string> {
if (node.type === AST_NODE_TYPES.TSAsExpression) {
if (AST.isTypeOnlyExpression(node)) {
return getName(node.expression);
}
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const RULE_FEATURES = [
export type MessageID = CamelCase<typeof RULE_NAME>;

function getName(node: TSESTree.Expression | TSESTree.PrivateIdentifier): O.Option<string> {
if (node.type === AST_NODE_TYPES.TSAsExpression) {
if (AST.isTypeOnlyExpression(node)) {
return getName(node.expression);
}
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ ruleTester.run(RULE_NAME, rule, {
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
],
Expand All @@ -48,19 +48,19 @@ ruleTester.run(RULE_NAME, rule, {
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
],
Expand All @@ -75,7 +75,126 @@ ruleTester.run(RULE_NAME, rule, {
{
messageId: "noDuplicateKey",
data: {
value: '"1"',
value: 'key="1"',
},
},
],
},
{
code: /* tsx */ `
const App = () => {
return [1, 2, 3].map((item) => { return <div key="1">{item}</div> })
};
`,
errors: [
{
messageId: "noDuplicateKey",
data: {
value: 'key="1"',
},
},
],
},
{
code: /* tsx */ `
const App = () => {
return nested.map((item) => {
return <div key="1">{item.map((i) => <div key="a">{i}</div>)}</div>
})
};
`,
errors: [
{
messageId: "noDuplicateKey",
data: {
value: 'key="1"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: 'key="a"',
},
},
],
},
{
code: /* tsx */ `
const App = () => {
return nested.map((foo) => {
return <div key="foo">{foo.map((bar) => <div key="bar">{bar.map((baz) => <div key="baz">{baz}</div>)}</div>)}</div>
})
};
`,
errors: [
{
messageId: "noDuplicateKey",
data: {
value: 'key="foo"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: 'key="bar"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: 'key="baz"',
},
},
],
},
{
code: /* tsx */ `
const App = () => {
return nested?.map((foo) => {
return <div key="foo">{foo!.map((bar) => <div key="bar">{bar!!.map(((baz) => <div key="baz">{baz}</div>)!!! as A satisfies B)}</div>)}</div>
})
};
`,
errors: [
{
messageId: "noDuplicateKey",
data: {
value: 'key="foo"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: 'key="bar"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: 'key="baz"',
},
},
],
},
{
code: /* tsx */ `
const App = () => {
return nested.map((foo) => {
return <div key="foo">{foo.notmap((bar) => <div key="bar">{bar.map((baz) => <div key="baz">{baz}</div>)}</div>)}</div>
})
};
`,
errors: [
{
messageId: "noDuplicateKey",
data: {
value: 'key="foo"',
},
},
{
messageId: "noDuplicateKey",
data: {
value: 'key="baz"',
},
},
],
Expand All @@ -102,5 +221,12 @@ ruleTester.run(RULE_NAME, rule, {
return [1, 2, 3].map((item) => { const key = item; return <div key={key}>{item}</div> })
};
`,
/* tsx */ `
const App = () => {
return nested.map((item) => {
return <div key={item}>{item.map((i) => { return <div key={i}>{i}</div> })}</div>
})
};
`,
],
});
Loading

0 comments on commit 0edd720

Please sign in to comment.