closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

import/export directives ignored when not at the start of line

Open msevcenko opened this issue 1 year ago • 2 comments

When debugging problems with unresolved import paths, it turned out that parsing import directives is triggered only when the directive actually starts the line:

JsFileRegexParser:347 (in version 20220601)

    if (line.startsWith("import") || line.startsWith("export")) {

If you put whitespace before the import directive, it is just ignored.

Not sure if this is correct and/or intentional, in any case it is rather unexpected.

msevcenko avatar Sep 13 '23 19:09 msevcenko

That does seem inconsistent, as the various goog.* patterns do not have that limitation, but it is explicitly documented as being different:

https://github.com/google/closure-compiler/blob/31018cb5b34f87d3ba519617be9e99eae7bb35de/src/com/google/javascript/jscomp/deps/JsFileRegexParser.java#L58-L64

The comment seems to suggest (but doesn't outright state) that this is to keep only one statement per line, but then goes on to say that it must be at the start of the line. Perhaps leading whitespace could be acceptable, while still keeping one statement per line?

niloc132 avatar Sep 13 '23 20:09 niloc132

Ok, just a strange glitch.

Not sure what are the exact consequences of ignoring the directive. Sometimes only the fact that the desired function (minification) actually works. Because processing the directive (in an environment when it cannot be resolved) is often something that user does not want or expect.

And AFAIK, processing static directives cannot be suppressed, can it? Only processing of the dynamic directives can be suppressed with --allow_dynamic_import, right?

msevcenko avatar Sep 14 '23 05:09 msevcenko