Skip to content

Commit

Permalink
Expand test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
merrywhether committed Oct 16, 2023
1 parent 56cfcca commit 52ff72c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 28 deletions.
10 changes: 6 additions & 4 deletions packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
Expand Up @@ -55,12 +55,14 @@ function isFunctionArgument(
}

/**
* Checks if a node is an expression in a JSX attribute assignment
* Checks if a node is type-constrained in JSX
* ```
* <Comp x={...} />
* <Foo x={() => {}} />
* <Bar>{() => {}}</Bar>
* <Baz {...props} />
* ```
*/
function isJSXAttribute(
function isTypedJSX(
node: TSESTree.Node,
): node is TSESTree.JSXExpressionContainer | TSESTree.JSXSpreadAttribute {
return (
Expand All @@ -78,7 +80,7 @@ function isTypedParent(
isVariableDeclaratorWithTypeAnnotation(parent) ||
isPropertyDefinitionWithTypeAnnotation(parent) ||
isFunctionArgument(parent, callee) ||
isJSXAttribute(parent)
isTypedJSX(parent)
);
}

Expand Down
Expand Up @@ -182,11 +182,7 @@ class App {
},
// https://github.com/typescript-eslint/typescript-eslint/issues/7552
{
code: `
const Comp: FC = () => {
return <button onClick={() => {}} />;
};
`,
code: 'const foo = <button onClick={() => {}} />;',
options: [{ allowTypedFunctionExpressions: true }],
parserOptions: {
ecmaFeatures: {
Expand All @@ -195,11 +191,7 @@ const Comp: FC = () => {
},
},
{
code: `
const Comp: FC = () => {
return <Button on={{ click: () => {} }} />;
};
`,
code: 'const foo = <button on={{ click: () => {} }} />;',
options: [{ allowTypedFunctionExpressions: true }],
parserOptions: {
ecmaFeatures: {
Expand All @@ -208,18 +200,33 @@ const Comp: FC = () => {
},
},
{
code: `
const Comp: FC = () => {
return <button {...{ onClick: () => {} }} />;
};
`,
code: 'const foo = <Bar>{() => {}}</Bar>;',
options: [{ allowTypedFunctionExpressions: true }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: 'const foo = <Bar>{{ on: () => {} }}</Bar>;',
options: [{ allowTypedFunctionExpressions: true }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: 'const foo = <button {...{ onClick: () => {} }} />;',
options: [{ allowTypedFunctionExpressions: true }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},

// https://github.com/typescript-eslint/typescript-eslint/issues/525
{
code: `
Expand Down Expand Up @@ -1018,11 +1025,7 @@ const x: Foo = {
],
},
{
code: `
const Comp = (): JSX.Element => {
return <button onClick={() => {}} />;
};
`,
code: 'const foo = <button onClick={() => {}} />;',
options: [{ allowTypedFunctionExpressions: false }],
parserOptions: {
ecmaFeatures: {
Expand All @@ -1032,10 +1035,82 @@ const Comp = (): JSX.Element => {
errors: [
{
messageId: 'missingReturnType',
line: 3,
endLine: 3,
column: 30,
endColumn: 32,
line: 1,
endLine: 1,
column: 33,
endColumn: 35,
},
],
},
{
code: 'const foo = <button on={{ click: () => {} }} />;',
options: [{ allowTypedFunctionExpressions: false }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 27,
endColumn: 34,
},
],
},
{
code: 'const foo = <Bar>{() => {}}</Bar>;',
options: [{ allowTypedFunctionExpressions: false }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 22,
endColumn: 24,
},
],
},
{
code: 'const foo = <Bar>{{ on: () => {} }}</Bar>;',
options: [{ allowTypedFunctionExpressions: false }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 21,
endColumn: 25,
},
],
},
{
code: 'const foo = <button {...{ onClick: () => {} }} />;',
options: [{ allowTypedFunctionExpressions: false }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
errors: [
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 27,
endColumn: 36,
},
],
},
Expand Down

0 comments on commit 52ff72c

Please sign in to comment.