diff --git a/src/com/google/javascript/jscomp/SourceFile.java b/src/com/google/javascript/jscomp/SourceFile.java index d66c83407f9..02af8117396 100644 --- a/src/com/google/javascript/jscomp/SourceFile.java +++ b/src/com/google/javascript/jscomp/SourceFile.java @@ -360,13 +360,13 @@ private static boolean isZipEntry(String path) { } @GwtIncompatible("java.io.File") - private static SourceFile fromZipEntry(String zipURL, Charset inputCharset) { + private static SourceFile fromZipEntry(String zipURL, Charset inputCharset, SourceKind kind) { checkArgument(isZipEntry(zipURL)); String[] components = zipURL.split(Pattern.quote(BANG_SLASH.replace("/", File.separator))); try { String zipPath = components[0]; String relativePath = components[1]; - return fromZipEntry(zipPath, zipPath, relativePath, inputCharset); + return fromZipEntry(zipPath, zipPath, relativePath, inputCharset, kind); } catch (MalformedURLException e) { throw new RuntimeException(e); } @@ -376,8 +376,21 @@ private static SourceFile fromZipEntry(String zipURL, Charset inputCharset) { public static SourceFile fromZipEntry( String originalZipPath, String absoluteZipPath, String entryPath, Charset inputCharset) throws MalformedURLException { + return fromZipEntry( + originalZipPath, absoluteZipPath, entryPath, inputCharset, SourceKind.STRONG); + } + + @GwtIncompatible("java.io.File") + public static SourceFile fromZipEntry( + String originalZipPath, + String absoluteZipPath, + String entryPath, + Charset inputCharset, + SourceKind kind) + throws MalformedURLException { // No longer throws MalformedURLException but we are keeping it for backward compatbility. return builder() + .withKind(kind) .withCharset(inputCharset) .withOriginalPath(originalZipPath + BANG_SLASH + entryPath) .buildFromZipEntry( @@ -489,7 +502,7 @@ public SourceFile buildFromPath(Path path) { checkNotNull(path); checkNotNull(charset); if (isZipEntry(path.toString())) { - return fromZipEntry(path.toString(), charset); + return fromZipEntry(path.toString(), charset, kind); } return new OnDisk(path, originalPath, charset, kind); } @@ -658,7 +671,7 @@ private static class AtZip extends SourceFile { private transient Charset inputCharset; AtZip(ZipEntryReader zipEntryReader, String originalPath, Charset c, SourceKind kind) { - super(originalPath, SourceKind.STRONG); + super(originalPath, kind); this.inputCharset = c; this.zipEntryReader = zipEntryReader; setOriginalPath(originalPath); diff --git a/test/com/google/javascript/jscomp/SourceFileTest.java b/test/com/google/javascript/jscomp/SourceFileTest.java index 15683a81f96..427a3f2ad39 100644 --- a/test/com/google/javascript/jscomp/SourceFileTest.java +++ b/test/com/google/javascript/jscomp/SourceFileTest.java @@ -135,14 +135,26 @@ public void testSourceFileResolvesZipEntries() throws IOException { Path jsZipPath = Files.createTempFile("test", ".js.zip"); createZipWithContent(jsZipPath, expectedContent); - // Test SourceFile#fromZipEntry(String, String, String, Charset) + // Test SourceFile#fromZipEntry(String, String, String, Charset, SourceKind) SourceFile sourceFileFromZipEntry = SourceFile.fromZipEntry( jsZipPath.toString(), jsZipPath.toAbsolutePath().toString(), "foo.js", - StandardCharsets.UTF_8); + StandardCharsets.UTF_8, + SourceKind.WEAK); assertThat(sourceFileFromZipEntry.getCode()).isEqualTo(expectedContent); + assertThat(sourceFileFromZipEntry.getKind()).isEqualTo(SourceKind.WEAK); + + // Test SourceFile#fromZipEntry(String, String, String, Charset) + SourceFile sourceFileFromZipEntryDefaultKind = + SourceFile.fromZipEntry( + jsZipPath.toString(), + jsZipPath.toAbsolutePath().toString(), + "foo.js", + StandardCharsets.UTF_8); + assertThat(sourceFileFromZipEntryDefaultKind.getCode()).isEqualTo(expectedContent); + assertThat(sourceFileFromZipEntryDefaultKind.getKind()).isEqualTo(SourceKind.STRONG); // Test SourceFile#fromFile(String) SourceFile sourceFileFromFileString =