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 false positives for uninitialized vars in vue/no-ref-as-operand rule #1988

Merged
merged 2 commits into from Oct 4, 2022

Conversation

ota-meshi
Copy link
Member

close #1980

@ota-meshi ota-meshi added the bug label Sep 22, 2022
@jacekkarczmarczyk
Copy link

jacekkarczmarczyk commented Sep 23, 2022

Not sure about the PR implementation, but I think the title is wrong - it's not only about "uninitialized" variables, it's about variables that are Ref | NotARef, for example

let foo: Ref<number> | number = 5;

foo = ref(3);

is also "fixed" to

const foo: Ref<number> | number = 5;

foo.value = ref(3);

even though variable is initialized

@ota-meshi
Copy link
Member Author

I am not familiar with English. Could you suggest a title?

The example you provided seems to work fine when I checked it on the demo site.

<script lang="ts">
import { ref } from 'vue'
let foo: Ref<number> | number = 5;

foo = ref(3);
</script>

https://deploy-preview-1988--eslint-plugin-vue.netlify.app/rules/no-ref-as-operand.html

@jacekkarczmarczyk
Copy link

I'm not native english neither, but I think that issue title describes the problem fine and might be used as a PR title too

@ota-meshi
Copy link
Member Author

The rule will report it if:

<script lang="ts">
import { ref } from 'vue'
let foo: Ref<number> | number = ref(3);

foo = 5;
</script>

(If you think this is a false positive, I recommend turning off the rule.)

So I think Ref | NotARef doesn't show the correct meaning.

@jacekkarczmarczyk
Copy link

I see your point. I'm not really sure if your code should report the error, however suggestion to turn the rule off seems to make sense as the problem would be reported by TS anyway, at least in this case (and that's actually how I've found out the problem, if not TS I might have missed it and end up with incorrect code).

On the other hand there are cases where TS wouldn't spot the error, e.g.

let t: Ref|number = ref(5);
function fn () {
  if (t > 3) t = 1;
}

I'm investigating this issue now

@ota-meshi ota-meshi merged commit 228b49f into master Oct 4, 2022
@ota-meshi ota-meshi deleted the issue1980 branch October 4, 2022 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vue/no-ref-as-operand should not autofix when variable is Ref | NotARef
2 participants