Skip to content

Commit

Permalink
Rename {Js,Deps}FileParser as {Js,Deps}FileRegexParser.
Browse files Browse the repository at this point in the history
We have two JavaScript dependency parsers: JsFileParser (regex-based) and JsfileParser (non-regex-based). This CL renames the first so that the two aren't as easily confused.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=259063514
  • Loading branch information
tjgq authored and lauraharker committed Jul 22, 2019
1 parent 6a418aa commit fd85b3e
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -38,7 +38,7 @@
import com.google.javascript.jscomp.SortingErrorManager.ErrorReportGenerator; import com.google.javascript.jscomp.SortingErrorManager.ErrorReportGenerator;
import com.google.javascript.jscomp.deps.BrowserModuleResolver; import com.google.javascript.jscomp.deps.BrowserModuleResolver;
import com.google.javascript.jscomp.deps.BrowserWithTransformedPrefixesModuleResolver; import com.google.javascript.jscomp.deps.BrowserWithTransformedPrefixesModuleResolver;
import com.google.javascript.jscomp.deps.JsFileParser; import com.google.javascript.jscomp.deps.JsFileRegexParser;
import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.deps.ModuleLoader.ModuleResolverFactory; import com.google.javascript.jscomp.deps.ModuleLoader.ModuleResolverFactory;
import com.google.javascript.jscomp.deps.NodeModuleResolver; import com.google.javascript.jscomp.deps.NodeModuleResolver;
Expand Down Expand Up @@ -2025,7 +2025,7 @@ private List<CompilerInput> parsePotentialModules(Iterable<CompilerInput> inputs
for (CompilerInput input : inputsToProcess) { for (CompilerInput input : inputsToProcess) {
// Only process files that are detected as ES6 modules // Only process files that are detected as ES6 modules
if (!options.getDependencyOptions().shouldPrune() if (!options.getDependencyOptions().shouldPrune()
|| !JsFileParser.isSupported() || !JsFileRegexParser.isSupported()
|| "es6".equals(input.getLoadFlags().get("module"))) { || "es6".equals(input.getLoadFlags().get("module"))) {
filteredInputs.add(input); filteredInputs.add(input);
} }
Expand Down
10 changes: 5 additions & 5 deletions src/com/google/javascript/jscomp/CompilerInput.java
Expand Up @@ -26,7 +26,7 @@
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.deps.DependencyInfo; import com.google.javascript.jscomp.deps.DependencyInfo;
import com.google.javascript.jscomp.deps.JsFileParser; import com.google.javascript.jscomp.deps.JsFileRegexParser;
import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.deps.ModuleLoader.ModulePath; import com.google.javascript.jscomp.deps.ModuleLoader.ModulePath;
import com.google.javascript.jscomp.deps.SimpleDependencyInfo; import com.google.javascript.jscomp.deps.SimpleDependencyInfo;
Expand Down Expand Up @@ -301,16 +301,16 @@ private DependencyInfo generateDependencyInfo() {
compiler.getErrorManager(), "Expected compiler to call an error manager: %s", this); compiler.getErrorManager(), "Expected compiler to call an error manager: %s", this);


// If the code is a JsAst, then it was originally JS code, and is compatible with the // If the code is a JsAst, then it was originally JS code, and is compatible with the
// regex-based parsing of JsFileParser. // regex-based parsing of JsFileRegexParser.
if (ast instanceof JsAst && JsFileParser.isSupported()) { if (ast instanceof JsAst && JsFileRegexParser.isSupported()) {
// Look at the source code. // Look at the source code.
// Note: it's OK to use getName() instead of // Note: it's OK to use getName() instead of
// getPathRelativeToClosureBase() here because we're not using // getPathRelativeToClosureBase() here because we're not using
// this to generate deps files. (We're only using it for // this to generate deps files. (We're only using it for
// symbol dependencies.) // symbol dependencies.)
try { try {
DependencyInfo info = DependencyInfo info =
new JsFileParser(compiler.getErrorManager()) new JsFileRegexParser(compiler.getErrorManager())
.setModuleLoader(compiler.getModuleLoader()) .setModuleLoader(compiler.getModuleLoader())
.setIncludeGoogBase(true) .setIncludeGoogBase(true)
.parseFile(getName(), getName(), getCode()); .parseFile(getName(), getName(), getCode());
Expand Down Expand Up @@ -483,7 +483,7 @@ void visitEs6ModuleName(Node n, Node parent) {
checkArgument(n.isString()); checkArgument(n.isString());
checkArgument(parent.isExport() || parent.isImport()); checkArgument(parent.isExport() || parent.isImport());


// TODO(blickly): Move this (and the duplicated logic in JsFileParser/Es6RewriteModules) // TODO(blickly): Move this (and the duplicated logic in JsFileRegexParser/Es6RewriteModules)
// into ModuleLoader. // into ModuleLoader.
String moduleName = n.getString(); String moduleName = n.getString();
if (moduleName.startsWith("goog:")) { if (moduleName.startsWith("goog:")) {
Expand Down
Expand Up @@ -141,7 +141,7 @@ private void addDependency(String symbol, Set<String> seen, List<String> list)
/** Parses a block of code for goog.require statements and extracts the required symbols. */ /** Parses a block of code for goog.require statements and extracts the required symbols. */
private static Collection<String> parseRequires(String code, boolean addClosureBase) { private static Collection<String> parseRequires(String code, boolean addClosureBase) {
ErrorManager errorManager = new LoggerErrorManager(logger); ErrorManager errorManager = new LoggerErrorManager(logger);
JsFileParser parser = new JsFileParser(errorManager); JsFileRegexParser parser = new JsFileRegexParser(errorManager);
DependencyInfo deps = DependencyInfo deps =
parser.parseFile("<unknown path>", "<unknown path>", code); parser.parseFile("<unknown path>", "<unknown path>", code);
List<String> requires = new ArrayList<>(); List<String> requires = new ArrayList<>();
Expand Down
3 changes: 1 addition & 2 deletions src/com/google/javascript/jscomp/deps/DependencyFile.java
Expand Up @@ -89,8 +89,7 @@ private void loadGraph() throws ServiceException {


// Parse the deps.js file. // Parse the deps.js file.
ErrorManager errorManager = new LoggerErrorManager(logger); ErrorManager errorManager = new LoggerErrorManager(logger);
DepsFileParser parser = DepsFileRegexParser parser = new DepsFileRegexParser(errorManager);
new DepsFileParser(errorManager);
List<DependencyInfo> depInfos = List<DependencyInfo> depInfos =
parser.parseFile(getName(), getContent()); parser.parseFile(getName(), getContent());


Expand Down
Expand Up @@ -42,13 +42,13 @@
/** /**
* A parser that can extract dependency information from existing deps.js files. * A parser that can extract dependency information from existing deps.js files.
* *
* <p>See //javascript/closure/deps.js for an example file.</p> * <p>See //javascript/closure/deps.js for an example file.
* *
* @author agrieve@google.com (Andrew Grieve) * @author agrieve@google.com (Andrew Grieve)
*/ */
@GwtIncompatible("java.util.regex") @GwtIncompatible("java.util.regex")
public final class DepsFileParser extends JsFileLineParser { public final class DepsFileRegexParser extends JsFileLineParser {
private static final Logger logger = Logger.getLogger(DepsFileParser.class.getName()); private static final Logger logger = Logger.getLogger(DepsFileRegexParser.class.getName());


/** /**
* Pattern for matching JavaScript string literals. The group is: * Pattern for matching JavaScript string literals. The group is:
Expand Down Expand Up @@ -80,16 +80,15 @@ public final class DepsFileParser extends JsFileLineParser {
* *
* @param errorManager Handles parse errors. * @param errorManager Handles parse errors.
*/ */
public DepsFileParser(ErrorManager errorManager) { public DepsFileRegexParser(ErrorManager errorManager) {
this(Functions.identity(), errorManager); this(Functions.identity(), errorManager);
} }


/** /**
* @param pathTranslator Translates paths in different build systems. * @param pathTranslator Translates paths in different build systems.
* @param errorManager Handles parse errors. * @param errorManager Handles parse errors.
*/ */
public DepsFileParser(Function<String, String> pathTranslator, public DepsFileRegexParser(Function<String, String> pathTranslator, ErrorManager errorManager) {
ErrorManager errorManager) {
super(errorManager); super(errorManager);
this.pathTranslator = pathTranslator; this.pathTranslator = pathTranslator;
} }
Expand Down
10 changes: 5 additions & 5 deletions src/com/google/javascript/jscomp/deps/DepsGenerator.java
Expand Up @@ -403,8 +403,8 @@ private void addToProvideMap(
} }
} }


protected DepsFileParser createDepsFileParser() { protected DepsFileRegexParser createDepsFileParser() {
DepsFileParser depsParser = new DepsFileParser(errorManager); DepsFileRegexParser depsParser = new DepsFileRegexParser(errorManager);
depsParser.setShortcutMode(true); depsParser.setShortcutMode(true);
return depsParser; return depsParser;
} }
Expand All @@ -421,7 +421,7 @@ protected boolean shouldSkipDepsFile(SourceFile file) {
* closure-relative path -> DependencyInfo. * closure-relative path -> DependencyInfo.
*/ */
private Map<String, DependencyInfo> parseDepsFiles() throws IOException { private Map<String, DependencyInfo> parseDepsFiles() throws IOException {
DepsFileParser depsParser = createDepsFileParser(); DepsFileRegexParser depsParser = createDepsFileParser();
Map<String, DependencyInfo> depsFiles = new LinkedHashMap<>(); Map<String, DependencyInfo> depsFiles = new LinkedHashMap<>();
for (SourceFile file : deps) { for (SourceFile file : deps) {
if (!shouldSkipDepsFile(file)) { if (!shouldSkipDepsFile(file)) {
Expand Down Expand Up @@ -455,7 +455,7 @@ private Map<String, DependencyInfo> parseDepsFiles() throws IOException {
} }


private DependencyInfo removeRelativePathProvide(DependencyInfo info) { private DependencyInfo removeRelativePathProvide(DependencyInfo info) {
// DepsFileParser adds an ES6 module's relative path to closure as a provide so that // DepsFileRegexParser adds an ES6 module's relative path to closure as a provide so that
// the resulting depgraph is valid. But we don't want to write this "fake" provide // the resulting depgraph is valid. But we don't want to write this "fake" provide
// back out, so remove it here. // back out, so remove it here.
return SimpleDependencyInfo.Builder.from(info) return SimpleDependencyInfo.Builder.from(info)
Expand All @@ -477,7 +477,7 @@ private DependencyInfo removeRelativePathProvide(DependencyInfo info) {
private Map<String, DependencyInfo> parseSources( private Map<String, DependencyInfo> parseSources(
Set<String> preparsedFiles) throws IOException { Set<String> preparsedFiles) throws IOException {
Map<String, DependencyInfo> parsedFiles = new LinkedHashMap<>(); Map<String, DependencyInfo> parsedFiles = new LinkedHashMap<>();
JsFileParser jsParser = new JsFileParser(errorManager).setModuleLoader(loader); JsFileRegexParser jsParser = new JsFileRegexParser(errorManager).setModuleLoader(loader);
Compiler compiler = new Compiler(); Compiler compiler = new Compiler();
compiler.init(ImmutableList.of(), ImmutableList.of(), new CompilerOptions()); compiler.init(ImmutableList.of(), ImmutableList.of(), new CompilerOptions());


Expand Down
Expand Up @@ -36,15 +36,15 @@
import java.util.regex.Pattern; import java.util.regex.Pattern;


/** /**
* A parser that can extract dependency information from a .js file, including * A parser that can extract dependency information from a .js file, including goog.require,
* goog.require, goog.provide, goog.module, import statements, and export statements. * goog.provide, goog.module, import statements, and export statements.
* *
* @author agrieve@google.com (Andrew Grieve) * @author agrieve@google.com (Andrew Grieve)
*/ */
@GwtIncompatible("java.util.regex") @GwtIncompatible("java.util.regex")
public final class JsFileParser extends JsFileLineParser { public final class JsFileRegexParser extends JsFileLineParser {


private static final Logger logger = Logger.getLogger(JsFileParser.class.getName()); private static final Logger logger = Logger.getLogger(JsFileRegexParser.class.getName());


/** Pattern for matching goog.provide(*) and goog.require(*). */ /** Pattern for matching goog.provide(*) and goog.require(*). */
private static final Pattern GOOG_PROVIDE_REQUIRE_PATTERN = private static final Pattern GOOG_PROVIDE_REQUIRE_PATTERN =
Expand Down Expand Up @@ -133,36 +133,33 @@ private enum ModuleType {
* *
* @param errorManager Handles parse errors. * @param errorManager Handles parse errors.
*/ */
public JsFileParser(ErrorManager errorManager) { public JsFileRegexParser(ErrorManager errorManager) {
super(errorManager); super(errorManager);
} }


/** /**
* Sets whether we should create implicit provides and requires of the * Sets whether we should create implicit provides and requires of the root namespace.
* root namespace.
* *
* When generating deps files, you do not want this behavior. Deps files * <p>When generating deps files, you do not want this behavior. Deps files need base.js to run
* need base.js to run anyway, so they don't need information about it. * anyway, so they don't need information about it.
* *
* When generating abstract build graphs, you probably do want this behavior. * <p>When generating abstract build graphs, you probably do want this behavior. It will create an
* It will create an implicit dependency of all files with provides/requires * implicit dependency of all files with provides/requires on base.js.
* on base.js.
* *
* @return this for easy chaining. * @return this for easy chaining.
*/ */
public JsFileParser setIncludeGoogBase(boolean include) { public JsFileRegexParser setIncludeGoogBase(boolean include) {
checkState(JsFileParser.isSupported()); checkState(JsFileRegexParser.isSupported());
includeGoogBase = include; includeGoogBase = include;
return this; return this;
} }


/** /**
* Sets a list of "module root" URIs, which allow relativizing filenames * Sets a list of "module root" URIs, which allow relativizing filenames for modules.
* for modules.
* *
* @return this for easy chaining. * @return this for easy chaining.
*/ */
public JsFileParser setModuleLoader(ModuleLoader loader) { public JsFileRegexParser setModuleLoader(ModuleLoader loader) {
this.loader = loader; this.loader = loader;
return this; return this;
} }
Expand Down
Expand Up @@ -18,22 +18,21 @@


import com.google.javascript.jscomp.ErrorManager; import com.google.javascript.jscomp.ErrorManager;


/** GWT compatible no-op replacement for {@code JsFileParser} */ /** GWT compatible no-op replacement for {@code JsFileRegexParser} */
public final class JsFileParser { public final class JsFileRegexParser {
public JsFileParser(ErrorManager errorManager) { public JsFileRegexParser(ErrorManager errorManager) {}
}


public JsFileParser setModuleLoader(ModuleLoader loader) { public JsFileRegexParser setModuleLoader(ModuleLoader loader) {
throw new UnsupportedOperationException("JsFileParser.setModuleLoader not implemented"); throw new UnsupportedOperationException("JsFileRegexParser.setModuleLoader not implemented");
} }


public JsFileParser setIncludeGoogBase(boolean include) { public JsFileRegexParser setIncludeGoogBase(boolean include) {
throw new UnsupportedOperationException("JsFileParser.setIncludeGoogBase not implemented"); throw new UnsupportedOperationException("JsFileRegexParser.setIncludeGoogBase not implemented");
} }


public DependencyInfo parseFile(String filePath, String closureRelativePath, public DependencyInfo parseFile(String filePath, String closureRelativePath,
String fileContents) { String fileContents) {
throw new UnsupportedOperationException("JsFileParser.parseFile not implemented"); throw new UnsupportedOperationException("JsFileRegexParser.parseFile not implemented");
} }


public static boolean isSupported() { public static boolean isSupported() {
Expand Down
Expand Up @@ -37,7 +37,8 @@


@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public final class RewriteGoogJsImportsTest extends CompilerTestCase { public final class RewriteGoogJsImportsTest extends CompilerTestCase {
// JsFileParser determines if this file is base.js by looking at the first comment of the file. // JsFileRegexParser determines if this file is base.js by looking at the first comment of the
// file.
private static final SourceFile BASE = private static final SourceFile BASE =
SourceFile.fromCode("/closure/base.js", "/** @provideGoog */"); SourceFile.fromCode("/closure/base.js", "/** @provideGoog */");


Expand Down
Expand Up @@ -30,21 +30,21 @@
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;


/** /**
* Tests for {@link DepsFileParser}. * Tests for {@link DepsFileRegexParser}.
* *
* @author agrieve@google.com (Andrew Grieve) * @author agrieve@google.com (Andrew Grieve)
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public final class DepsFileParserTest { public final class DepsFileRegexParserTest {


private DepsFileParser parser; private DepsFileRegexParser parser;
private ErrorManager errorManager; private ErrorManager errorManager;
private static final String SRC_PATH = "/path/1.js"; private static final String SRC_PATH = "/path/1.js";


@Before @Before
public void setUp() { public void setUp() {
errorManager = new PrintStreamErrorManager(System.err); errorManager = new PrintStreamErrorManager(System.err);
parser = new DepsFileParser(errorManager); parser = new DepsFileRegexParser(errorManager);
parser.setShortcutMode(true); parser.setShortcutMode(true);
} }


Expand Down
Expand Up @@ -32,14 +32,14 @@
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;


/** /**
* Tests for {@link JsFileParser}. * Tests for {@link JsFileRegexParser}.
* *
* @author agrieve@google.com (Andrew Grieve) * @author agrieve@google.com (Andrew Grieve)
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public final class JsFileParserTest { public final class JsFileRegexParserTest {


JsFileParser parser; JsFileRegexParser parser;
private ErrorManager errorManager; private ErrorManager errorManager;


private static final String SRC_PATH = "a"; private static final String SRC_PATH = "a";
Expand All @@ -48,7 +48,7 @@ public final class JsFileParserTest {
@Before @Before
public void setUp() { public void setUp() {
errorManager = new PrintStreamErrorManager(System.err); errorManager = new PrintStreamErrorManager(System.err);
parser = new JsFileParser(errorManager); parser = new JsFileRegexParser(errorManager);
parser.setShortcutMode(true); parser.setShortcutMode(true);
} }


Expand Down

0 comments on commit fd85b3e

Please sign in to comment.