Skip to content
Open
Show file tree
Hide file tree
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
78 changes: 78 additions & 0 deletions its/ruling/src/test/expected/ant-design/typescript-S4782.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
{
"ant-design:components/button/button-group.tsx": [
8
],
"ant-design:components/button/button.tsx": [
105
],
"ant-design:components/cascader/index.tsx": [
104
],
"ant-design:components/collapse/Collapse.tsx": [
32
],
"ant-design:components/config-provider/DisabledContext.tsx": [
8
],
"ant-design:components/config-provider/SizeContext.tsx": [
8
],
"ant-design:components/config-provider/context.tsx": [
41
],
"ant-design:components/config-provider/index.tsx": [
83,
85
],
"ant-design:components/date-picker/generatePicker/index.tsx": [
82
],
"ant-design:components/drawer/index.tsx": [
66
],
"ant-design:components/form/Form.tsx": [
31
],
"ant-design:components/input-number/index.tsx": [
25
],
"ant-design:components/input/ClearableLabeledInput.tsx": [
31,
40
],
"ant-design:components/input/Input.tsx": [
117
],
"ant-design:components/input/TextArea.tsx": [
51
],
"ant-design:components/menu/MenuContext.tsx": [
10
],
"ant-design:components/modal/Modal.tsx": [
124
],
"ant-design:components/progress/Line.tsx": [
9
],
"ant-design:components/radio/interface.tsx": [
14,
15
],
"ant-design:components/segmented/index.tsx": [
41
],
"ant-design:components/select/index.tsx": [
40
],
"ant-design:components/table/Table.tsx": [
88
],
"ant-design:components/tabs/index.tsx": [
22
],
"ant-design:components/transfer/operation.tsx": [
17
],
"ant-design:components/tree-select/index.tsx": [
50
],
"ant-design:components/typography/Editable.tsx": [
19
]
}
3 changes: 3 additions & 0 deletions its/ruling/src/test/expected/eigen/typescript-S4782.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"eigen:src/app/Scenes/Artwork/Components/ImageCarousel/ImageCarousel.tsx": [
34
],
"eigen:src/app/Scenes/Fair/Components/FairArtworks.tsx": [
25
],
Expand Down
6 changes: 6 additions & 0 deletions its/ruling/src/test/expected/vitest/typescript-S4782.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
78,
384
],
"vitest:packages/pretty-format/src/types.ts": [
108
],
"vitest:packages/runner/src/fixture.ts": [
633
],
Expand All @@ -22,6 +25,9 @@
238,
1266
],
"vitest:packages/utils/src/diff/types.ts": [
31
],
"vitest:packages/vitest/src/node/cli/filter.ts": [
31
],
Expand Down
10 changes: 10 additions & 0 deletions its/ruling/src/test/expected/vuetify/typescript-S4782.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{
"vuetify:packages/vuetify/src/composables/color.ts": [
21,
21
],
"vuetify:packages/vuetify/src/composables/group.ts": [
21,
22
],
"vuetify:packages/vuetify/src/composables/rounded.ts": [
11
],
"vuetify:packages/vuetify/src/composables/router.tsx": [
42,
43
],
"vuetify:packages/vuetify/src/framework.ts": [
27
]
}
38 changes: 33 additions & 5 deletions packages/analysis/src/jsts/rules/S4782/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import type { AST, Rule } from 'eslint';
import type estree from 'estree';
import type { TSESTree } from '@typescript-eslint/utils';
import { generateMeta } from '../helpers/generate-meta.js';
import { isRequiredParserServices } from '../helpers/parser-services.js';
import { isRequiredParserServices, RequiredParserServices } from '../helpers/parser-services.js';
import { report, toSecondaryLocation } from '../helpers/location.js';
import * as meta from './generated-meta.js';
import ts from 'typescript';

