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. 4 5 package org.chromium.closure.compiler; 6 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; 16 17 import java.util.Collection; 18 import java.util.Set; 19 20 public class ChromeCodingConvention extends CodingConventions.Proxy { 21 22 private final Set<String> indirectlyDeclaredProperties; 23 24 public ChromeCodingConvention() { 25 this(CodingConventions.getDefault()); 26 } 27 28 public ChromeCodingConvention(CodingConvention wrapped) { 29 super(wrapped); 30 31 Set<String> props = Sets.newHashSet("instance_", "getInstance"); 32 props.addAll(wrapped.getIndirectlyDeclaredProperties()); 33 indirectlyDeclaredProperties = ImmutableSet.copyOf(props); 34 } 35 36 @Override 37 public String getSingletonGetterClassName(Node callNode) { 38 Node callArg = callNode.getFirstChild(); 39 40 if (!callArg.matchesQualifiedName("cr.addSingletonGetter") || 41 callNode.getChildCount() != 2) { 42 return super.getSingletonGetterClassName(callNode); 43 } 44 45 return callArg.getNext().getQualifiedName(); 46 } 47 48 @Override 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()); 56 } 57 58 @Override 59 public Collection<String> getIndirectlyDeclaredProperties() { 60 return indirectlyDeclaredProperties; 61 } 62 63 @Override 64 public Collection<AssertionFunctionSpec> getAssertionFunctions() { 65 return ImmutableList.of( 66 new AssertionFunctionSpec("assert"), 67 new AssertInstanceofSpec("cr.ui.decorate") 68 ); 69 } 70 } 71