Skip to content

Commit 826b0fc

Browse files
johnplaistedbrad4d
authored andcommitted
Fix detection of goog.js.
We need to: - Use something other than "var COMPILED = false;" to find base.js. Inside base.js.i.js it will start with "var COMPILED;" @provideGoog seems reasonable. - Scan externs for base.js and / or goog.js for incremental type checking. - Check for a file that provides "goog", rather than a file that provides only "goog" (base.js will also have a synthetic provide for its path). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=216644571
1 parent f8a3bcd commit 826b0fc

File tree

8 files changed

+123
-50
lines changed

8 files changed

+123
-50
lines changed

src/com/google/javascript/jscomp/RewriteGoogJsImports.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public enum Mode {
8888
LINT_AND_REWRITE
8989
}
9090

91-
private static final ImmutableList<String> EXPECTED_BASE_PROVIDES = ImmutableList.of("goog");
91+
private static final String EXPECTED_BASE_PROVIDE = "goog";
9292

9393
private final Mode mode;
9494
private final AbstractCompiler compiler;
@@ -326,7 +326,7 @@ private Node findGoogJsScriptNode(Node root) {
326326
// Find Closure's base.js file. goog.js should be right next to it.
327327
for (Node script : root.children()) {
328328
ImmutableList<String> provides = compiler.getInput(script.getInputId()).getProvides();
329-
if (EXPECTED_BASE_PROVIDES.equals(provides)) {
329+
if (provides.contains(EXPECTED_BASE_PROVIDE)) {
330330
// Use resolveModuleAsPath as if it is not part of the input we don't want to report an
331331
// error.
332332
expectedGoogPath =
@@ -365,6 +365,11 @@ public void process(Node externs, Node root) {
365365

366366
Node googJsScriptNode = findGoogJsScriptNode(root);
367367

368+
if (googJsScriptNode == null) {
369+
// Potentially in externs if library level type checking.
370+
googJsScriptNode = findGoogJsScriptNode(externs);
371+
}
372+
368373
if (mode == Mode.LINT_AND_REWRITE) {
369374
if (googJsScriptNode != null) {
370375
exportsFinder = new FindExports(googJsScriptNode);

src/com/google/javascript/jscomp/deps/JsFileLineParser.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ void doParse(String filePath, Reader fileContents) {
136136
++lineNum;
137137
try {
138138
String revisedLine = line;
139+
String revisedBlockCommentLine = "";
139140
if (inMultilineComment) {
140141
int endOfComment = revisedLine.indexOf("*/");
141142
if (endOfComment != -1) {
143+
revisedBlockCommentLine = revisedLine.substring(0, endOfComment + 2);
142144
revisedLine = revisedLine.substring(endOfComment + 2);
143145
inMultilineComment = false;
144146
} else {
147+
revisedBlockCommentLine = line;
145148
revisedLine = "";
146149
}
147150
}
@@ -156,14 +159,18 @@ void doParse(String filePath, Reader fileContents) {
156159
revisedLine = revisedLine.substring(0, startOfLineComment);
157160
break;
158161
} else if (startOfMultilineComment != -1) {
159-
int endOfMultilineComment = revisedLine.indexOf("*/",
160-
startOfMultilineComment + 2);
162+
int endOfMultilineComment = revisedLine.indexOf("*/", startOfMultilineComment + 2);
161163
if (endOfMultilineComment == -1) {
162-
revisedLine = revisedLine.substring(
163-
0, startOfMultilineComment);
164+
revisedBlockCommentLine = revisedLine.substring(startOfMultilineComment);
165+
revisedLine = revisedLine.substring(0, startOfMultilineComment);
164166
inMultilineComment = true;
165167
break;
166168
} else {
169+
if (!parseBlockCommentLine(
170+
revisedLine.substring(startOfMultilineComment, endOfMultilineComment + 2))
171+
&& shortcutMode) {
172+
break;
173+
}
167174
revisedLine =
168175
revisedLine.substring(0, startOfMultilineComment) +
169176
revisedLine.substring(endOfMultilineComment + 2);
@@ -174,6 +181,12 @@ void doParse(String filePath, Reader fileContents) {
174181
}
175182
}
176183

184+
if (!revisedBlockCommentLine.isEmpty()) {
185+
if (!parseBlockCommentLine(revisedBlockCommentLine) && shortcutMode) {
186+
break;
187+
}
188+
}
189+
177190
if (!revisedLine.isEmpty()) {
178191
// This check for shortcut mode should be redundant, but
179192
// it's done for safety reasons.
@@ -208,12 +221,15 @@ void doParse(String filePath, Reader fileContents) {
208221
*/
209222
abstract boolean parseLine(String line) throws ParseException;
210223

224+
boolean parseBlockCommentLine(String line) {
225+
return true;
226+
}
227+
211228
/**
212229
* Parses a JS string literal.
213230
*
214231
* @param jsStringLiteral The literal. Must look like "asdf" or 'asdf'
215-
* @throws ParseException Thrown if there is a string literal that cannot be
216-
* parsed.
232+
* @throws ParseException Thrown if there is a string literal that cannot be parsed.
217233
*/
218234
String parseJsString(String jsStringLiteral) throws ParseException {
219235
valueMatcher.reset(jsStringLiteral);

src/com/google/javascript/jscomp/deps/JsFileParser.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public final class JsFileParser extends JsFileLineParser {
8787
*/
8888
private static final Pattern ES6_EXPORT_PATTERN = Pattern.compile("^export\\b");
8989

90-
/** The first non-comment line of base.js */
91-
private static final String BASE_JS_START = "var COMPILED = false;";
90+
/** Line in comment indicating that the file is Closure's base.js. */
91+
private static final String PROVIDES_GOOG_COMMENT = "@provideGoog";
9292

9393
/** The start of a bundled goog.module, i.e. one that is wrapped in a goog.loadModule call */
9494
private static final String BUNDLED_GOOG_MODULE_START = "goog.loadModule(function(";
@@ -245,10 +245,16 @@ private void setModuleType(ModuleType type) {
245245
moduleType = type;
246246
}
247247

248-
/**
249-
* Parses a line of JavaScript, extracting goog.provide and goog.require
250-
* information.
251-
*/
248+
@Override
249+
protected boolean parseBlockCommentLine(String line) {
250+
if (includeGoogBase && line.contains(PROVIDES_GOOG_COMMENT)) {
251+
provides.add("goog");
252+
return false;
253+
}
254+
return true;
255+
}
256+
257+
/** Parses a line of JavaScript, extracting goog.provide and goog.require information. */
252258
@Override
253259
protected boolean parseLine(String line) throws ParseException {
254260
boolean lineHasProvidesOrRequires = false;
@@ -308,12 +314,6 @@ protected boolean parseLine(String line) throws ParseException {
308314
}
309315
}
310316
}
311-
} else if (includeGoogBase && line.startsWith(BASE_JS_START) &&
312-
provides.isEmpty() && requires.isEmpty()) {
313-
provides.add("goog");
314-
315-
// base.js can't provide or require anything else.
316-
return false;
317317
}
318318

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

test/com/google/javascript/jscomp/CommandLineRunnerTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,9 @@ public void testSourceSortingOn() {
813813
public void testSourceSortingOn2() {
814814
test(
815815
new String[] {
816-
"goog.provide('a');",
817-
"goog.require('a');\n/** This is base.js */\nvar COMPILED = false;",
816+
"goog.provide('a');", "goog.require('a');",
818817
},
819-
new String[] {"var a={};", "var COMPILED=!1"});
818+
new String[] {"var a={};", ""});
820819
}
821820

822821
@Test
@@ -826,7 +825,7 @@ public void testSourceSortingOn3() {
826825
test(
827826
new String[] {
828827
"goog.addDependency('sym', [], []);\nvar x = 3;",
829-
"/** This is base.js */\nvar COMPILED = false;",
828+
"/** This is base.js @provideGoog */ var COMPILED = false;",
830829
},
831830
new String[] {"var COMPILED = !1;", "var x = 3;"});
832831
}
@@ -940,7 +939,7 @@ public void testSourcePruningOn7() {
940939
args.add("--dependency_mode=LOOSE");
941940
test(
942941
new String[] {
943-
"/** This is base.js */\nvar COMPILED = false;",
942+
"/** This is base.js @provideGoog */ var COMPILED = false;",
944943
},
945944
new String[] {
946945
"var COMPILED = !1;",
@@ -1055,19 +1054,20 @@ public void testOnlyClosureDependenciesEmptyEntryPoints() throws Exception {
10551054
public void testOnlyClosureDependenciesOneEntryPoint() {
10561055
args.add("--dependency_mode=STRICT");
10571056
args.add("--entry_point=goog:beer");
1058-
test(new String[] {
1057+
test(
1058+
new String[] {
10591059
"goog.require('beer'); var beerRequired = 1;",
10601060
"goog.provide('beer');\ngoog.require('hops');\nvar beerProvided = 1;",
10611061
"goog.provide('hops'); var hopsProvided = 1;",
10621062
"goog.provide('scotch'); var scotchProvided = 1;",
10631063
"goog.require('scotch');\nvar includeFileWithoutProvides = 1;",
1064-
"/** This is base.js */\nvar COMPILED = false;",
1065-
},
1066-
new String[] {
1067-
"var COMPILED = !1;",
1068-
"var hops = {}, hopsProvided = 1;",
1069-
"var beer = {}, beerProvided = 1;"
1070-
});
1064+
"/** This is base.js @provideGoog */ var COMPILED = false;",
1065+
},
1066+
new String[] {
1067+
"var COMPILED = !1;",
1068+
"var hops = {}, hopsProvided = 1;",
1069+
"var beer = {}, beerProvided = 1;"
1070+
});
10711071
}
10721072

10731073
@Test

test/com/google/javascript/jscomp/CompilerTestCase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,21 @@ protected static Externs externs(String... srcTexts) {
22362236
return new Externs(createSources("externs", srcTexts));
22372237
}
22382238

2239+
protected static Externs externs(SourceFile... externs) {
2240+
// Copy SourceFile objects to prevent the externs bit from polluting tests.
2241+
return new Externs(
2242+
Arrays.stream(externs)
2243+
.map(
2244+
f -> {
2245+
try {
2246+
return SourceFile.fromCode(f.getName(), f.getCode());
2247+
} catch (IOException e) {
2248+
throw new RuntimeException(e);
2249+
}
2250+
})
2251+
.collect(ImmutableList.toImmutableList()));
2252+
}
2253+
22392254
protected static Externs externs(List<SourceFile> files) {
22402255
return new Externs(files);
22412256
}

test/com/google/javascript/jscomp/RewriteGoogJsImportsTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
@RunWith(JUnit4.class)
3232
public final class RewriteGoogJsImportsTest extends CompilerTestCase {
33-
// JsFileParser determines if this file is base.js by looking at the first line of the file.
33+
// JsFileParser determines if this file is base.js by looking at the first comment of the file.
3434
private static final SourceFile BASE =
35-
SourceFile.fromCode("/closure/base.js", "var COMPILED = false;");
35+
SourceFile.fromCode("/closure/base.js", "/** @provideGoog */");
3636

3737
private static final SourceFile GOOG =
3838
SourceFile.fromCode(
@@ -102,6 +102,24 @@ public void testImportStar() {
102102
"use(goog.require, goog.foo, goog.MyClass, goog.constant);"))));
103103
}
104104

105+
@Test
106+
public void testGoogAndBaseInExterns() {
107+
test(
108+
externs(BASE, GOOG),
109+
srcs(
110+
SourceFile.fromCode(
111+
"testcode",
112+
lines(
113+
"import * as goog from './closure/goog.js';",
114+
"use(goog.require, goog.foo, goog.MyClass, goog.constant);"))),
115+
expected(
116+
SourceFile.fromCode(
117+
"testcode",
118+
lines(
119+
"import './closure/goog.js';",
120+
"use(goog.require, goog.foo, goog.MyClass, goog.constant);"))));
121+
}
122+
105123
@Test
106124
public void testBadPropertyAccess() {
107125
test(

test/com/google/javascript/jscomp/deps/JsFileLineParserTest.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.javascript.jscomp.PrintStreamErrorManager;
2323
import java.io.StringReader;
2424
import junit.framework.TestCase;
25-
import org.junit.Before;
2625
import org.junit.Test;
2726
import org.junit.runner.RunWith;
2827
import org.junit.runners.JUnit4;
@@ -35,16 +34,6 @@
3534
@RunWith(JUnit4.class)
3635
public final class JsFileLineParserTest extends TestCase {
3736

38-
TestParser parser;
39-
private ErrorManager errorManager;
40-
41-
@Override
42-
@Before
43-
public void setUp() {
44-
errorManager = new PrintStreamErrorManager(System.err);
45-
parser = new TestParser(errorManager);
46-
}
47-
4837
@Test
4938
public void testSingleLine1() {
5039
assertStrip("2", "// 1\n2");
@@ -95,13 +84,40 @@ public void testMixedLine2() {
9584
assertStrip("1 34", "1/** // 2 **/ 3\n4");
9685
}
9786

87+
@Test
88+
public void testBlockComment() {
89+
assertBlocks("/** one line */", "/** one line */");
90+
}
91+
92+
@Test
93+
public void testInlineBlockComment() {
94+
assertBlocks("/** one line */", "var x; /** one line */");
95+
assertBlocks("/** one line */", "/** one line */ var y;");
96+
assertBlocks("/** one line */", "var x; /** one line */ var y;");
97+
}
98+
99+
@Test
100+
public void testMultipleBlockComments() {
101+
assertBlocks("/** first *//** * second */", "/** first */\n/**\n * second\n */");
102+
}
103+
98104
private void assertStrip(String expected, String input) {
105+
ErrorManager errorManager = new PrintStreamErrorManager(System.err);
106+
TestParser parser = new TestParser(errorManager);
99107
parser.doParse("file", new StringReader(input));
100108
assertThat(parser.toString()).isEqualTo(expected);
101109
}
102110

111+
private void assertBlocks(String expected, String input) {
112+
ErrorManager errorManager = new PrintStreamErrorManager(System.err);
113+
TestParser parser = new TestParser(errorManager);
114+
parser.doParse("file", new StringReader(input));
115+
assertThat(parser.comments.toString()).isEqualTo(expected);
116+
}
117+
103118
private static class TestParser extends JsFileLineParser {
104119
StringBuilder sb = new StringBuilder();
120+
StringBuilder comments = new StringBuilder();
105121

106122
TestParser(ErrorManager errorManager) {
107123
super(errorManager);
@@ -113,6 +129,12 @@ boolean parseLine(String line) {
113129
return true;
114130
}
115131

132+
@Override
133+
boolean parseBlockCommentLine(String line) {
134+
comments.append(line);
135+
return true;
136+
}
137+
116138
@Override
117139
public String toString() {
118140
return sb.toString();

test/com/google/javascript/jscomp/deps/JsFileParserTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,7 @@ public void testShortcutMode3() {
498498

499499
@Test
500500
public void testIncludeGoog1() {
501-
String contents = "/**\n" +
502-
" * the first constant in base.js\n" +
503-
" */\n" +
504-
"var COMPILED = false;\n";
501+
String contents = "/**\n" + " * @provideGoog\n" + " */\n";
505502

506503
DependencyInfo expected = SimpleDependencyInfo.builder(CLOSURE_PATH, SRC_PATH)
507504
.setProvides(ImmutableList.of("goog"))

0 commit comments

Comments
 (0)