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 FP S3800 (function-return-type): Exception for sanitation functions #4548

Merged
merged 1 commit into from
Feb 5, 2024
Merged
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
32 changes: 0 additions & 32 deletions its/ruling/src/test/expected/jsts/vuetify/javascript-S3800.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
{
"vuetify:packages/docs/src/examples/v-date-picker/event-events.vue": [
44
],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For information, I checked all the removed lines and can confirm that they are not triggering the rule anymore - they all belong to a sanitation function.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

"vuetify:packages/docs/src/examples/v-file-input/prop-validation.vue": [
15
],
"vuetify:packages/docs/src/examples/v-form/misc-vee-validate.vue": [
59,
64,
69,
74,
79
],
"vuetify:packages/docs/src/examples/v-form/prop-fast-fail.vue": [
26,
34
],
"vuetify:packages/docs/src/examples/v-form/rules-required.vue": [
19
],
"vuetify:packages/docs/src/examples/v-form/usage.vue": [
54,
59,
67,
72
],
"vuetify:packages/docs/src/examples/v-input/prop-rules.vue": [
11
],
"vuetify:packages/docs/src/examples/v-text-field/misc-guide.vue": [
61
],
"vuetify:packages/docs/src/examples/v-text-field/prop-validation.vue": [
42
]
}
13 changes: 13 additions & 0 deletions packages/jsts/src/rules/S3800/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import {
getParent,
getTypeFromTreeNode,
isAny,
isBooleanTrueType,
isRequiredParserServices,
isStringType,
RuleContext,
toEncodedMessage,
} from '../helpers';
import { SONAR_RUNTIME } from '../../linter/parameters';
import { type UnionType } from 'typescript';

class FunctionScope {
private readonly returnStatements: estree.ReturnStatement[] = [];
Expand All @@ -46,6 +49,12 @@ class FunctionScope {
}
}

const isASanitationFunction = (signature: ts.Signature) => {
const { types } = signature.getReturnType() as UnionType;

return types.length === 2 && types.some(isBooleanTrueType) && types.some(isStringType);
};

export const rule: Rule.RuleModule = {
meta: {
schema: [
Expand Down Expand Up @@ -74,6 +83,10 @@ export const rule: Rule.RuleModule = {
services.esTreeNodeToTSNodeMap.get(node as TSESTree.Node) as ts.SignatureDeclaration,
);
if (signature && hasMultipleReturnTypes(signature, checker)) {
if (isASanitationFunction(signature)) {
return;
}

const stmts = returnStatements.filter(
retStmt => !isNullLike(getTypeFromTreeNode(retStmt.argument!, services)),
);
Expand Down
38 changes: 31 additions & 7 deletions packages/jsts/src/rules/S3800/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,29 @@ ruleTesterTs.run(`Functions should always return the same type [ts]`, rule, {
}
}`,
},
{
code: `
function foo() {
return condition ? 'str' : true;
}`,
},
{
code: `
const sanitize = () => {
return condition ? true : 'Value should be a string';
};
`,
},
{
code: `
const sanitize = () => {
if (condition) {
return true;
};

return 'Value should be a string';
}`,
},
],
invalid: [
{
Expand Down Expand Up @@ -468,13 +491,6 @@ ruleTesterTs.run(`Functions should always return the same type [ts]`, rule, {
},
],
},
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was moved from the invalid list to the valid one.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

code: `
function foo() {
return condition ? 'str' : true;
}`,
errors: 1,
},
{
code: `
class C {
Expand Down Expand Up @@ -533,6 +549,14 @@ ruleTesterTs.run(`Functions should always return the same type [ts]`, rule, {
},
],
},
{
code: `
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this test to guarantee that only functions with true and string return types are valid.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

const sanitize = () => {
return condition ? true : 42;
};
`,
errors: 1,
},
],
});

Expand Down
10 changes: 10 additions & 0 deletions packages/jsts/src/rules/helpers/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,13 @@ export function isTypeAlias(node: TSESTree.TypeNode, context: Rule.RuleContext)
const variable = getVariableFromScope(scope, node.typeName.name);
return variable?.defs.some(def => def.node.type === 'TSTypeAliasDeclaration');
}

export function isBooleanLiteralType(type: ts.Type): type is ts.Type & {
intrinsicName: 'true' | 'false';
} {
return type.flags === ts.TypeFlags.BooleanLiteral;
}

export function isBooleanTrueType(type: ts.Type) {
return isBooleanLiteralType(type) && type.intrinsicName === 'true';
}
Loading