rome / tools

Unified developer tools for JavaScript, TypeScript, and the web
https://docs.rome.tools/
MIT License
23.75k stars 659 forks source link

🐛 Linter rule `noUnusedVariables` doesn't like `as const` sometimes #4140

Closed ralexmatthews closed 1 year ago

ralexmatthews commented 1 year ago

Environment information

I added it to the comment as a txt file since my "comment was too long"

What happened?

So I had an existing project and had been using Rome as its full time formatter and linter, but then I turned on noUndeclaredVariables and noUnusedVariables

{
  "$schema": "./node_modules/rome/configuration_schema.json",
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true,
      "correctness": {
        "noUnusedVariables": "error",
        "noUndeclaredVariables": "error"
      }
    }
  }
}

and now running yarn rome check . and the vscode extension both give me errors of

The const variable is undeclared

everywhere I try to do something like

import * as React from "react";
import { useSpring } from "@react-spring/native";
import { sleep } from "utils/async";

const useSpringSwitch = (on: boolean) => {
    const { opacity } = useSpring({ opacity: on ? 1 : 0 });
    const [opacityState, setOpacityState] = React.useState(0);

    React.useEffect(() => {
        const tick = async () => {
            const newValue = opacity.get();
            setOpacityState(newValue);
            if ((on && newValue < 1) || (!on && newValue > 0)) {
                await sleep(50);
                tick();
            }
        };

        tick();
    }, [on]);

    return [opacityState, opacity] as const; // <- this right here is where I get the error
};

export default useSpringSwitch;
Screenshot 2023-01-04 at 8 29 21 PM

But I can upload this code to the playground and it works as expected, so it seems like this is just a bug somewhere in my setup. I have tried removing and reinstalling my node_modules, and I am on version 11.0.0, which is just installed locally to my project.

rome_rage.txt

Expected result

const should be a valid global typescript type.

Code of Conduct

ematipico commented 1 year ago

I had a look at your rage output, and it seems you're still running an old version of the VSCode extension.

I suggest to update it, close VSCode, kill the LSP server (via rome stop or using the process manager) and re open it.

If you still see the issue and you can't reproduce it in the playground (I couldn't), it means the issue is fixed on main.

ralexmatthews commented 1 year ago

Hmmm... I checked for updates and it said there were none. It was on v0.20.0. I then tried uninstalling and reinstalling, restarting the rome server, and still same thing.

I then tried switching to the prerelease version, v0.21.20221217, and restarting the lsp and still same thing.

What would be the current version?

flother commented 1 year ago

I've just ran into this too. Here's a minimal example that's reproducible on the command-line:

$ mkdir new-project
$ cd new-project
$ npm install rome
$ npx rome --version
Rome CLI version 11.0.0
$ cat > rome.json <<EOT
{
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true,
      "correctness": {
        "noUndeclaredVariables": "warn"
      }
    }
  }
}
EOT
$ echo "const a = [1, 2, 3] as const;" > index.ts
$ npx rome check index.ts
index.ts:1:24 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  ⚠ The const variable is undeclared

  > 1 │ const a = [1, 2, 3] as const;
      │                        ^^^^^
    2 │

Checked 1 file(s) in 3ms
github-actions[bot] commented 1 year ago

👋 @rome/staff please triage this issue by adding one of the following labels: S-Bug: confirmed, S-Planned , S-Wishlist or umbrella

Conaclos commented 1 year ago

This should be fixed by https://github.com/rome/tools/pull/3981

The fix will be in the incoming release.