Skip to content

test(eslint-plugin): [no-unnecessary-type-assertion] add tests with noUncheckedIndexedAccess #8581

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

Merged
merged 5 commits into from
Mar 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import rule from '../../src/rules/no-unnecessary-type-assertion';
const rootDir = path.resolve(__dirname, '../fixtures/');
const ruleTester = new RuleTester({
parserOptions: {
EXPERIMENTAL_useProjectService: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, CI / Run Unit Tests with Experimental TSServer (eslint-plugin) (pull_request) failed

sourceType: 'module',
tsconfigRootDir: rootDir,
project: './tsconfig.json',
},
parser: '@typescript-eslint/parser',
});

const optionsWithOnUncheckedIndexedAccess = {
tsconfigRootDir: rootDir,
project: './tsconfig.noUncheckedIndexedAccess.json',
};

ruleTester.run('no-unnecessary-type-assertion', rule, {
valid: [
`
Expand Down Expand Up @@ -242,6 +248,17 @@ const item = arr[0] as object;
declare const arr: (object | undefined)[];
const item = <object>arr[0];
`,
{
code: `
function foo(item: string) {}
function bar(items: string[]) {
for (let i = 0; i < items.length; i++) {
foo(items[i]!);
}
}
`,
parserOptions: optionsWithOnUncheckedIndexedAccess,
},
],

invalid: [
Expand Down Expand Up @@ -815,5 +832,31 @@ const foo = (3 + 5);
},
],
},
// onUncheckedIndexedAccess = false
{
code: `
function foo(item: string) {}
function bar(items: string[]) {
for (let i = 0; i < items.length; i++) {
foo(items[i]!);
}
}
`,
output: `
function foo(item: string) {}
function bar(items: string[]) {
for (let i = 0; i < items.length; i++) {
foo(items[i]);
}
}
`,
errors: [
{
messageId: 'unnecessaryAssertion',
line: 5,
column: 9,
},
],
},
],
});