BenoitZugmeyer / eslint-plugin-html

An ESLint plugin to extract and lint scripts from HTML files.
ISC License
430 stars 51 forks source link

no-undef rule triggered when function is defined in js file referenced by separate script tags src attribute #81

Closed BlacKCaT27 closed 6 years ago

BlacKCaT27 commented 6 years ago

In my application, I have the following in my html:

    <script type="text/javascript" src="js/myJs.js"></script>

    <script>
        $(document).ready(function () {
            MyFunction();
        });
    </script>

MyFunction() is being flagged with the no-undef rule, even though it's defined and executes properly from within myJs.js.

I'm also seeing "no-unused-vars" on that method in my js file, so perhaps this is just an issue in how either the html plugin or eslint itself is wired up. I'm assuming this case is normally handled correctly. Any thoughts on how to correct the problem on my end?

BenoitZugmeyer commented 6 years ago

Yes, neither eslint or this plugin are loading files declared as dependency. In your case, you should mark MyFunction as "global" in the script tag (as it is, in fact, a global), and "exported" in myJs.js.