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-var-requires] False negative if require result is passed to a function #665

Closed
theneva opened this issue Jul 1, 2019 · 3 comments · Fixed by #725
Closed

[no-var-requires] False negative if require result is passed to a function #665

theneva opened this issue Jul 1, 2019 · 3 comments · Fixed by #725
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@theneva
Copy link

theneva commented Jul 1, 2019

Repro

The provided code can also be found in the minimal reproduction repo at https://github.com/theneva/no-var-requires-trick.

I use the @typescript-eslint/recommended plugin with no configuration (and disable @typescript-eslint/no-unused-vars to reduce noise for the repro):

{
  "extends": ["plugin:@typescript-eslint/recommended"],
  "rules": {
    "@typescript-eslint/no-unused-vars": "off"
  }
}

I can then trick the rule by passing the result of the require('fs') call into a function:

// your repro code case
function trick(a: unknown) {
  return a;
}

const fs = trick(require('fs'));

Expected Result

The rule should report this as an error, as I am using the returned value of a require(…) call.

Actual Result

The rule does not consider this to be an error.

Additional Info

When my TS code looks like this (with the same eslint config), the rule reports one assignment and ignores the other:

function trick(a: unknown) {
  return a;
}

const fs = trick(require('fs'));

const fs2 = require('fs');

The output looks as follows (reporting fs2 but not fs):

$ yarn run eslint .

/private/tmp/no-var-requires-trick/index.js
  5:11  error  Require statement not part of import statement  @typescript-eslint/no-var-requires

✖ 1 problem (1 error, 0 warnings)

Versions

package version
@typescript-eslint/eslint-plugin 1.11.0
@typescript-eslint/parser 1.11.0
TypeScript 3.5.2
ESLint 6.0.1
node 11.10.1
npm N/A (yarn version 1.17.0)
@theneva theneva added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jul 1, 2019
@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule good first issue Good for newcomers and removed triage Waiting for maintainers to take a look labels Jul 1, 2019
@bradzacher
Copy link
Member

I mean technically, this is working as intended, as you are not using var var require.

Happy to accept a PR for this.

@JayaKrishnaNamburu
Copy link
Contributor

JayaKrishnaNamburu commented Jul 19, 2019

Hi @bradzacher I would like to try this and raise PR.

JayaKrishnaNamburu added a commit to JayaKrishnaNamburu/typescript-eslint that referenced this issue Jul 19, 2019
Throwing error if the parent is CallExpression in no-var-requires plugin

Closes: typescript-eslint#665
@JayaKrishnaNamburu
Copy link
Contributor

Hi @bradzacher I give a PR, it fixes the current. It fixes the current issue, would like to know if there are any edge cases are any other preferred way to handle this.

JayaKrishnaNamburu added a commit to JayaKrishnaNamburu/typescript-eslint that referenced this issue Jul 24, 2019
Adding a missing test case after handling new edge case

Closes: typescript-eslint#665
@bradzacher bradzacher added the has pr there is a PR raised to close this label Jul 24, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants