示例#1
0
 private void addExtern() {
   Node name = IR.name(PROTECTOR_FN);
   name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
   Node var = IR.var(name);
   // Add "@noalias" so we can strip the method when AliasExternals is enabled.
   JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
   builder.recordNoAlias();
   var.setJSDocInfo(builder.build(var));
   CompilerInput input = compiler.getSynthesizedExternsInput();
   input.getAstRoot(compiler).addChildrenToBack(var);
   compiler.reportCodeChange();
 }
 /** @param inputs The ordered list of all inputs for the compiler. */
 GlobalVarReferenceMap(List<CompilerInput> inputs, List<CompilerInput> externs) {
   inputOrder = new HashMap<>();
   int ind = 0;
   for (CompilerInput extern : externs) {
     inputOrder.put(extern.getInputId(), ind);
     ind++;
   }
   for (CompilerInput input : inputs) {
     inputOrder.put(input.getInputId(), ind);
     ind++;
   }
 }
 /**
  * Verifies that the compiler pass's JS output is the same as the input.
  *
  * @param modules Module inputs
  * @param warning A warning, or null for no expected warning.
  */
 public void testSame(JSModule[] modules, DiagnosticType warning) {
   try {
     String[] expected = new String[modules.length];
     for (int i = 0; i < modules.length; i++) {
       expected[i] = "";
       for (CompilerInput input : modules[i].getInputs()) {
         expected[i] += input.getSourceFile().getCode();
       }
     }
     test(modules, expected, null, warning);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
 private void addInstaller(Node sourceNode, String function) {
   // Find the module
   InputId inputId = sourceNode.getInputId();
   CompilerInput input = inputId != null ? compiler.getInput(inputId) : null;
   JSModule module = input != null ? input.getModule() : null;
   InjectedInstaller injected = new InjectedInstaller(module, function);
   if (installers.add(injected)) {
     changed = true;
     Node installer = compiler.parseSyntheticCode(function).removeChildren();
     installer.useSourceInfoIfMissingFromForTree(sourceNode);
     Node enclosingScript = NodeUtil.getEnclosingScript(sourceNode);
     enclosingScript.addChildrenToFront(installer);
   }
 }
  private void assertSortedInputs(List<SourceFile> expected, List<SourceFile> shuffled)
      throws Exception {
    Compiler compiler = new Compiler(System.err);
    compiler.initCompilerOptionsIfTesting();
    compiler.getOptions().setProcessCommonJSModules(true);
    compiler
        .getOptions()
        .dependencyOptions
        .setEntryPoints(ImmutableList.of(ES6ModuleLoader.toModuleName(URI.create("a"))));
    compiler.compile(
        ImmutableList.of(SourceFile.fromCode("externs.js", "")), shuffled, compiler.getOptions());

    List<SourceFile> result = new ArrayList<>();
    for (JSModule m : compiler.getModuleGraph().getAllModules()) {
      for (CompilerInput i : m.getInputs()) {
        result.add(i.getSourceFile());
      }
    }

    assertEquals(expected, result);
  }
  public void testExternsLifting1() throws Exception {
    String code = "/** @externs */ function f() {}";
    test(new String[] {code}, new String[] {});

    assertEquals(2, lastCompiler.getExternsForTesting().size());

    CompilerInput extern = lastCompiler.getExternsForTesting().get(1);
    assertNull(extern.getModule());
    assertTrue(extern.isExtern());
    assertEquals(code, extern.getCode());

    assertEquals(1, lastCompiler.getInputsForTesting().size());

    CompilerInput input = lastCompiler.getInputsForTesting().get(0);
    assertNotNull(input.getModule());
    assertFalse(input.isExtern());
    assertEquals("", input.getCode());
  }
 /**
  * Creates a variable reference in a given script file name, used in tests.
  *
  * @return The created reference.
  */
 @VisibleForTesting
 static Reference createRefForTest(CompilerInput input) {
   return new Reference(new Node(Token.NAME), null, null, input.getInputId());
 }
 JSModule getRefModule(ReferenceCollectingCallback.Reference ref) {
   CompilerInput input = compiler.getInput(ref.getInputId());
   return input == null ? null : input.getModule();
 }
 /** Gets the current input module. */
 public JSModule getModule() {
   CompilerInput input = getInput();
   return input == null ? null : input.getModule();
 }