-
-
Notifications
You must be signed in to change notification settings - Fork 48
svelte/@typescript-eslint/no-unnecessary-condition possible false positive #476
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
Comments
Shouldn't you write the following to make the <script lang="ts">
export let href: string | undefined = undefined;
$: url = href ?? 'http://example.com';
</script> I think that linting will work well if you write it as above. |
It doesn't always have to be reactive, it's just a parameter coming from someone that created the component, it's not going to change unless someone changes the url from the outside, which in that case the component will be redrawn. Unless I'm wildly misunderstanding Svelte. |
Check those two REPLs. Your code is fixed with initial values. https://svelte.dev/repl/9eead0107cbd4f32b626a634fe8af2dd?version=3.59.1 https://svelte.dev/repl/d530530e12394d59b52623b5b4366266?version=3.59.1 |
Sure, again you're talking about reactivity, but why would it give me a warning telling me a value is always "null" or always "false" when I can do this. It's just not correct since it could be initialized with other values. https://svelte.dev/repl/26022347d03b472190c9581b0640d901?version=3.59.1 |
Please consider opening a pull request if you'd like to fix it. |
Thanks for looking into it, I'll see if I have time for a PR later on. For anyone reading this, the workaround works and is fine. |
I think instead of modifying this rule, it's better to create like <script>
export let href: string | undefined = undefined;
const url = href ?? 'http://example.com';
// ^ Use reactive statement `$` instead of const / let / var.
</script> |
I like that new rule you proposed. However, I think @olafurw intentionally does not use reactive variables. |
I think this makes a chance to introduce a bug, so in terms of best practice, it's better to use And to handle this issue, again we need to implement |
In my opinion, to fix the problem we need to fix the parser. I believe that removing the initial expression when parsing with typescript and reverting the AST will give we the correct one. |
I'm not sure about concrete cases but I'm afraid that the parser removing a default value will make a different issue. |
Yeah. I think we need a lot of tests. For example, we might want to process only exports with type annotations, as type inference doesn't work. |
I think i might have a similar problem, but it is not fixed by reactivity. <script lang="ts">
import LoadingComponent from "$lib/components/LoadingComponent.svelte";
export let type: "button" | "submit" = "submit";
export let loading = false;
export let variant: "primary" | "secondary" | "error" = "primary";
export let form: string | undefined = undefined;
export let href = "";
$: className =
"w-full h-10 flex flex-row gap-3 justify-center items-center rounded px-4 shadow-md font-normal text-base text-white transition";
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (variant === "primary") {
className += " bg-secondary-700 hover:bg-secondary-800";
} else if (variant === "secondary") {
className += " bg-primary-800 hover:bg-primary-900";
} else if (variant === "error") {
className += " bg-error-600";
}
</script> Even when i made the className a reactiv, it still see variant AS "primary" and ONLY "primary" :D |
I think you should write: $: className =
"w-full h-10 flex flex-row gap-3 justify-center items-center rounded px-4 shadow-md font-normal text-base text-white transition"
+ (
variant === "primary"
? " bg-secondary-700 hover:bg-secondary-800"
: variant === "secondary"
? " bg-primary-800 hover:bg-primary-900"
: " bg-error-600"
); Also, I think overwriting the |
@ota-meshi but there problem here is not className at all, check out this example
|
Yeah. It has already been reported this issue. |
consider this: <script lang="ts">
export let externalProp: {id: string} | undefined = undefined
let internalState = externalProp; // let's make a copy to work with inside the component
onMount(() => {
if (internalState) doSomething() // according to eslint, this check is unnecessary.
// However it is not since the component could be mounted
// with an undefined externalProp.
})
</script> <script lang="ts">
type T = {id: string}
export let externalProp: T | undefined = undefined
let internalState: T | undefined = externalProp; // let's make a copy to work with inside the component
// and explicitly type it
onMount(() => {
if (internalState) doSomething() // this is now fine!
})
</script> |
Before You File a Bug Report Please Confirm You Have Done The Following...
What version of ESLint are you using?
8.39.0
What version of
eslint-plugin-svelte
are you using?2.28.0
What did you do?
https://typescript-eslint.io/play/#ts=5.0.4&sourceType=module&code=KYDwDg9gTgLgBAG2PAFlYAzAXHAzjKASwDsBzOAHzgFdiATTE4OuAXhvseOYG4AoPgGMIxfDSgI2cNJjgB+OXADkKGDDBYA9JtABDALZgkAOmH6l-IA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK+SpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA
I'm showing here in eslint playground but the same thing happens in a
.svelte
file, with bothsvelte/@typescript-eslint/no-unnecessary-condition
and@typescript-eslint/no-unnecessary-condition
It will tell me that the
href
condition check is unnecessary since it will always be null or undefined.But it's an
export
variable, so it might get some value from whoever is using the property, I've just defaulted it toundefined
if someone doesn't send it in.What did you expect to happen?
No warning/error
What actually happened?
Warning:
Unnecessary conditional, left-hand side of
??operator is always
nullor
undefined. 3:13 - 3:17
Link to GitHub Repo with Minimal Reproducible Example
https://typescript-eslint.io/play/#ts=5.0.4&sourceType=module&code=KYDwDg9gTgLgBAG2PAFlYAzAXHAzjKASwDsBzOAHzgFdiATTE4OuAXhvseOYG4AoPgGMIxfDSgI2cNJjgB+OXADkKGDDBYA9JtABDALZgkAOmH6l-IA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK+SpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA
Additional comments
No response
The text was updated successfully, but these errors were encountered: