1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.closure.compiler;
7 import com.google.common.collect.ImmutableList;
8 import com.google.common.collect.ImmutableSet;
9 import com.google.common.collect.Sets;
10 import com.google.javascript.jscomp.ClosureCodingConvention.AssertInstanceofSpec;
11 import com.google.javascript.jscomp.CodingConvention;
12 import com.google.javascript.jscomp.CodingConventions;
13 import com.google.javascript.rhino.Node;
14 import com.google.javascript.rhino.jstype.FunctionType;
15 import com.google.javascript.rhino.jstype.ObjectType;
17 import java.util.Collection;
20 public class ChromeCodingConvention extends CodingConventions.Proxy {
22 private final Set<String> indirectlyDeclaredProperties;
24 public ChromeCodingConvention() {
25 this(CodingConventions.getDefault());
28 public ChromeCodingConvention(CodingConvention wrapped) {
31 Set<String> props = Sets.newHashSet("instance_", "getInstance");
32 props.addAll(wrapped.getIndirectlyDeclaredProperties());
33 indirectlyDeclaredProperties = ImmutableSet.copyOf(props);
37 public String getSingletonGetterClassName(Node callNode) {
38 Node callArg = callNode.getFirstChild();
40 if (!callArg.matchesQualifiedName("cr.addSingletonGetter") ||
41 callNode.getChildCount() != 2) {
42 return super.getSingletonGetterClassName(callNode);
45 return callArg.getNext().getQualifiedName();
49 public void applySingletonGetter(FunctionType functionType,
50 FunctionType getterType, ObjectType objectType) {
51 super.applySingletonGetter(functionType, getterType, objectType);
52 functionType.defineDeclaredProperty("getInstance", getterType,
53 functionType.getSource());
54 functionType.defineDeclaredProperty("instance_", objectType,
55 functionType.getSource());
59 public Collection<String> getIndirectlyDeclaredProperties() {
60 return indirectlyDeclaredProperties;
64 public Collection<AssertionFunctionSpec> getAssertionFunctions() {
65 return ImmutableList.of(
66 new AssertionFunctionSpec("assert"),
67 new AssertInstanceofSpec("cr.ui.decorate")