ChromeCodingConvention.java revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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
5package org.chromium.closure.compiler;
6
7import com.google.javascript.jscomp.CodingConvention;
8import com.google.javascript.jscomp.CodingConventions;
9import com.google.javascript.jscomp.DiagnosticType;
10import com.google.common.collect.ImmutableSet;
11import com.google.common.collect.Sets;
12import com.google.javascript.rhino.Node;
13import com.google.javascript.rhino.jstype.FunctionType;
14import com.google.javascript.rhino.jstype.ObjectType;
15
16import java.util.Collection;
17import java.util.List;
18import java.util.Set;
19
20public 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}
64