Skip to content

Commit

Permalink
Extend the SourceFile factory methods to support setting the source k…
Browse files Browse the repository at this point in the history
…ind.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=210601027
  • Loading branch information
tjgq authored and blickly committed Aug 28, 2018
1 parent b8a9773 commit 9318e32
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 27 deletions.
55 changes: 39 additions & 16 deletions src/com/google/javascript/jscomp/SourceFile.java
Expand Up @@ -108,6 +108,8 @@ public SourceFile(String fileName, SourceKind kind) {
} else { } else {
this.fileName = fileName; this.fileName = fileName;
} }

this.kind = kind;
} }


@Override @Override
Expand Down Expand Up @@ -397,9 +399,14 @@ public static SourceFile fromZipEntry(
.buildFromUrl(zipEntryUrl); .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") @GwtIncompatible("java.io.File")
public static SourceFile fromFile(String fileName, Charset charset) { public static SourceFile fromFile(String fileName, Charset charset) {
return builder().withCharset(charset).buildFromFile(fileName); return fromFile(fileName, charset, SourceKind.STRONG);
} }


@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
Expand All @@ -422,12 +429,21 @@ public static SourceFile fromFile(File file) {
} }


@GwtIncompatible("java.io.File") @GwtIncompatible("java.io.File")
public static SourceFile fromPath(Path path, Charset c) { public static SourceFile fromPath(Path path, Charset charset, SourceKind kind) {
return builder().withCharset(c).buildFromPath(path); 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) { public static SourceFile fromCode(String fileName, String code) {
return builder().buildFromCode(fileName, code); return fromCode(fileName, code, SourceKind.STRONG);
} }


/** /**
Expand Down Expand Up @@ -469,11 +485,18 @@ public static Builder builder() {
* the source file (if it differs from the path on disk). * the source file (if it differs from the path on disk).
*/ */
public static class Builder { public static class Builder {
private SourceKind kind = SourceKind.STRONG;
private Charset charset = UTF_8; private Charset charset = UTF_8;
private String originalPath = null; private String originalPath = null;


public Builder() {} 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. */ /** Set the charset to use when reading from an input stream or file. */
public Builder withCharset(Charset charset) { public Builder withCharset(Charset charset) {
this.charset = charset; this.charset = charset;
Expand Down Expand Up @@ -504,16 +527,16 @@ public SourceFile buildFromPath(Path path) {
if (isZipEntry(path.toString())) { if (isZipEntry(path.toString())) {
return fromZipEntry(path.toString(), charset); return fromZipEntry(path.toString(), charset);
} }
return new OnDisk(path, originalPath, charset); return new OnDisk(path, originalPath, charset, kind);
} }


@GwtIncompatible("java.net.URL") @GwtIncompatible("java.net.URL")
public SourceFile buildFromUrl(URL 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) { public SourceFile buildFromCode(String fileName, String code) {
return new Preloaded(fileName, originalPath, code); return new Preloaded(fileName, originalPath, code, kind);
} }


@GwtIncompatible("java.io.InputStream") @GwtIncompatible("java.io.InputStream")
Expand All @@ -527,7 +550,7 @@ public SourceFile buildFromReader(String fileName, Reader r) throws IOException
} }


public SourceFile buildFromGenerator(String fileName, Generator generator) { public SourceFile buildFromGenerator(String fileName, Generator generator) {
return new Generated(fileName, originalPath, generator); return new Generated(fileName, originalPath, generator, kind);
} }
} }


Expand All @@ -541,8 +564,8 @@ public SourceFile buildFromGenerator(String fileName, Generator generator) {
static class Preloaded extends SourceFile { static class Preloaded extends SourceFile {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;


Preloaded(String fileName, String originalPath, String code) { Preloaded(String fileName, String originalPath, String code, SourceKind kind) {
super(fileName, SourceKind.STRONG); super(fileName, kind);
super.setOriginalPath(originalPath); super.setOriginalPath(originalPath);
super.setCode(code); super.setCode(code);
} }
Expand All @@ -560,8 +583,8 @@ static class Generated extends SourceFile {
private transient Generator generator; private transient Generator generator;


// Not private, so that LazyInput can extend it. // Not private, so that LazyInput can extend it.
Generated(String fileName, String originalPath, Generator generator) { Generated(String fileName, String originalPath, Generator generator, SourceKind kind) {
super(fileName, SourceKind.STRONG); super(fileName, kind);
super.setOriginalPath(originalPath); super.setOriginalPath(originalPath);
this.generator = generator; this.generator = generator;
} }
Expand Down Expand Up @@ -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 * 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. * 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 { static class OnDisk extends SourceFile {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private transient Path path; private transient Path path;
private transient Charset inputCharset = UTF_8; private transient Charset inputCharset = UTF_8;


OnDisk(Path path, String originalPath, Charset c) { OnDisk(Path path, String originalPath, Charset c, SourceKind kind) {
super(path.toString(), SourceKind.STRONG); super(path.toString(), kind);
this.path = path; this.path = path;
setOriginalPath(originalPath); setOriginalPath(originalPath);
if (c != null) { if (c != null) {
Expand Down Expand Up @@ -706,7 +729,7 @@ static class AtUrl extends SourceFile {
// Default input file format for the compiler has always been UTF_8. // Default input file format for the compiler has always been UTF_8.
private String inputCharset = UTF_8.name(); 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(originalPath, SourceKind.STRONG);
super.setOriginalPath(originalPath); super.setOriginalPath(originalPath);
this.url = url; this.url = url;
Expand Down
34 changes: 23 additions & 11 deletions test/com/google/javascript/jscomp/SourceFileTest.java
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;


import com.google.common.io.MoreFiles; import com.google.common.io.MoreFiles;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
Expand All @@ -32,24 +33,35 @@


public final class SourceFileTest extends TestCase { public final class SourceFileTest extends TestCase {


private static class ResetableSourceFile extends SourceFile.Preloaded { public void testSourceKind() {
ResetableSourceFile(String fileName, String code) { SourceFile sf1 = SourceFile.fromCode("test1.js", "1");
super(fileName, null, code); assertThat(sf1.isStrong()).isTrue();
} assertThat(sf1.isWeak()).isFalse();

assertThat(sf1.isExtern()).isFalse();
void updateCode(String code) {
setCode(code); 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 { 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(1)).isEqualTo(0);
assertThat(sf.getLineOffset(2)).isEqualTo(5); assertThat(sf.getLineOffset(2)).isEqualTo(5);
assertThat(sf.getLineOffset(3)).isEqualTo(10); 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(1)).isEqualTo(0);
assertThat(sf.getLineOffset(2)).isEqualTo(7); assertThat(sf.getLineOffset(2)).isEqualTo(7);
assertThat(sf.getLineOffset(3)).isEqualTo(14); assertThat(sf.getLineOffset(3)).isEqualTo(14);
Expand Down

0 comments on commit 9318e32

Please sign in to comment.