Skip to content

Commit

Permalink
Modify SourceFile to store whether a file is strong, weak or externs.
Browse files Browse the repository at this point in the history
This is necessary to support eliding weak sources passed under the --weakdep flag from the output.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=209794908
  • Loading branch information
tjgq authored and blickly committed Aug 27, 2018
1 parent 470b603 commit 95f6fed
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -1787,7 +1787,7 @@ Node parseInputs() {
} }


if (NodeUtil.isFromTypeSummary(n)) { if (NodeUtil.isFromTypeSummary(n)) {
input.setIsExtern(true); input.setIsExtern();
externsRoot.addChildToBack(n); externsRoot.addChildToBack(n);
} else { } else {
jsRoot.addChildToBack(n); jsRoot.addChildToBack(n);
Expand Down Expand Up @@ -1991,7 +1991,7 @@ private boolean hoistIfExtern(CompilerInput input) {
// If the input file is explicitly marked as an externs file, then move it out of the main // If the input file is explicitly marked as an externs file, then move it out of the main
// JS root and put it with the other externs. // JS root and put it with the other externs.
externsRoot.addChildToBack(n); externsRoot.addChildToBack(n);
input.setIsExtern(true); input.setIsExtern();


input.getModule().remove(input); input.getModule().remove(input);


Expand Down
21 changes: 15 additions & 6 deletions src/com/google/javascript/jscomp/CompilerInput.java
Expand Up @@ -33,6 +33,7 @@
import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.rhino.InputId; import com.google.javascript.rhino.InputId;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -70,6 +71,15 @@ public class CompilerInput extends DependencyInfo.Base implements SourceAst {
private transient AbstractCompiler compiler; private transient AbstractCompiler compiler;
private transient ModulePath modulePath; private transient ModulePath modulePath;


// TODO(tjgq): Whether a CompilerInput is an externs file is determined by the `isExtern`
// constructor argument and the `setIsExtern` method. Both are necessary because, while externs
// files passed under the --externs flag are not required to contain an @externs annotation,
// externs files passed under any other flag must have one, and the presence of the latter can
// only be known once the file has been parsed. To add to the confusion, note that CompilerInput
// doesn't actually store the extern bit itself, but instead mutates the SourceFile associated
// with the AST node. Once (when?) we enforce that extern files always contain an @externs
// annotation, we can store the extern bit in the AST node, and make SourceFile immutable.

public CompilerInput(SourceAst ast) { public CompilerInput(SourceAst ast) {
this(ast, ast.getSourceFile().getName(), false); this(ast, ast.getSourceFile().getName(), false);
} }
Expand All @@ -86,10 +96,8 @@ public CompilerInput(SourceAst ast, InputId inputId, boolean isExtern) {
this.ast = ast; this.ast = ast;
this.id = inputId; this.id = inputId;


// TODO(nicksantos): Add a precondition check here. People are passing if (isExtern) {
// in null, but they should not be. setIsExtern();
if (ast != null && ast.getSourceFile() != null) {
ast.getSourceFile().setIsExtern(isExtern);
} }
} }


Expand Down Expand Up @@ -498,11 +506,12 @@ public boolean isExtern() {
return ast.getSourceFile().isExtern(); return ast.getSourceFile().isExtern();
} }


void setIsExtern(boolean isExtern) { void setIsExtern() {
// TODO(tjgq): Add a precondition check here. People are passing in null, but they shouldn't be.
if (ast == null || ast.getSourceFile() == null) { if (ast == null || ast.getSourceFile() == null) {
return; return;
} }
ast.getSourceFile().setIsExtern(isExtern); ast.getSourceFile().setKind(SourceKind.EXTERN);
} }


public int getLineOffset(int lineno) { public int getLineOffset(int lineno) {
Expand Down
41 changes: 23 additions & 18 deletions src/com/google/javascript/jscomp/SourceFile.java
Expand Up @@ -25,6 +25,7 @@
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.StaticSourceFile;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
Expand Down Expand Up @@ -57,6 +58,7 @@
* @author nicksantos@google.com (Nick Santos) * @author nicksantos@google.com (Nick Santos)
*/ */
public class SourceFile implements StaticSourceFile, Serializable { public class SourceFile implements StaticSourceFile, Serializable {

private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final String UTF8_BOM = "\uFEFF"; private static final String UTF8_BOM = "\uFEFF";


Expand All @@ -74,7 +76,7 @@ public interface Generator {
private static final int SOURCE_EXCERPT_REGION_LENGTH = 5; private static final int SOURCE_EXCERPT_REGION_LENGTH = 5;


private final String fileName; private final String fileName;
private boolean isExternFile = false; private SourceKind kind;


// The fileName may not always identify the original file - for example, // The fileName may not always identify the original file - for example,
// supersourced Java inputs, or Java inputs that come from Jar files. This // supersourced Java inputs, or Java inputs that come from Jar files. This
Expand All @@ -94,8 +96,9 @@ public interface Generator {
* @param fileName The file name of the source file. It does not necessarily need to correspond to * @param fileName The file name of the source file. It does not necessarily need to correspond to
* a real path. But it should be unique. Will appear in warning messages emitted by the * a real path. But it should be unique. Will appear in warning messages emitted by the
* compiler. * compiler.
* @param kind The source kind.
*/ */
public SourceFile(String fileName) { public SourceFile(String fileName, SourceKind kind) {
if (isNullOrEmpty(fileName)) { if (isNullOrEmpty(fileName)) {
throw new IllegalArgumentException("a source must have a name"); throw new IllegalArgumentException("a source must have a name");
} }
Expand Down Expand Up @@ -145,9 +148,6 @@ private void resetLineOffsets() {
lineOffsets = null; lineOffsets = null;
} }


//////////////////////////////////////////////////////////////////////////////
// Implementation

/** /**
* Gets all the code in this source file. * Gets all the code in this source file.
* @throws IOException * @throws IOException
Expand All @@ -156,7 +156,6 @@ public String getCode() throws IOException {
return code; return code;
} }



/** /**
* Gets a reader for the code in this source file. * Gets a reader for the code in this source file.
*/ */
Expand Down Expand Up @@ -210,15 +209,21 @@ public String getName() {
return fileName; return fileName;
} }


/** Returns whether this is an extern. */ /** Returns the source kind. */
@Override @Override
public boolean isExtern() { public SourceKind getKind() {
return isExternFile; return kind;
} }


/** Sets that this is an extern. */ /**
void setIsExtern(boolean newVal) { * Sets the source kind.
isExternFile = newVal; *
* <p>TODO(tjgq): Move the extern bit into the AST, so we can make the kind immutable. This is
* currently not possible because for some files the extern bit is not determined by the contents
* (e.g. files passed under the --externs flag and missing an externs annotation).
*/
void setKind(SourceKind kind) {
this.kind = kind;
} }


@Override @Override
Expand Down Expand Up @@ -537,7 +542,7 @@ 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) {
super(fileName); super(fileName, SourceKind.STRONG);
super.setOriginalPath(originalPath); super.setOriginalPath(originalPath);
super.setCode(code); super.setCode(code);
} }
Expand All @@ -556,7 +561,7 @@ static class Generated extends SourceFile {


// 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) {
super(fileName); super(fileName, SourceKind.STRONG);
super.setOriginalPath(originalPath); super.setOriginalPath(originalPath);
this.generator = generator; this.generator = generator;
} }
Expand Down Expand Up @@ -597,7 +602,7 @@ static class OnDisk extends SourceFile {
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) {
super(path.toString()); super(path.toString(), SourceKind.STRONG);
this.path = path; this.path = path;
setOriginalPath(originalPath); setOriginalPath(originalPath);
if (c != null) { if (c != null) {
Expand Down Expand Up @@ -670,7 +675,7 @@ private void writeObject(java.io.ObjectOutputStream out) throws Exception {
out.writeObject(inputCharset != null ? inputCharset.name() : null); out.writeObject(inputCharset != null ? inputCharset.name() : null);
out.writeObject(path != null ? path.toUri() : null); out.writeObject(path != null ? path.toUri() : null);
} }

@GwtIncompatible("ObjectInputStream") @GwtIncompatible("ObjectInputStream")
private void readObject(java.io.ObjectInputStream in) throws Exception { private void readObject(java.io.ObjectInputStream in) throws Exception {
in.defaultReadObject(); in.defaultReadObject();
Expand Down Expand Up @@ -702,9 +707,9 @@ static class AtUrl extends SourceFile {
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) {
super(originalPath); super(originalPath, SourceKind.STRONG);
this.url = url;
super.setOriginalPath(originalPath); super.setOriginalPath(originalPath);
this.url = url;
if (c != null) { if (c != null) {
this.setCharset(c); this.setCharset(c);
} }
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/SyntheticAst.java
Expand Up @@ -21,6 +21,7 @@
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.InputId; import com.google.javascript.rhino.InputId;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;


/** /**
* An AST generated totally by the compiler. * An AST generated totally by the compiler.
Expand All @@ -37,13 +38,13 @@ public final class SyntheticAst implements SourceAst {


SyntheticAst(String sourceName) { SyntheticAst(String sourceName) {
this.inputId = new InputId(sourceName); this.inputId = new InputId(sourceName);
this.sourceFile = new SourceFile(sourceName); this.sourceFile = new SourceFile(sourceName, SourceKind.STRONG);
clearAst(); clearAst();
} }


public SyntheticAst(Node root) { public SyntheticAst(Node root) {
this.inputId = new InputId(root.getSourceFileName()); this.inputId = new InputId(root.getSourceFileName());
this.sourceFile = new SourceFile(root.getSourceFileName()); this.sourceFile = new SourceFile(root.getSourceFileName(), SourceKind.STRONG);
this.root = checkNotNull(root); this.root = checkNotNull(root);
} }


Expand Down
3 changes: 2 additions & 1 deletion src/com/google/javascript/rhino/Node.java
Expand Up @@ -47,6 +47,7 @@
import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSType;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInput; import java.io.ObjectInput;
Expand Down Expand Up @@ -1334,7 +1335,7 @@ public final void setStaticSourceFile(@Nullable StaticSourceFile file) {


/** Sets the source file to a non-extern file of the given name. */ /** Sets the source file to a non-extern file of the given name. */
public final void setSourceFileForTesting(String name) { public final void setSourceFileForTesting(String name) {
this.putProp(STATIC_SOURCE_FILE, new SimpleSourceFile(name, false)); this.putProp(STATIC_SOURCE_FILE, new SimpleSourceFile(name, SourceKind.STRONG));
} }


// TODO(johnlenz): make this final // TODO(johnlenz): make this final
Expand Down
10 changes: 5 additions & 5 deletions src/com/google/javascript/rhino/SimpleSourceFile.java
Expand Up @@ -45,11 +45,11 @@
*/ */
public final class SimpleSourceFile implements StaticSourceFile { public final class SimpleSourceFile implements StaticSourceFile {
private final String name; private final String name;
private final boolean extern; private final SourceKind kind;


public SimpleSourceFile(String name, boolean extern) { public SimpleSourceFile(String name, SourceKind kind) {
this.name = name; this.name = name;
this.extern = extern; this.kind = kind;
} }


@Override @Override
Expand All @@ -58,8 +58,8 @@ public String getName() {
} }


@Override @Override
public boolean isExtern() { public SourceKind getKind() {
return extern; return kind;
} }


@Override @Override
Expand Down
32 changes: 28 additions & 4 deletions src/com/google/javascript/rhino/StaticSourceFile.java
Expand Up @@ -44,15 +44,39 @@
* @author nicksantos@google.com (Nick Santos) * @author nicksantos@google.com (Nick Santos)
*/ */
public interface StaticSourceFile { public interface StaticSourceFile {

/** Source kinds. */
public enum SourceKind {
/** A file whose contents are necessary both for type checking and emitting code. */
STRONG,
/** A file whose contents are necessary for type checking only. */
WEAK,
/** A file whose contents are extern declarations. */
EXTERN
}

/** /**
* The name of the file. Must be unique across all files in the compilation. * The name of the file. Must be unique across all files in the compilation.
*/ */
String getName(); String getName();


/** /** The source kind. */
* Returns whether this is an externs file. SourceKind getKind();
*/
boolean isExtern(); /** Whether the source kind is STRONG. */
default boolean isStrong() {
return getKind() == SourceKind.STRONG;
}

/** Whether the source kind is WEAK. */
default boolean isWeak() {
return getKind() == SourceKind.WEAK;
}

/** Whether the source kind is EXTERN. */
default boolean isExtern() {
return getKind() == SourceKind.EXTERN;
}


/** /**
* Returns the offset of the given line number relative to the file start. * Returns the offset of the given line number relative to the file start.
Expand Down
7 changes: 4 additions & 3 deletions test/com/google/javascript/jscomp/AstValidatorTest.java
Expand Up @@ -28,6 +28,7 @@
import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SimpleSourceFile; import com.google.javascript.rhino.SimpleSourceFile;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;


/** /**
Expand Down Expand Up @@ -139,7 +140,7 @@ public void testValidScript() {
Node n = new Node(Token.SCRIPT); Node n = new Node(Token.SCRIPT);
expectInvalid(n, Check.SCRIPT); expectInvalid(n, Check.SCRIPT);
n.setInputId(new InputId("something_input")); n.setInputId(new InputId("something_input"));
n.setStaticSourceFile(new SimpleSourceFile("something", false)); n.setStaticSourceFile(new SimpleSourceFile("something", SourceKind.STRONG));
expectValid(n, Check.SCRIPT); expectValid(n, Check.SCRIPT);
expectInvalid(n, Check.STATEMENT); expectInvalid(n, Check.STATEMENT);
expectInvalid(n, Check.EXPRESSION); expectInvalid(n, Check.EXPRESSION);
Expand Down Expand Up @@ -534,7 +535,7 @@ public void testValidFeatureInScript() {


Node n = new Node(Token.SCRIPT); Node n = new Node(Token.SCRIPT);
n.setInputId(new InputId("something_input")); n.setInputId(new InputId("something_input"));
n.setStaticSourceFile(new SimpleSourceFile("something", false)); n.setStaticSourceFile(new SimpleSourceFile("something", SourceKind.STRONG));
expectValid(n, Check.SCRIPT); expectValid(n, Check.SCRIPT);


n.addChildToFront(IR.let(IR.name("a"), IR.number(3))); n.addChildToFront(IR.let(IR.name("a"), IR.number(3)));
Expand Down Expand Up @@ -604,7 +605,7 @@ private Node parseScriptWithoutCheckingLanguageLevel(String code) {
Node script = n.getFirstChild(); Node script = n.getFirstChild();
assertNode(script).hasType(Token.SCRIPT); assertNode(script).hasType(Token.SCRIPT);
script.setInputId(new InputId("something_input")); script.setInputId(new InputId("something_input"));
script.setStaticSourceFile(new SimpleSourceFile("something", false)); script.setStaticSourceFile(new SimpleSourceFile("something", SourceKind.STRONG));
return script; return script;
} }
} }
3 changes: 2 additions & 1 deletion test/com/google/javascript/jscomp/CompilerTestCase.java
Expand Up @@ -35,6 +35,7 @@
import com.google.javascript.jscomp.type.ReverseAbstractInterpreter; import com.google.javascript.jscomp.type.ReverseAbstractInterpreter;
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter; import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import com.google.javascript.rhino.testing.BaseJSTypeTestCase; import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
Expand Down Expand Up @@ -2316,7 +2317,7 @@ protected static final class Externs implements TestPart {
externs = files; externs = files;
if (files != null) { if (files != null) {
for (SourceFile s : files) { for (SourceFile s : files) {
s.setIsExtern(true); s.setKind(SourceKind.EXTERN);
} }
} }
} }
Expand Down
10 changes: 5 additions & 5 deletions test/com/google/javascript/jscomp/parsing/AttachJsdocsTest.java
Expand Up @@ -23,6 +23,7 @@
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SimpleSourceFile; import com.google.javascript.rhino.SimpleSourceFile;
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.testing.BaseJSTypeTestCase; import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
import com.google.javascript.rhino.testing.TestErrorReporter; import com.google.javascript.rhino.testing.TestErrorReporter;
Expand Down Expand Up @@ -795,11 +796,10 @@ private Node parse(String source, String... warnings) {
null, null,
true, true,
StrictMode.SLOPPY); StrictMode.SLOPPY);
Node script = ParserRunner.parse( Node script =
new SimpleSourceFile("input", false), ParserRunner.parse(
source, new SimpleSourceFile("input", SourceKind.STRONG), source, config, testErrorReporter)
config, .ast;
testErrorReporter).ast;


// verifying that all warnings were seen // verifying that all warnings were seen
testErrorReporter.assertHasEncounteredAllErrors(); testErrorReporter.assertHasEncounteredAllErrors();
Expand Down

0 comments on commit 95f6fed

Please sign in to comment.