diff --git a/src/com/google/javascript/jscomp/SourceFile.java b/src/com/google/javascript/jscomp/SourceFile.java index 2e50436d697..534ad362f63 100644 --- a/src/com/google/javascript/jscomp/SourceFile.java +++ b/src/com/google/javascript/jscomp/SourceFile.java @@ -108,6 +108,8 @@ public SourceFile(String fileName, SourceKind kind) { } else { this.fileName = fileName; } + + this.kind = kind; } @Override @@ -397,9 +399,14 @@ public static SourceFile fromZipEntry( .buildFromUrl(zipEntryUrl); } + @GwtIncompatible("java.io.File") + public static SourceFile fromFile(String fileName, Charset charset, SourceKind kind) { + return builder().withKind(kind).withCharset(charset).buildFromFile(fileName); + } + @GwtIncompatible("java.io.File") public static SourceFile fromFile(String fileName, Charset charset) { - return builder().withCharset(charset).buildFromFile(fileName); + return fromFile(fileName, charset, SourceKind.STRONG); } @GwtIncompatible("java.io.File") @@ -422,12 +429,21 @@ public static SourceFile fromFile(File file) { } @GwtIncompatible("java.io.File") - public static SourceFile fromPath(Path path, Charset c) { - return builder().withCharset(c).buildFromPath(path); + public static SourceFile fromPath(Path path, Charset charset, SourceKind kind) { + return builder().withKind(kind).withCharset(charset).buildFromPath(path); + } + + @GwtIncompatible("java.io.File") + public static SourceFile fromPath(Path path, Charset charset) { + return fromPath(path, charset, SourceKind.STRONG); + } + + public static SourceFile fromCode(String fileName, String code, SourceKind kind) { + return builder().withKind(kind).buildFromCode(fileName, code); } public static SourceFile fromCode(String fileName, String code) { - return builder().buildFromCode(fileName, code); + return fromCode(fileName, code, SourceKind.STRONG); } /** @@ -469,11 +485,18 @@ public static Builder builder() { * the source file (if it differs from the path on disk). */ public static class Builder { + private SourceKind kind = SourceKind.STRONG; private Charset charset = UTF_8; private String originalPath = null; public Builder() {} + /** Set the source kind. */ + public Builder withKind(SourceKind kind) { + this.kind = kind; + return this; + } + /** Set the charset to use when reading from an input stream or file. */ public Builder withCharset(Charset charset) { this.charset = charset; @@ -504,16 +527,16 @@ public SourceFile buildFromPath(Path path) { if (isZipEntry(path.toString())) { return fromZipEntry(path.toString(), charset); } - return new OnDisk(path, originalPath, charset); + return new OnDisk(path, originalPath, charset, kind); } @GwtIncompatible("java.net.URL") public SourceFile buildFromUrl(URL url) { - return new AtUrl(url, originalPath, charset); + return new AtUrl(url, originalPath, charset, kind); } public SourceFile buildFromCode(String fileName, String code) { - return new Preloaded(fileName, originalPath, code); + return new Preloaded(fileName, originalPath, code, kind); } @GwtIncompatible("java.io.InputStream") @@ -527,7 +550,7 @@ public SourceFile buildFromReader(String fileName, Reader r) throws IOException } public SourceFile buildFromGenerator(String fileName, Generator generator) { - return new Generated(fileName, originalPath, generator); + return new Generated(fileName, originalPath, generator, kind); } } @@ -541,8 +564,8 @@ public SourceFile buildFromGenerator(String fileName, Generator generator) { static class Preloaded extends SourceFile { private static final long serialVersionUID = 1L; - Preloaded(String fileName, String originalPath, String code) { - super(fileName, SourceKind.STRONG); + Preloaded(String fileName, String originalPath, String code, SourceKind kind) { + super(fileName, kind); super.setOriginalPath(originalPath); super.setCode(code); } @@ -560,8 +583,8 @@ static class Generated extends SourceFile { private transient Generator generator; // Not private, so that LazyInput can extend it. - Generated(String fileName, String originalPath, Generator generator) { - super(fileName, SourceKind.STRONG); + Generated(String fileName, String originalPath, Generator generator, SourceKind kind) { + super(fileName, kind); super.setOriginalPath(originalPath); this.generator = generator; } @@ -595,14 +618,14 @@ public void restoreFrom(SourceFile sourceFile) { * A source file where the code is only read into memory if absolutely necessary. We will try to * delay loading the code into memory as long as possible. */ - @GwtIncompatible("java.io.File") + @GwtIncompatible("com.google.common.io.CharStreams") static class OnDisk extends SourceFile { private static final long serialVersionUID = 1L; private transient Path path; private transient Charset inputCharset = UTF_8; - OnDisk(Path path, String originalPath, Charset c) { - super(path.toString(), SourceKind.STRONG); + OnDisk(Path path, String originalPath, Charset c, SourceKind kind) { + super(path.toString(), kind); this.path = path; setOriginalPath(originalPath); if (c != null) { @@ -706,7 +729,7 @@ static class AtUrl extends SourceFile { // Default input file format for the compiler has always been UTF_8. private String inputCharset = UTF_8.name(); - AtUrl(URL url, String originalPath, Charset c) { + AtUrl(URL url, String originalPath, Charset c, SourceKind kind) { super(originalPath, SourceKind.STRONG); super.setOriginalPath(originalPath); this.url = url; diff --git a/test/com/google/javascript/jscomp/SourceFileTest.java b/test/com/google/javascript/jscomp/SourceFileTest.java index f2203de3033..b004c5da81d 100644 --- a/test/com/google/javascript/jscomp/SourceFileTest.java +++ b/test/com/google/javascript/jscomp/SourceFileTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.io.MoreFiles; +import com.google.javascript.rhino.StaticSourceFile.SourceKind; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -32,24 +33,35 @@ public final class SourceFileTest extends TestCase { - private static class ResetableSourceFile extends SourceFile.Preloaded { - ResetableSourceFile(String fileName, String code) { - super(fileName, null, code); - } - - void updateCode(String code) { - setCode(code); - } + public void testSourceKind() { + SourceFile sf1 = SourceFile.fromCode("test1.js", "1"); + assertThat(sf1.isStrong()).isTrue(); + assertThat(sf1.isWeak()).isFalse(); + assertThat(sf1.isExtern()).isFalse(); + + sf1.setKind(SourceKind.WEAK); + assertThat(sf1.isStrong()).isFalse(); + assertThat(sf1.isWeak()).isTrue(); + assertThat(sf1.isExtern()).isFalse(); + + SourceFile sf2 = SourceFile.fromCode("test2.js", "2", SourceKind.WEAK); + assertThat(sf2.isStrong()).isFalse(); + assertThat(sf2.isWeak()).isTrue(); + assertThat(sf2.isExtern()).isFalse(); + + sf2.setKind(SourceKind.EXTERN); + assertThat(sf2.isStrong()).isFalse(); + assertThat(sf2.isWeak()).isFalse(); + assertThat(sf2.isExtern()).isTrue(); } - /** Tests that keys are assigned sequentially. */ public void testLineOffset() throws Exception { - ResetableSourceFile sf = new ResetableSourceFile("test.js", "'1';\n'2';\n'3'\n"); + SourceFile sf = SourceFile.fromCode("test.js", "'1';\n'2';\n'3'\n"); assertThat(sf.getLineOffset(1)).isEqualTo(0); assertThat(sf.getLineOffset(2)).isEqualTo(5); assertThat(sf.getLineOffset(3)).isEqualTo(10); - sf.updateCode("'100';\n'200;'\n'300'\n"); + sf.setCode("'100';\n'200;'\n'300'\n"); assertThat(sf.getLineOffset(1)).isEqualTo(0); assertThat(sf.getLineOffset(2)).isEqualTo(7); assertThat(sf.getLineOffset(3)).isEqualTo(14);