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 triggered wrongly #61

Closed
jonathanGB opened this issue Jun 16, 2017 · 4 comments
Closed

no_unused_vars triggered wrongly #61

jonathanGB opened this issue Jun 16, 2017 · 4 comments

Comments

@jonathanGB
Copy link

Hi,

I have a file with the following structure, in which I get the following error:

'checkSubmit' is defined but never used

<form onsubmit="return checkSubmit(1)">
  ...
</form>

<script>
  function checkSubmit(num) {
    ...
  }
</script>

Clearly, checkSubmit is used as a listener. Maybe the html is ignored by the plugin, even though it may have an impact on rules like this one.

Thanks!

@BenoitZugmeyer
Copy link
Owner

Hi, thanks for the report. Sadly, I have no idea how linting JS in HTML attributes could work... I can't think of a way to feed it to ESLint. I'm open to suggestions.

@hsxfjames
Copy link

How about parse onXXX attributes and transform into a fake JS segment? Then hack the messages from ESLint.

@armano2
Copy link

armano2 commented Nov 15, 2018

@BenoitZugmeyer To parse attributes you have to use something like this

  const parser = new htmlparser.Parser(
    {
...
      onattribute(name, value) {
        const cdata = []
        if (/^javascript:/i.test(value)) {
          const end = parser.endIndex - 1 // idk if quote is included
          chunks.push({
            type: "inlineScript",
            start: end - value.length + 10, // not sure about those values
            end,
            cdata,
          })
        } else if (/^on/i.test(name)) {
          const end = parser.endIndex - 1 // idk if quote is included
          chunks.push({
            type: "inlineScript",
            start: end - value.length,
            end,
            cdata,
          })
        }
      },
...

after parsing html event attributes you will have to make rule thats triggers

context.markVariableAsUsed(name)

see https://eslint.org/docs/developer-guide/working-with-rules#the-context-object

@BenoitZugmeyer
Copy link
Owner

Superseded by #123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants