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

fix(eslint-plugin): [no-unnec-type-assert] handle JSX attributes #1002

Merged
merged 11 commits into from Jan 13, 2020
@@ -1,6 +1,7 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import {
isCallExpression,
isJsxExpression,
isNewExpression,
isObjectType,
isObjectFlagSet,
Expand Down Expand Up @@ -117,6 +118,8 @@ export default util.createRule<Options, MessageIds>({
return parent.type
? checker.getTypeFromTypeNode(parent.type)
: undefined;
} else if (isJsxExpression(parent)) {
return checker.getContextualType(parent);
} else if (
![ts.SyntaxKind.TemplateSpan, ts.SyntaxKind.JsxExpression].includes(
parent.kind,
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/tests/RuleTester.ts
Expand Up @@ -44,6 +44,7 @@ class RuleTester extends TSESLint.RuleTester {
): void {
const errorMessage = `Do not set the parser at the test level unless you want to use a parser other than ${parser}`;

// standardize the valid tests as objects
tests.valid = tests.valid.map(test => {
if (typeof test === 'string') {
return {
Expand Down
Empty file.
7 changes: 6 additions & 1 deletion packages/eslint-plugin/tests/fixtures/tsconfig.json
@@ -1,10 +1,15 @@
{
"compilerOptions": {
"jsx": "preserve",
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"lib": ["es2015", "es2017", "esnext"],
"experimentalDecorators": true
}
},
"include": [
"file.ts",
"react.tsx"
]
}
Expand Up @@ -117,6 +117,19 @@ function testFunction(_param: string | null): void { /* noop */ }
const value = 'test' as string | null | undefined
testFunction(value!)
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/982
{
code: `
declare namespace JSX { interface IntrinsicElements { div: { key?: string | number } } }

function Test(props: {
id?: null | string | number;
}) {
return <div key={props.id!} />;
}
`,
filename: path.join(rootDir, 'react.tsx'),
Copy link
Member

@armano2 armano2 Jan 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename for now has to be absolute when used with watch program (project)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to put this in the RuleTester?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it should not be there, it should work correctly with relative paths
see #1439

},
],

invalid: [
Expand Down Expand Up @@ -327,5 +340,33 @@ class Mx {
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/982
{
code: `
declare namespace JSX { interface IntrinsicElements { div: { key?: string | number } } }

function Test(props: {
id?: string | number;
}) {
return <div key={props.id!} />;
}
`,
output: `
declare namespace JSX { interface IntrinsicElements { div: { key?: string | number } } }

function Test(props: {
id?: string | number;
}) {
return <div key={props.id} />;
}
`,
errors: [
{
messageId: 'contextuallyUnnecessary',
line: 7,
},
],
filename: path.join(rootDir, 'react.tsx'),
},
],
});