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

Improve S1874 (deprecation): Report deprecations from TypeScript compiler #4122

Merged
merged 1 commit into from
Aug 31, 2023

Conversation

yassin-kammoun-sonarsource
Copy link
Contributor

Fixes #4008

Building from #4064, it turns out quite tricky to cover every single case of deprecated symbols that are used. In fact, after looking at TypeScript's codebase, there are many places where usages of deprecated APIs are detected and reported. This is due to several edge cases. Instead of replicating the logic for every single one, we propose to extract and propagate deprecation suggestions emitted by TypeScript's type checker. There are minor functional differences compared to our current implementation, like the fact that TypeScript reports on import statements. However, with these changes, we behave precisely like TypeScript compilers, thus reducing false positives and negatives.

@sonarqube-next
Copy link


import defaultImport, {deprecatedFunction, anotherDeprecatedFunction as aliasForDeprecated, notDeprecated1, notDeprecated2} from './cb.fixture.deprecations';
import defaultImport, {deprecatedFunction, anotherDeprecatedFunction as aliasForDeprecated, notDeprecated1, notDeprecated2} from './cb.fixture.deprecations'; // Noncompliant 2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeScript reports on imported deprecated symbols, which we didn't previously.

@@ -81,7 +81,7 @@ let deprecatedVar;
const const1 = 1,
const2 = 2;

h(const1 + const2); // Noncompliant
h(const1 + const2); // Noncompliant 2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We had a false negative before on const2.

@@ -93,7 +93,7 @@ function fn() { }

fn<number>();
fn(1); // Noncompliant
h(fn); // Noncompliant
h(fn);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeScript doesn't report deprecated symbols used as references, like here with fn.

Comment on lines +164 to +186
/* i4008 */

/** @deprecated */ function someDeprecated(a: string): string;
/** @param a */ function someDeprecated(a: number): number;
function someDeprecated(a: number | string): number | string {
if (typeof a === 'number') return a + 1;
return a;
}

someDeprecated('yolo'); // Noncompliant
someDeprecated(42); // OK
someDeprecated; // OK

/** @deprecated */ function allDeprecated(a: string): string;
/** @deprecated */ function allDeprecated(a: number): number;
function allDeprecated(a: number | string): number | string {
if (typeof a === 'number') return a + 1;
return a;
}

allDeprecated('yolo'); // Noncompliant
allDeprecated(42); // Noncompliant
allDeprecated; // OK
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Taken from #4064

Copy link
Contributor

@victor-diez-sonarsource victor-diez-sonarsource left a comment

Choose a reason for hiding this comment

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

Really good job!

@yassin-kammoun-sonarsource yassin-kammoun-sonarsource merged commit 1959418 into master Aug 31, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix FP S1874: (deprecation): Should not report when at least one function signature is not deprecated
2 participants