Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-unsafe-argument] add rule#3256

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 1 commit intomasterfromno-unsafe-argument
Apr 2, 2021

Conversation

bradzacher
Copy link
Member

Fixes#791

This adds a new rule which just checks the passing of arguments.
This case is a lot more complex than most cases due to spread arguments.

danielnixon and joealden reacted with rocket emoji
Fixes#791This adds a new rule which just checks the passing of arguments.This case is a lot more complex than most cases due to spread arguments.
@bradzacherbradzacher added the enhancement: new plugin ruleNew rule request for eslint-plugin labelApr 1, 2021
@typescript-eslint
Copy link
Contributor

Thanks for the PR,@bradzacher!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day.

@codecov
Copy link

codecovbot commentedApr 1, 2021

Codecov Report

Merging#3256 (a086fa1) intomaster (62dfcc6) willdecrease coverage by0.07%.
The diff coverage is81.15%.

@@            Coverage Diff             @@##           master    #3256      +/-   ##==========================================- Coverage   92.91%   92.84%   -0.08%==========================================  Files         316      317       +1       Lines       10854    10923      +69       Branches     3069     3086      +17     ==========================================+ Hits        10085    10141      +56- Misses        342      348       +6- Partials      427      434       +7
FlagCoverage Δ
unittest92.84% <81.15%> (-0.08%)⬇️

Flags with carried forward coverage won't be shown.Click here to find out more.

Impacted FilesCoverage Δ
packages/eslint-plugin/src/configs/all.ts100.00% <ø> (ø)
...ages/eslint-plugin/src/rules/no-unsafe-argument.ts81.15% <81.15%> (ø)

@bradzacherbradzacher merged commitb1aa7dc intomasterApr 2, 2021
@bradzacherbradzacher deleted the no-unsafe-argument branchApril 2, 2021 20:19
@G-Rath
Copy link
Contributor

G-Rath commentedApr 5, 2021
edited
Loading

@bradzacher do you think this could be another one worth extending ineslint-plugin-jest, to have it ignore theany ofexpect async matchers?

i.e

expect(mockMessageReactor.prototype.react).toHaveBeenCalledWith(  message,  expect.arrayContaining<ReactionAction>([    ['heavy_multiplication_x', true]  ]));

I know I can satisfy this by casting tounknown (or stronger), but in this context such a cast is typically very meaningless.

In saying that, I am casting for properties due tono-unsafe-assignment:

expect(mockSlackReactionsApi.remove).toHaveBeenCalledWith({  name: expect.any(String) as string,  timestamp: '1',  channel: '#my-channel'});

Curious on if you have any opinions on if this is something extending :)

@bradzacher
Copy link
MemberAuthor

this rule checks arguments using outisUnsafeAssignment function.

exportfunctionisUnsafeAssignment(
type:ts.Type,
receiver:ts.Type,
checker:ts.TypeChecker,
):false|{sender:ts.Type;receiver:ts.Type}{
if(isTypeAnyType(type)){
// Allow assignment of any ==> unknown.
if(isTypeUnknownType(receiver)){
returnfalse;
}
if(!isTypeAnyType(receiver)){
return{sender:type, receiver};
}
}
if(isTypeReference(type)&&isTypeReference(receiver)){
// TODO - figure out how to handle cases like this,
// where the types are assignable, but not the same type
/*
function foo(): ReadonlySet<number> { return new Set<any>(); }
// and
type Test<T> = { prop: T }
type Test2 = { prop: string }
declare const a: Test<any>;
const b: Test2 = a;
*/
if(type.target!==receiver.target){
// if the type references are different, assume safe, as we won't know how to compare the two types
// the generic positions might not be equivalent for both types
returnfalse;
}
consttypeArguments=type.typeArguments??[];
constreceiverTypeArguments=receiver.typeArguments??[];
for(leti=0;i<typeArguments.length;i+=1){
constarg=typeArguments[i];
constreceiverArg=receiverTypeArguments[i];
constunsafe=isUnsafeAssignment(arg,receiverArg,checker);
if(unsafe){
return{sender:type, receiver};
}
}
returnfalse;
}
returnfalse;
}

Which will allow assignments ofany to eitherany orunknown.
So as long as the jest expect functions are typed as receivingany orunknown, then it should just work.

@bradzacher
Copy link
MemberAuthor

But it looks like there is a bug in the rule.

The jest function is typed as:

declarefunctiontoHaveBeenCalledWith<Eextendsany[]>(...params:E):void;

So at typecheck type, when called withtoHaveBeenCalledWith(1 as any), the type ofparams isn'tany[], it's[any].. which isn't handled right now

@G-Rath
Copy link
Contributor

So as long as the jest expect functions are typed as receiving any or unknown, then it should just work.

which (if I remember and understand everything correctly) they're not - they're typed to receiveany[] (where appropriate).

So:

expect(captureExceptionSpy).toHaveBeenCalledWith(expect.any(Error));

Gets

ESLint: Unsafe argument of typeany assigned to a parameter of type[any].(@typescript-eslint/no-unsafe-argument)

(that could be what you mean by[any] not being handled right now).

@fabb
Copy link

shouldn't the new rule be added to therecommended-requiring-type-checking preset?

@bradzacher
Copy link
MemberAuthor

bradzacher commentedApr 20, 2021
edited
Loading

It will, but changes to the recommended sets is a breaking change.

@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsMay 21, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers
No reviews
Assignees
No one assigned
Labels
enhancement: new plugin ruleNew rule request for eslint-plugin
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

New set of rules:no-unsafe-*
3 participants
@bradzacher@G-Rath@fabb

[8]ページ先頭

©2009-2025 Movatter.jp