Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/com/google/javascript/jscomp/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.google.javascript.jscomp.CompilerOptions.DevMode;
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection;
import com.google.javascript.jscomp.TypeValidator.TypeMismatch;
import com.google.javascript.jscomp.deps.DependencyInfo;
import com.google.javascript.jscomp.deps.JsFileParser;
import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.deps.SortedDependencies.MissingProvideException;
import com.google.javascript.jscomp.parsing.Config;
Expand Down Expand Up @@ -1466,6 +1468,10 @@ Node parseInputs() {

this.moduleLoader = new ModuleLoader(this, options.moduleRoots, inputs);

if (options.processCommonJSModules) {
this.moduleLoader.setPackageJsonMainEntries(processJsonInputs(inputs));
}

if (options.lowerFromEs6()) {
processEs6Modules();
}
Expand Down Expand Up @@ -1677,6 +1683,34 @@ private void repartitionInputs() {
rebuildInputsFromModules();
}

/**
* Transforms JSON files to a module export that closure compiler can
* process and keeps track of any "main" entries in package.json files.
*/
Map<String, String> processJsonInputs(List<CompilerInput> inputsToProcess) {
RewriteJsonToModule rewriteJson = new RewriteJsonToModule(this);
for (CompilerInput input : inputsToProcess) {
if (!input.getSourceFile().getOriginalPath().endsWith(".json")) {
continue;
}

input.setCompiler(this);

try {
// JSON objects need wrapped in parens to parse properly
input.getSourceFile().setCode("(" + input.getSourceFile().getCode() + ")");
} catch (IOException e) {
this.getErrorManager().report(CheckLevel.ERROR,
JSError.make(AbstractCompiler.READ_ERROR, input.getSourceFile().getOriginalPath()));
continue;
}

Node root = input.getAstRoot(this);
rewriteJson.process(null, root);
}
return rewriteJson.getPackageJsonMainEntries();
}

void processEs6Modules() {
processEs6Modules(inputs, false);
}
Expand Down
150 changes: 150 additions & 0 deletions src/com/google/javascript/jscomp/RewriteJsonToModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright 2016 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import java.util.HashMap;
import java.util.Map;

/**
* Rewrites a JSON file to be a module export. So that the JSON file
* parses correctly, it is wrapped in an EXPR_RESULT. The pass makes
* only basic checks that the file provided is valid JSON. It is
* not a full JSON validator.
*
* Looks for JSON files named "package.json" so that
* the "main" property can be used as an alias in module
* name resolution.
*/
public class RewriteJsonToModule extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass {
public static final DiagnosticType JSON_UNEXPECTED_TOKEN = DiagnosticType.error(
"JSC_JSON_UNEXPECTED_TOKEN",
"Unexpected JSON token");

private final Map<String, String> packageJsonMainEntries;
private final Compiler compiler;

/**
* Creates a new RewriteJsonToModule instance which can be used to
* rewrite JSON files to modules.
*
* @param compiler The compiler
*/
public RewriteJsonToModule(Compiler compiler) {
this.compiler = compiler;
this.packageJsonMainEntries = new HashMap<>();
}

public ImmutableMap<String, String> getPackageJsonMainEntries() {
return ImmutableMap.copyOf(packageJsonMainEntries);
}

/**
* Module rewriting is done a on per-file basis prior to main compilation.
* The root node for each file is a SCRIPT - not the typical jsRoot of other passes.
*/
@Override
public void process(Node externs, Node root) {
Preconditions.checkState(root.isScript());
NodeTraversal.traverseEs6(compiler, root, this);
}

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getToken()) {
case SCRIPT:
if (n.getChildCount() != 1) {
compiler.report(t.makeError(n, JSON_UNEXPECTED_TOKEN));
} else {
visitScript(t, n, parent);
}
return;

case OBJECTLIT:
case ARRAYLIT:
case NUMBER:
case TRUE:
case FALSE:
case NULL:
case STRING:
break;

case STRING_KEY:
if (!n.isQuotedString() || n.getChildCount() != 1) {
compiler.report(t.makeError(n, JSON_UNEXPECTED_TOKEN));
}
break;

case EXPR_RESULT:
if (!parent.isScript()) {
compiler.report(t.makeError(n, JSON_UNEXPECTED_TOKEN));
}
break;

default:
compiler.report(t.makeError(n, JSON_UNEXPECTED_TOKEN));
break;
}

if (n.getLineno() == 1) {
// We wrapped the expression in parens so our first-line columns are off by one.
// We need to correct for this.
n.setCharno(n.getCharno() - 1);
compiler.reportCodeChange();
}
}

/**
* For script nodes of JSON objects, add a module variable assignment
* so the result is exported.
*
* If the file path ends with "/package.json", look for a "main"
* key in the object literal and track it as a module alias.
*/
private void visitScript(NodeTraversal t, Node n, Node parent) {
if (n.getChildCount() != 1 || !n.getFirstChild().isExprResult()) {
compiler.report(t.makeError(n, JSON_UNEXPECTED_TOKEN));
return;
}

Node jsonObject = n.getFirstFirstChild().detach();
n.removeFirstChild();

String moduleName = t.getInput().getPath().toModuleName();

n.addChildToFront(IR.var(IR.name(moduleName).useSourceInfoFrom(jsonObject),
jsonObject).useSourceInfoFrom(jsonObject));

n.addChildToFront(
IR.exprResult(
IR.call(IR.getprop(IR.name("goog"), IR.string("provide")), IR.string(moduleName)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for use with Node.js, right?
Shouldn't we be rewriting this to look like a non-JSON Node.js module then, rather than a goog.provide()?
Perhaps Node.js modules get rewritten to look like goog.provide() anyway?
Even if that's so, isn't it safer to rewrite this as a Node.js module & allow the later pass to rewrite it again as a goog.provide(). Otherwise, we're duplicating the logic here & risking inconsistent behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed this yesterday & agreed to leave it as-is.

.useSourceInfoIfMissingFromForTree(n));

String inputPath = t.getInput().getSourceFile().getOriginalPath();
if (inputPath.endsWith("/package.json") && jsonObject.isObjectLit()) {
Node main = NodeUtil.getFirstPropMatchingKey(jsonObject, "main");
if (main != null && main.isString()) {
String dirName = inputPath.substring(0, inputPath.length() - "package.json".length());
packageJsonMainEntries.put(inputPath, dirName + main.getString());
}
}

compiler.reportCodeChange();
}
}
Loading