Java Code Examples for com.google.javascript.rhino.Token#IMPORT

The following examples show how to use com.google.javascript.rhino.Token#IMPORT . 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: ModuleConversionPass.java    From clutz with MIT License 6 votes vote down vote up
private void convertRequireForAlreadyConverted(ModuleImport moduleImport) {
  // we cannot use referencedFile here, because usually it points to the ES5 js file that is
  // the output of TS, and not the original source TS file.
  // However, we can reverse map the goog.module name to a file name.
  // TODO(rado): sync this better with the mapping done in tsickle.
  String originalPath =
      moduleImport.requiredNamespace.replace(alreadyConvertedPrefix + ".", "").replace(".", "/");
  String sourceFileName = moduleImport.originalImportNode.getSourceFileName();
  String referencedFile = pathUtil.getImportPath(sourceFileName, originalPath);
  // goog.require('...'); -> import '...';
  Node importSpec = IR.empty();
  if (moduleImport.isDestructuringImport) {
    importSpec = createNamedImports(moduleImport);
  } else if (moduleImport.isFullModuleImport()) {
    // It is safe to assume there's one full local name because this is validated before.
    String fullLocalName = moduleImport.fullLocalNames.get(0);
    importSpec = Node.newString(Token.IMPORT_STAR, fullLocalName);
  }
  Node importNode =
      new Node(Token.IMPORT, IR.empty(), importSpec, Node.newString(referencedFile));
  nodeComments.replaceWithComment(moduleImport.originalImportNode, importNode);
  compiler.reportChangeToEnclosingScope(importNode);
}
 
Example 2
Source File: ModuleConversionPass.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Converts a destructuring Closure goog.require call into a TypeScript import statement.
 *
 * <p>The resulting node is dependent on the exports by the module being imported:
 *
 * <pre>
 *   import {A as localName, B} from "./valueExports";
 * </pre>
 */
private void convertDestructuringRequireToImportStatements(Node n, ModuleImport moduleImport) {
  // The imported file is already in TS
  if (moduleImport.isAlreadyConverted()) {
    convertRequireForAlreadyConverted(moduleImport);
    return;
  }

  // The imported file is kept in JS
  if (moduleImport.module.shouldUseOldSyntax()) {
    convertRequireToImportsIfImportedIsKeptInJs(moduleImport);
    return;
  }
  // For the rest of the function, the imported and importing files are migrating together

  // import {localName} from "./file"
  Node importSpecs = createNamedImports(moduleImport);
  Node importNode =
      new Node(
          Token.IMPORT, IR.empty(), importSpecs, Node.newString(moduleImport.referencedFile));
  addImportNode(n, importNode);

  for (int i = 0; i < moduleImport.fullLocalNames.size(); i++) {
    registerLocalSymbol(
        n.getSourceFileName(),
        moduleImport.fullLocalNames.get(i),
        moduleImport.requiredNamespace,
        moduleImport.localNames.get(i));
  }

  compiler.reportChangeToEnclosingScope(n);
  n.detach();
}
 
Example 3
Source File: ModuleConversionPass.java    From clutz with MIT License 4 votes vote down vote up
private void convertRequireForSideEffectOnlyImport(ModuleImport moduleImport) {
  Node importNode =
      new Node(Token.IMPORT, IR.empty(), IR.empty(), Node.newString(moduleImport.referencedFile));
  addImportNode(moduleImport.originalImportNode, importNode);
}