Skip to content

Commit

Permalink
Remove code to reference types by path that doesn't really work. It u…
Browse files Browse the repository at this point in the history
…ses dot (.) as a delimiter from path to type, meaning you can't use this if your module resolution isn't node (since you'd need to include the .js extension). It also only works within ES6 modules, which isn't great.

#3041 is open to implement something more robust.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=208718943
  • Loading branch information
johnplaisted authored and lauraharker committed Aug 14, 2018
1 parent 1a8cca2 commit acea045
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 94 deletions.
78 changes: 23 additions & 55 deletions src/com/google/javascript/jscomp/Es6RewriteModules.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,66 +864,34 @@ public void visit(NodeTraversal t, Node n, Node parent) {
private void fixTypeNode(NodeTraversal t, Node typeNode) {
if (typeNode.isString()) {
String name = typeNode.getString();
if (ModuleLoader.isPathIdentifier(name)) {
int lastSlash = name.lastIndexOf('/');
int endIndex = name.indexOf('.', lastSlash);
String localTypeName = null;
if (endIndex == -1) {
endIndex = name.length();
List<String> splitted = Splitter.on('.').limit(2).splitToList(name);
String baseName = splitted.get(0);
String rest = "";
if (splitted.size() == 2) {
rest = "." + splitted.get(1);
}
Var var = t.getScope().getVar(baseName);
if (var != null && var.isGlobal()) {
maybeSetNewName(t, typeNode, name, baseName + "$$" + suffix + rest);
} else if (var == null && importMap.containsKey(baseName)) {
ModuleOriginalNamePair pair = importMap.get(baseName);
if (pair.originalName.isEmpty()) {
maybeSetNewName(t, typeNode, name, pair.module + rest);
} else {
localTypeName = name.substring(endIndex);
}

String moduleName = name.substring(0, endIndex);
ModuleLoader.ModulePath path =
t.getInput()
.getPath()
.resolveJsModule(
moduleName,
typeNode.getSourceFileName(),
typeNode.getLineno(),
typeNode.getCharno());

if (path == null) {
path = t.getInput().getPath().resolveModuleAsPath(moduleName);
}
String globalModuleName = path.toModuleName();

maybeSetNewName(
t,
typeNode,
name,
localTypeName == null ? globalModuleName : globalModuleName + localTypeName);
} else {
List<String> splitted = Splitter.on('.').limit(2).splitToList(name);
String baseName = splitted.get(0);
String rest = "";
if (splitted.size() == 2) {
rest = "." + splitted.get(1);
maybeSetNewName(t, typeNode, name, baseName + "$$" + pair.module + rest);
}
Var var = t.getScope().getVar(baseName);
if (var != null && var.isGlobal()) {
maybeSetNewName(t, typeNode, name, baseName + "$$" + suffix + rest);
} else if (var == null && importMap.containsKey(baseName)) {
ModuleOriginalNamePair pair = importMap.get(baseName);
if (pair.originalName.isEmpty()) {
maybeSetNewName(t, typeNode, name, pair.module + rest);
} else {
maybeSetNewName(t, typeNode, name, baseName + "$$" + pair.module + rest);
}

if (preprocessorSymbolTable != null) {
// Jsdoc type node is a single STRING node that spans the whole type. For example
// STRING node "bar.Foo". ES6 import rewrite replaces only "module"
// part of the type: "bar.Foo" => "module$full$path$bar$Foo". We have to record
// "bar" as alias.
Node onlyBaseName = Node.newString(baseName).useSourceInfoFrom(typeNode);
onlyBaseName.setLength(baseName.length());
maybeAddAliasToSymbolTable(onlyBaseName, t.getSourceName());
}
if (preprocessorSymbolTable != null) {
// Jsdoc type node is a single STRING node that spans the whole type. For example
// STRING node "bar.Foo". ES6 import rewrite replaces only "module"
// part of the type: "bar.Foo" => "module$full$path$bar$Foo". We have to record
// "bar" as alias.
Node onlyBaseName = Node.newString(baseName).useSourceInfoFrom(typeNode);
onlyBaseName.setLength(baseName.length());
maybeAddAliasToSymbolTable(onlyBaseName, t.getSourceName());
}
typeNode.setOriginalName(name);
}
typeNode.setOriginalName(name);
}

for (Node child = typeNode.getFirstChild(); child != null; child = child.getNext()) {
Expand Down
39 changes: 0 additions & 39 deletions test/com/google/javascript/jscomp/Es6RewriteModulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -653,45 +653,6 @@ public void testFixTypeNode() {
"/** @const */ module$testcode.Child = Child$$module$testcode;"));
}

public void testReferenceToTypeFromOtherModule() {
setModuleResolutionMode(ModuleLoader.ResolutionMode.NODE);
testModules(
lines(
"export class Foo {", " /** @param {./other.Baz} baz */", " useBaz(baz) {}", "}"),
lines(
"class Foo$$module$testcode {",
" /** @param {module$other.Baz} baz */",
" useBaz(baz) {}",
"}",
"/** @const */ var module$testcode = {};",
"/** @const */ module$testcode.Foo = Foo$$module$testcode;"));

testModules(
lines(
"export class Foo {", " /** @param {/other.Baz} baz */", " useBaz(baz) {}", "}"),
lines(
"class Foo$$module$testcode {",
" /** @param {module$other.Baz} baz */",
" useBaz(baz) {}",
"}",
"/** @const */ var module$testcode = {};",
"/** @const */ module$testcode.Foo = Foo$$module$testcode;"));

testModules(
lines(
"import {Parent} from './other.js';",
"class Child extends Parent {",
" /** @param {./other.Parent} parent */",
" useParent(parent) {}",
"}"),
lines(
"class Child$$module$testcode extends module$other.Parent {",
" /** @param {module$other.Parent} parent */",
" useParent(parent) {}",
"}",
"/** @const */ var module$testcode = {};"));
}

public void testRenameTypedef() {
testModules(
lines(
Expand Down

0 comments on commit acea045

Please sign in to comment.