Skip to content

Commit

Permalink
JS-67 Fix FP S4782 (no-redundant-optional): Ignore when "exactOptio…
Browse files Browse the repository at this point in the history
…nalPropertyTypes" is enabled (#4829)
  • Loading branch information
yassin-kammoun-sonarsource committed Sep 18, 2024
1 parent a0bd0e4 commit bec1017
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/jsts/src/rules/S4782/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// intentionally left empty
6 changes: 6 additions & 0 deletions packages/jsts/src/rules/S4782/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strictNullChecks": true,
"exactOptionalPropertyTypes": true
}
}
5 changes: 5 additions & 0 deletions packages/jsts/src/rules/S4782/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export const rule: Rule.RuleModule = {
return {};
}

const compilerOptions = context.sourceCode.parserServices.program.getCompilerOptions();
if (compilerOptions.exactOptionalPropertyTypes) {
return {};
}

function checkProperty(node: estree.Node) {
const tsNode = node as TSESTree.Node as
| TSESTree.PropertyDefinition
Expand Down
22 changes: 22 additions & 0 deletions packages/jsts/src/rules/S4782/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
import { TypeScriptRuleTester } from '../../../tests/tools';
import { rule } from './';
import { RuleTester } from 'eslint';
import path from 'path';

const ruleTester = new TypeScriptRuleTester();
ruleTester.run(
Expand Down Expand Up @@ -222,3 +224,23 @@ ruleTester.run(
],
},
);

const noopRuleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
project: `tsconfig.json`,
tsconfigRootDir: path.join(__dirname, 'fixtures'),
},
});

noopRuleTester.run('S4782 becomes noop when exactOptionalPropertyTypes is enabled', rule, {
valid: [
{
code: 'interface T { p?: string | undefined; }',
filename: path.join(__dirname, 'fixtures', 'index.ts'),
},
],
invalid: [],
});

0 comments on commit bec1017

Please sign in to comment.