google/closure-compiler-npm

bug(grunt): “jscomp_off” option doesn’t work

Kristinita opened this issue · 1 comments

1. Summary

I don’t understand what should I do to jscomp_off option work for closure-compiler Grunt task.

I may have found a bug.

2. MCVE

You can see this configuration in the KiraClosureCompilerJscompOff branch branch of my repository for demo/debugging.

  1. KiraChocolat.js:

    document.addEventListener('DOMContentLoaded', function(event) {
      return Chocolat(document.querySelectorAll('.chocolat-parent .chocolat-image'));
    });
  2. Gruntfile.coffee:

    module.exports = (grunt) ->
    
        require('google-closure-compiler').grunt grunt,
            platform: [
                'native'
                'java'
                'javascript'
            ]
            max_parallel_compilations: require('os').cpus().length
    
        grunt.initConfig
    
            'closure-compiler':
                options:
                    jscomp_off: ["undefinedVars"]
                    jscomp_warning: "*"
                    language_out: "ECMASCRIPT_NEXT"
                    warning_level: "VERBOSE"
                kira_target:
                    files: [
                        src: 'KiraChocolat.js'
                        dest: 'KiraDest.js'
                    ]
    
  3. The part of .travis.yml:

    install:
    - npm i -g grunt-cli google-closure-compiler
    - npm i
    
    script:
    - grunt closure-compiler --verbose --stack
    - google-closure-compiler --js KiraChocolat.js --language_out ECMASCRIPT_NEXT --warning_level VERBOSE --jscomp_warning "*" --jscomp_off undefinedVars
    - google-closure-compiler --js KiraChocolat.js --language_out ECMASCRIPT_NEXT --warning_level VERBOSE --jscomp_warning "*"

3. Behavior

See Travis builds.

3.1. Expected — CLI

  1. If --jscomp_off undefinedVars:

    no warnings.

  2. Else no --jscomp_off option:

    $ google-closure-compiler --js KiraChocolat.js --language_out ECMASCRIPT_NEXT --warning_level VERBOSE --jscomp_warning "*"
    
    KiraChocolat.js:2:9: WARNING - [JSC_UNDEFINED_VARIABLE] variable Chocolat is undeclared
    
      2|   return Chocolat(document.querySelectorAll('.chocolat-parent .chocolat-image'));
    
                  ^^^^^^^^
    
    0 error(s), 1 warning(s), 83.3% typed

3.2. Unexpected — Grunt

  • Travis:

    Running "closure-compiler:kira_target" (closure-compiler) task
    
    Verifying property closure-compiler.kira_target exists in config…OK
    
    Files: KiraChocolat.js -> KiraDest.js
    
    Options: args=undefined, jscomp_off=["undefinedVars"], jscomp_warning="*", language_out="ECMASCRIPT_NEXT", warning_level="VERBOSE"
    
    >> grunt-google-closure-compiler: KiraChocolat.js:2:9: WARNING - [JSC_UNDEFINED_VARIABLE] variable Chocolat is undeclared
    
    >>   2|   return Chocolat(document.querySelectorAll('.chocolat-parent .chocolat-image'));
    
    >>               ^^^^^^^^
    
    >>
    
    >> 0 error(s), 1 warning(s), 83.3% typed
    
    >> KiraDest.js created

I got [JSC_UNDEFINED_VARIABLE] warning despite the option jscomp_off: ["undefinedVars"] in my Gruntfile.coffee.

4. Reason for suppression

For my site I add JavaScript to HTML via <script> tag. An example for Chocolat third-party plugin:

<!-- [INFO] First file -->
<script src="https://cdn.jsdelivr.net/npm/chocolat/dist/js/chocolat.min.js" defer></script>
<!-- [INFO] Second file — the JavaScript file from the MCVE above -->
<script src="../path/to/KiraChocolat.js" defer></script>

A first JavaScript file — third-party plugin; a second — my personal configuration for this plugin. And this is how I do it for all third-party plugins.

I get [JSC_UNDEFINED_VARIABLE] and [JSC_STRICT_INEXISTENT_PROPERTY] warnings for almost every JavaScript configuration file. I couldn’t find how I can fix these warnings (I don’t think writing extern for every third-party plugin is the best idea). Therefore, I suppress [JSC_UNDEFINED_VARIABLE] and [JSC_STRICT_INEXISTENT_PROPERTY].

If possible to fix, not to suppress [JSC_UNDEFINED_VARIABLE] and [JSC_STRICT_INEXISTENT_PROPERTY] in this case, please tell me what I have to do.

5. Environment

  1. Operating systems (Travis CI):

    1. Ubuntu 20.04.1 LTS
    2. Mac OS X 10.15.7
    3. Windows Server 1809
  2. Node.js 15.2.1

  3. Grunt:

    1. grunt 1.3.0
    2. grunt-cli 1.3.2
  4. google-closure-compiler (NPM release) 20201102.0.1

Thanks.

My guess is that the order that of application is interfering. Notice that jscomp_off is applied but then right after that jscomp_warning: * occurs. If those get applied in the wrong order, the warning might take precedence. There isn't any specific logic in the plugins regarding these flags. They are passed verbatim to the compiler.

Try switching the order they are listed in your config:

'closure-compiler':
            options:
                jscomp_warning: "*"
                jscomp_off: ["undefinedVars"]
                language_out: "ECMASCRIPT_NEXT"
                warning_level: "VERBOSE"