use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class DisambiguateProperties method invalidateNonDeclaredPropertyAccesses.
/**
* Invalidate all property accesses that cause "missing property" warnings.
*
* <p>This behavior is not inherently necessary for correctness. It only exists because the older
* version of the disambiguator invalidated these properties and some projects began to rely on
* this.
*
* <p>TODO(b/177695515): delete this method
*/
private void invalidateNonDeclaredPropertyAccesses(ColorGraphNode colorGraphNode) {
Color color = colorGraphNode.getColor();
checkArgument(!color.isInvalidating(), "Not applicable to invalidating types. All their properties are invalidated");
for (PropertyClustering prop : colorGraphNode.getAssociatedProps().keySet()) {
if (prop.isInvalidated()) {
// Skip unnecessary `hasProperty` lookups which can be expensive.
continue;
}
if (!mayHaveProperty(color, prop.getName())) {
prop.invalidate(Invalidation.undeclaredAccess(colorGraphNode.getIndex()));
}
}
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class ColorFindPropertyReferences method handleObjectLit.
private void handleObjectLit(Node n) {
// Object.defineProperties literals are handled at the CALL node.
if (n.getParent().isCall() && NodeUtil.isObjectDefinePropertiesDefinition(n.getParent())) {
return;
}
Color owner = n.getColor();
this.traverseObjectlitLike(n, (m) -> owner);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class ColorFindPropertyReferences method handleObjectPattern.
private void handleObjectPattern(Node pattern) {
Color owner = pattern.getColor();
this.traverseObjectlitLike(pattern, (m) -> owner);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class RewriteAsyncFunctionsTest method testInnerSuperReference.
@Test
public void testInnerSuperReference() {
test(externs(new TestExternsBuilder().addFunction().addJSCompLibraries().build()), srcs(lines("class A {", " m() {", " return this;", " }", "}", "class X extends A {", " async m() {", " const tmp = super.m;", " return tmp.call(null);", " }", "}")), expected(lines("class A {", " m() {", " return this;", " }", "}", "class X extends A {", " m() {", " const $jscomp$async$super$get$m = () => super.m;", " return $jscomp.asyncExecutePromiseGeneratorFunction(", " function* () {", " const tmp = $jscomp$async$super$get$m();", // type of tmp will indicate it requires `this` be provided, but will allow null.
" return tmp.call(null);", " });", " }", "}")));
// type of A.prototype.m
Color classAPropertyMType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("m").getRootNode().getColor();
Color classAInstanceType = getGlobalInstanceColor("A");
CodeSubTree classXMethodMDefinition = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m");
// Check type information on wrapper function for `super.m`
ImmutableList<Node> superMethodWrapperNameNodes = classXMethodMDefinition.findMatchingQNameReferences("$jscomp$async$super$get$m");
// one declaration and one reference
assertThat(superMethodWrapperNameNodes).hasSize(2);
// first name node is declaration
// const $jscomp$async$super$get$m = () => super.m;
Node wrapperDeclarationNameNode = superMethodWrapperNameNodes.get(0);
// arrow function has a Color representing a object
Node wrapperArrowFunction = wrapperDeclarationNameNode.getOnlyChild();
assertNode(wrapperArrowFunction).isArrowFunction().hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
// wrapper function variable has type matching the function itself
Color wrapperArrowColor = wrapperArrowFunction.getColor();
assertNode(wrapperDeclarationNameNode).hasColorThat().isEqualTo(wrapperArrowColor);
// get `super.m` from `() => `super.m`
Node superDotM = wrapperArrowFunction.getLastChild();
assertNode(superDotM).matchesQualifiedName("super.m").hasColorThat().isEqualTo(classAPropertyMType);
Node superNode = superDotM.getFirstChild();
assertNode(superNode).hasColorThat().isEqualTo(classAInstanceType);
// second name node is reference
// const tmp = $jscomp$async$super$get$m();
Node wrapperReferenceNameNode = superMethodWrapperNameNodes.get(1);
// optimization colors don't track function signatures
assertNode(wrapperReferenceNameNode).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
// `$jscomp$async$super$get$m()`
Node wrapperCallNode = wrapperReferenceNameNode.getParent();
assertNode(wrapperCallNode).isCall().hasColorThat().isEqualTo(classAPropertyMType);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class RewriteAsyncFunctionsTest method testInnerArrowFunctionUsingArguments.
@Test
public void testInnerArrowFunctionUsingArguments() {
test(externs(new TestExternsBuilder().addArguments().addJSCompLibraries().build()), srcs(lines("class X {", " async m() {", " return new Promise((resolve, reject) => {", " return arguments;", " });", " }", "}")), expected(lines("class X {", " m() {", " const $jscomp$async$arguments = arguments;", " return $jscomp.asyncExecutePromiseGeneratorFunction(", " function* () {", " return new Promise((resolve, reject) => {", " return $jscomp$async$arguments", " });", " });", " }", "}")));
ImmutableList<Node> argumentsAliasRefs = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m").findMatchingQNameReferences("$jscomp$async$arguments");
// one declaration and 1 use
assertThat(argumentsAliasRefs).hasSize(2);
Color argumentsColor = getGlobalColor(StandardColors.ARGUMENTS_ID);
// declaration reference
// const $jscomp$async$arguments = arguments;
Node argumentsAliasDeclaration = argumentsAliasRefs.get(0);
Node argumentsValue = argumentsAliasDeclaration.getOnlyChild();
assertNode(argumentsValue).matchesQualifiedName("arguments").hasColorThat().isEqualTo(argumentsColor);
assertNode(argumentsAliasDeclaration).matchesQualifiedName("$jscomp$async$arguments").hasColorThat().isEqualTo(argumentsColor);
// usage reference
// return $jscomp$async$arguments;
Node argumentsAliasUsage = argumentsAliasRefs.get(1);
assertNode(argumentsAliasUsage).matchesQualifiedName("$jscomp$async$arguments").hasColorThat().isEqualTo(argumentsColor);
}
Aggregations