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

[no-unused-vars] False positive when var is used in @jsxFrag #2514

Open
3 tasks done
dbartholomae opened this issue Sep 7, 2020 · 7 comments
Open
3 tasks done

[no-unused-vars] False positive when var is used in @jsxFrag #2514

dbartholomae opened this issue Sep 7, 2020 · 7 comments
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement New feature or request package: parser Issues related to @typescript-eslint/parser

Comments

@dbartholomae
Copy link

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  extends: [
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended"
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: "tsconfig.json",
    sourceType: "module",
    ecmaFeatures: {
      jsx: true,
    }
  },
  rules: {
    "react/prop-types": 0
  }
}
/* @jsx MD */
/* @jsxFrag Fragment */
import MD from "..";
import { Fragment } from ".";
<>Test</>;

Expected Result

This should lint just fine.

Actual Result

warning  'Fragment' is defined but never used  @typescript-eslint/no-unused-vars

Versions

package version
@typescript-eslint/eslint-plugin 4.1.1-alpha.5
@typescript-eslint/parser 4.1.1-alpha.5
TypeScript 4.0.2
ESLint 7.8.1
node 12.18.3
@dbartholomae dbartholomae added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 7, 2020
@dbartholomae
Copy link
Author

Not sure if this will be solved by #1856 but linking the issue as it might be related

@bradzacher bradzacher added enhancement New feature or request package: parser Issues related to @typescript-eslint/parser and removed package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 7, 2020
@bradzacher
Copy link
Member

We have parserOptions for both the pragma and the fragment name, both of which we are able to infer from the tsconfig if you're using type-aware parsing.

However this style of config will need to be built specifically - which is why support for inline config comments has not implemented.

Happy to accept a PR.

@dbartholomae
Copy link
Author

Thanks! I'll first need to finish the project where I'm using this, but happy to work on a PR for this next. I haven't yet contributed to typescript-eslint, so a small hint where to start would be greatly appreciated (only if it doesn't take you much time). It seems like the jsx pragma is already implemented? If you think, adding the jsxFrag pragma is a good first issue, I'm happy to take it, though a hint where to start would be greatly appreciated (I would assume somewhere in scope-manager?).
This might not be the most important ticket for this library at the moment, though, so if there is another issue where it would make more sense for me to help, I would be happy to do so :)

@bradzacher
Copy link
Member

The way it is set up is such that concerns are separated.

  • typescript-estree is solely concerned with coordinating with TS to produce an ESTree AST.
  • scope-manager is solely concerned with doing scope analysis using the provided options.
  • parser is concerned with coordinating the above packages in the context of an ESLint run.

So the feature would live in parser.
This is where we currently prepare the scope analysis options before calling the analyser. Right now this code just fills in the blanks if you're using type-aware parsing.

if (services.hasFullTypeInformation) {
// automatically apply the options configured for the program
const compilerOptions = services.program.getCompilerOptions();
if (analyzeOptions.lib == null) {
analyzeOptions.lib = getLib(compilerOptions);
log('Resolved libs from program: %o', analyzeOptions.lib);
}
if (parserOptions.jsx === true) {
if (
analyzeOptions.jsxPragma === undefined &&
compilerOptions.jsxFactory != null
) {
// in case the user has specified something like "preact.h"
const factory = compilerOptions.jsxFactory.split('.')[0].trim();
analyzeOptions.jsxPragma = factory;
log('Resolved jsxPragma from program: %s', analyzeOptions.jsxPragma);
}
if (
analyzeOptions.jsxFragmentName === undefined &&
compilerOptions.jsxFragmentFactory != null
) {
// in case the user has specified something like "preact.Fragment"
const fragFactory = compilerOptions.jsxFragmentFactory
.split('.')[0]
.trim();
analyzeOptions.jsxFragmentName = fragFactory;
log(
'Resolved jsxFragmentName from program: %s',
analyzeOptions.jsxFragmentName,
);
}
}
}
const scopeManager = analyze(ast, analyzeOptions);

The feature could be to iterate the comments in the AST, and then overwrite the the option that we find config comments for. This would match TS's behaviour, as you can configure jsxFactory in your tsconfig and overwrite it per-file with a config comment.

@dbartholomae
Copy link
Author

Thanks! Unfortunately I did not get to this issue over the last weeks and due to changes in priorities won't be able to take it in the near future :(
Feel free to either close or leave open, depending on your policy for this repo :)

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@daryazata
Copy link

@dbartholomae
I'm facing the same issue still, used vars are claimed as unused quite randomly, see an example below:
Bildschirmfoto 2022-01-07 um 14 27 12

@bradzacher
Copy link
Member

@daryazata please file a separate issue. That is entirely unrelated to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement New feature or request package: parser Issues related to @typescript-eslint/parser
Projects
None yet
Development

No branches or pull requests

4 participants