Skip to content

Warning "used but not goog.require'd" doesn't make sense here #1143

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

Closed
supersteves opened this issue Sep 15, 2015 · 10 comments
Closed

Warning "used but not goog.require'd" doesn't make sense here #1143

supersteves opened this issue Sep 15, 2015 · 10 comments

Comments

@supersteves
Copy link
Contributor

I'm loading a CommonJS module in the browser. Externs:

/**
 * @param  {string} module
 * @return {*}
 */
var require = function(module) { return {}; };

Code:

goog.module("test");
let SomeCommonJsMod = require("SomeCommonJsMod");
exports = new SomeCommonJsMod();

Warning:

input0:5: WARNING - 'SomeCommonJsMod' used but not goog.require'd
exports = new SomeCommonJsMod();
          ^

Test case:
https://closure-compiler-debugger.appspot.com/#input0%3Dgoog.module(%2522test%2522)%253B%250Alet%2520SomeCommonJsMod%2520%253D%2520require(%2522SomeCommonJsMod%2522)%253B%250Aexports%2520%253D%2520new%2520SomeCommonJsMod()%253B%250A%250A%26input1%26conformanceConfig%26externs%3D%252F**%250A%2520*%2520%2540param%2520%2520%257Bstring%257D%2520module%250A%2520*%2520%2540return%2520%257B*%257D%250A%2520*%252F%250Avar%2520require%2520%253D%2520function(module)%2520%257B%2520return%2520%257B%257D%253B%2520%257D%253B%250A%26refasterjs-template%26CHECK_REQUIRES%3D1%26LANG_IN_IS_ES6%3D1

I'm not sure what case this warning is trying to assist with, but this isn't it. Something to do with it being a constructor call (see https://github.com/google/closure-compiler/blob/b7abddebe2d9da6cac9827f452ebba2c0e980f93/src/com/google/javascript/jscomp/CheckRequiresForConstructors.java).

It doesn't happen if I write:

goog.module("test");
let SomeCommonJsMod = require("SomeCommonJsMod");
exports = SomeCommonJsMod;

Also If I do this, it doesn't happen:

goog.module("test");
{
  let SomeCommonJsMod = require("SomeCommonJsMod");
  exports = new SomeCommonJsMod();
}

(and is my block-scoped exports line here a valid way of using goog.module? Is it OK to work around this warning - which my project classes as a fatal error - by block-scoping the exports and also the ES6 class definition if any?)

@MatrixFrog
Copy link
Contributor

If you're using a combination of goog.{module,provide,require} and commonJS then it probably makes sense to turn that off: --jscomp_off=missingRequire

@supersteves
Copy link
Contributor Author

@blickly
Copy link
Contributor

blickly commented Sep 15, 2015

An alternate way to fix but still get the check would be to define a CommonJS coding convention with the correct implementation of

public String extractClassNameIfRequire(Node node, Node parent);

that understands the CommonJS-style require().

@supersteves
Copy link
Contributor Author

Another workaround I found was to create a goog.module which aliases the dynamically loaded CommonJS modules:

goog.module("myAlias");
exports = {
    SomeCommonJsMod: require("SomeCommonJsMod")
};

Then to use it:

const SomeCommonJsMod = goog.require("myAlias").SomeCommonJsMod;

Currently I'm using this approach to load React while avoiding polluting window.React.

I don't want to turn off warnings about missing requires, because the codebase is extensive and primarily goog.module/provide based.

@supersteves
Copy link
Contributor Author

I'm now trying React as a global (window.React), instead of dynamic runtime loading.

See PR #1147

Here I'm using externs traditionally. I still get this "used but not goog.require'd" warning. Should I? If my little PR is accepted, this can be easily worked around using "@Suppress".

@blickly
Copy link
Contributor

blickly commented Sep 16, 2015

If you aren't using goog.provide/goog.require or some other mechanism that
has an associated CodingConvention, then there's no reason to turn on the
missingRequire check at all, and you're better just to turn it off with
--jscomp_off=missingRequire

On Wed, Sep 16, 2015 at 3:24 AM, supersteves [email protected]
wrote:

I'm now trying React as a global (window.React), instead of dynamic
runtime loading.

See PR #1147 #1147

Here I'm using externs traditionally. I still get this "used but not
goog.require'd" warning. Should I? If my little PR is accepted, this can be
easily worked around using "@Suppress https://github.com/suppress".


Reply to this email directly or view it on GitHub
#1143 (comment)
.

@supersteves
Copy link
Contributor Author

Understood, however I am using goog.* extensively, while starting to use React (now using window.React, having given up dynamically loading via CommonJS require):

class A extends React.Component { // window.React.Component is defined in externs
}

If it weren't for #1145 and #1125, I'd post a test case.

So I don't want to turn off a warning which is useful for 99% of my codebase.

@blickly
Copy link
Contributor

blickly commented Sep 17, 2015

Ah, OK, got it. I do think it makes sense to have class-level suppressions.

On Wed, Sep 16, 2015 at 9:36 AM, supersteves [email protected]
wrote:

Understood, however I am using goog.* extensively, while starting to use
React (now using window.React, having given up dynamically loading via
CommonJS require):

class A extends React.Component { // window.React.Component is defined in externs
}

If it weren't for #1145
#1145 and #1125
#1125, I'd post a test
case.

So I don't want to turn off a warning which is useful for 99% of my
codebase.


Reply to this email directly or view it on GitHub
#1143 (comment)
.

@Dominator008
Copy link
Contributor

Looks like this is fixed by the recent changes. @MatrixFrog

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

No branches or pull requests

4 participants