export const rule: Rule.RuleModule = {
meta: generateMeta(meta, { hasSuggestions: true }),
Expand All @@ -45,8 +46,10 @@ export const rule: Rule.RuleModule = {
if (!tsNode.optional || !optionalToken) {
return;
}

const typeNode = getUndefinedTypeAnnotation(tsNode.typeAnnotation);
const typeNode = getUndefinedTypeAnnotation(
tsNode.typeAnnotation,
context.sourceCode.parserServices,
);
if (typeNode) {
const suggest = getQuickFixSuggestions(context, optionalToken, typeNode);

Expand All @@ -69,13 +72,38 @@ export const rule: Rule.RuleModule = {
},
};

function getUndefinedTypeAnnotation(tsTypeAnnotation?: TSESTree.TSTypeAnnotation) {
if (tsTypeAnnotation?.typeAnnotation.type === 'TSUnionType') {
function getUndefinedTypeAnnotation(
tsTypeAnnotation: TSESTree.TSTypeAnnotation | undefined,
services: RequiredParserServices,
) {
if (!tsTypeAnnotation) {
return undefined;
}

if (tsTypeAnnotation.typeAnnotation.type === 'TSTypeReference') {
return getUndefinedFromTypeAlias(tsTypeAnnotation, services);
}
if (tsTypeAnnotation.typeAnnotation.type === 'TSUnionType') {
return getUndefinedTypeNode(tsTypeAnnotation.typeAnnotation);
}
return undefined;
}

function getUndefinedFromTypeAlias(
tsTypeAnnotation: TSESTree.TSTypeAnnotation,
services: RequiredParserServices,
): TSESTree.TypeNode | undefined {
const tsTypeNode = services.esTreeNodeToTSNodeMap.get(tsTypeAnnotation.typeAnnotation);
const checker: ts.TypeChecker = services.program.getTypeChecker();
const type = checker.getTypeAtLocation(tsTypeNode);
if (!type.isUnion()) {
return undefined;
}
const hasUndefined = type.types.some(t => (t.flags & ts.TypeFlags.Undefined) !== 0);
const hasNonUndefined = type.types.some(t => (t.flags & ts.TypeFlags.Undefined) === 0);
return hasUndefined && hasNonUndefined ? tsTypeAnnotation.typeAnnotation : undefined;
}

function getUndefinedTypeNode(typeNode: TSESTree.TypeNode): TSESTree.TypeNode | undefined {
if (typeNode.type === 'TSUndefinedKeyword') {
return typeNode;
Expand Down
165 changes: 165 additions & 0 deletions packages/analysis/src/jsts/rules/S4782/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,56 @@ describe('S4782', () => {
specificAttribute?: undefined;
};`,
},
{
code: `
type StringOrNumber = string | number;
interface Example {
attribute?: StringOrNumber;
};
type UndefinedAlias = undefined;
interface Example2 {
attribute?: UndefinedAlias;
};
`,
},
{
code: `
type A = string;
type B = A;
type C = B;
interface Example {
attribute?: C;
};
type D = undefined;
type E = D;
type F = E;
interface Example2 {
attribute?: F;
};
`,
},
{
code: `
type Recursive = string | Recursive;
interface Example {
attribute?: Recursive;
};
type RecursiveUndefined = undefined | Recursive;
interface Example2 {
attribute?: RecursiveUndefined;
};`,
},
{
code: `
type Box<T> = T;
interface Example {
attribute?: Box<string>;
};
type BoxU<T> = T;
interface Example2 {
attribute?: BoxU<undefined>;
};`,
},
],
invalid: [
{
Expand Down Expand Up @@ -286,6 +336,121 @@ describe('S4782', () => {
},
],
},
{
code: `type StringOrUndefined = string | undefined;
interface Example {
attribute?: StringOrUndefined;
};`,
errors: [
{
message:
"Consider removing 'undefined' type or '?' specifier, one of them is redundant.",
suggestions: [
{
desc: 'Remove "?" operator',
output: `type StringOrUndefined = string | undefined;
interface Example {
attribute: StringOrUndefined;
};`,
},
],
},
],
},
{
code: `
type NumberOrUndefined = number | undefined;
type Attribute = string | NumberOrUndefined;
interface Example {
attribute?: Attribute;
};`,
errors: [
{
message:
"Consider removing 'undefined' type or '?' specifier, one of them is redundant.",
suggestions: [
{
desc: 'Remove "?" operator',
output: `
type NumberOrUndefined = number | undefined;
type Attribute = string | NumberOrUndefined;
interface Example {
attribute: Attribute;
};`,
},
],
},
],
},
{
code: `
type Maybe<T> = T | undefined;
interface Example {
attribute?: Maybe<string>;
};`,
errors: [
{
message:
"Consider removing 'undefined' type or '?' specifier, one of them is redundant.",
suggestions: [
{
desc: 'Remove "?" operator',
output: `
type Maybe<T> = T | undefined;
interface Example {
attribute: Maybe<string>;
};`,
},
],
},
],
},
{
code: `
type Box<T> = T;
interface Example {
attribute?: Box<string | undefined>;
};`,
errors: [
{
message:
"Consider removing 'undefined' type or '?' specifier, one of them is redundant.",
suggestions: [
{
desc: 'Remove "?" operator',
output: `
type Box<T> = T;
interface Example {
attribute: Box<string | undefined>;
};`,
},
],
},
],
},
{
code: `
type Wrapped = (string | undefined);
interface Example {
attribute?: Wrapped;
};`,
errors: [
{
message:
"Consider removing 'undefined' type or '?' specifier, one of them is redundant.",
suggestions: [
{
desc: 'Remove "?" operator',
output: `
type Wrapped = (string | undefined);
interface Example {
attribute: Wrapped;
};`,
},
],
},
],
},
],
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"allowJs": true,
"noImplicitAny": true,
"strict": false,
"strictNullChecks": false,
"strictNullChecks": true,
"lib": ["ESNext", "DOM"],
},
"include": [
Expand Down
Loading