Java Code Examples for com.google.javascript.rhino.Node#getDirectives()

The following examples show how to use com.google.javascript.rhino.Node#getDirectives() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Reader.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void transformPrologueDecl(JsonML element, Node parent)
    throws JsonMLException {
  String directive = getStringAttribute(element, TagAttr.DIRECTIVE);

  if (ALLOWED_DIRECTIVES.contains(directive)) {
    Set<String> directives = parent.getDirectives();
    if (directives == null) {
      directives = Sets.newHashSet();
    }
    directives.add(directive);
    parent.setDirectives(directives);
  } else {
    // for a directive which is not supported, we create a regular node
    Node node = IR.exprResult(IR.string(directive));
    parent.addChildToBack(node);
  }
}
 
Example 2
Source File: Writer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void processDirectives(Node node, JsonML currentParent) {
  Set<String> directives = node.getDirectives();

  if (directives == null) {
    return;
  }

  for (String directive : directives) {
    JsonML element = new JsonML(TagType.PrologueDecl);
    element.setAttribute(TagAttr.DIRECTIVE, directive);
    element.setAttribute(TagAttr.VALUE, directive);
    currentParent.appendChild(element);
  }
}
 
Example 3
Source File: NodeModulePass.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private void visitScript(NodeTraversal t, Node script) {
  if (currentModule == null) {
    return;
  }

  // Remove any 'use strict' directives. The compiler adds these by default to
  // closure modules and will generate a warning if specified directly.
  // TODO: remove when https://github.com/google/closure-compiler/issues/1263 is fixed.
  Set<String> directives = script.getDirectives();
  if (directives != null && directives.contains("use strict")) {
    // Directives is likely an immutable collection, so we need to make a copy.
    Set<String> newDirectives = new HashSet<>(directives);
    newDirectives.remove("use strict");
    script.setDirectives(newDirectives);
  }

  processModuleExportRefs(t);

  Node moduleBody = createModuleBody();
  moduleBody.srcrefTree(script);
  if (script.getChildCount() > 0) {
    moduleBody.addChildrenToBack(script.removeChildren());
  }
  script.addChildToBack(moduleBody);
  script.putBooleanProp(Node.GOOG_MODULE, true);

  t.getInput().addProvide(currentModule);

  traverse(t.getCompiler(), script, new TypeCleanup());

  googRequireExpr.clear();
  currentModule = null;

  t.reportCodeChange();
}