use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testGoogProvideNamespace_assignmentToProperty.
@Test
public void testGoogProvideNamespace_assignmentToProperty() {
GlobalNamespace namespace = parse("goog.provide('a.b'); a.b.Class = class {};");
Name abClass = namespace.getSlot("a.b.Class");
assertThat(abClass).isNotNull();
assertThat(abClass.getGlobalSets()).isEqualTo(1);
assertThat(abClass.getParent()).isEqualTo(namespace.getSlot("a.b"));
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testNestedObjectPatternAliasInDeclaration.
@Test
public void testNestedObjectPatternAliasInDeclaration() {
GlobalNamespace namespace = parse("const ns = {a: {b: 3}}; const {a: {b}} = ns;");
Name bar = namespace.getSlot("ns");
assertThat(bar.getGlobalSets()).isEqualTo(1);
assertThat(bar.getAliasingGets()).isEqualTo(1);
// we treat ns.a as having an 'aliasing' get since we don't traverse into the nested pattern
Name nsA = namespace.getSlot("ns.a");
assertThat(nsA.getGlobalSets()).isEqualTo(1);
assertThat(nsA.getAliasingGets()).isEqualTo(1);
Name nsAB = namespace.getSlot("ns.a.b");
assertThat(nsAB.getGlobalSets()).isEqualTo(1);
// we don't consider this an 'aliasing get' because it's in a nested pattern
assertThat(nsAB.getAliasingGets()).isEqualTo(0);
Name b = namespace.getSlot("b");
assertThat(b.getGlobalSets()).isEqualTo(1);
assertThat(b.getTotalGets()).isEqualTo(0);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method firstDeclarationJSDocAlwaysWins.
@Test
public void firstDeclarationJSDocAlwaysWins() {
GlobalNamespace namespace = parse(lines(//
"const X = {};", // later assignment should win
"/** @type {symbol} */", "X.number;", // this is the JSDoc we should use
"/** @type {number} */", "X.number = 3;", "/** @type {string} */", "X.number = 'hi';", "/** @type {Object} */", "X.number;", ""));
Name nameX = namespace.getOwnSlot("X.number");
Ref declarationRef = nameX.getDeclaration();
assertThat(declarationRef).isNotNull();
// make sure first assignment is considered to be the declaration
Node declarationNode = declarationRef.getNode();
assertNode(declarationNode).matchesQualifiedName("X.number");
Node assignNode = declarationNode.getParent();
assertNode(assignNode).isAssign();
Node valueNode = declarationNode.getNext();
assertNode(valueNode).isNumber(3);
// Make sure JSDoc on the first assignment is the JSDoc for the name
JSDocInfo jsDocInfo = nameX.getJSDocInfo();
assertThat(jsDocInfo).isNotNull();
JSTypeExpression jsTypeExpression = jsDocInfo.getType();
assertThat(jsTypeExpression).isNotNull();
JSType jsType = jsTypeExpression.evaluate(/* scope= */
null, lastCompiler.getTypeRegistry());
assertType(jsType).isNumber();
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method googModuleLevelNamesAreCaptured.
@Test
public void googModuleLevelNamesAreCaptured() {
GlobalNamespace namespace = parseAndGatherModuleData("goog.module('m'); const x = 0;");
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByGoogNamespace().get("m");
Name x = namespace.getNameFromModule(metadata, "x");
assertThat(x).isNotNull();
assertThat(x.getDeclaration()).isNotNull();
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method logicalAssignment1.
@Test
public void logicalAssignment1() {
GlobalNamespace namespace = parse("var a = a ||= {};");
Name a = namespace.getSlot("a");
assertThat(a).isNotNull();
assertThat(a.getRefs()).hasSize(2);
assertThat(a.getLocalSets()).isEqualTo(0);
assertThat(a.getGlobalSets()).isEqualTo(2);
}
Aggregations