| 1 | /* |
| 2 | * Copyright 2007 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.javascript.jscomp; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | import com.google.javascript.rhino.Node; |
| 23 | |
| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | /** |
| 27 | * This describes the Google-specific JavaScript coding conventions. |
| 28 | * Within Google, variable names are semantically significant. |
| 29 | * |
| 30 | */ |
| 31 | public class GoogleCodingConvention extends CodingConventions.Proxy { |
| 32 | |
| 33 | private static final long serialVersionUID = 1L; |
| 34 | |
| 35 | private static final String OPTIONAL_ARG_PREFIX = "opt_"; |
| 36 | |
| 37 | private static final String VAR_ARGS_NAME = "var_args"; |
| 38 | |
| 39 | private static final Pattern ENUM_KEY_PATTERN = |
| 40 | Pattern.compile("[A-Z0-9][A-Z0-9_]*"); |
| 41 | |
| 42 | /** By default, decorate the ClosureCodingConvention. */ |
| 43 | public GoogleCodingConvention() { |
| 44 | this(new ClosureCodingConvention()); |
| 45 | } |
| 46 | |
| 47 | /** Decorates a wrapped CodingConvention. */ |
| 48 | public GoogleCodingConvention(CodingConvention convention) { |
| 49 | super(convention); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * {@inheritDoc} |
| 54 | * |
| 55 | * <p>This enforces the Google const name convention, that the first character |
| 56 | * after the last $ must be an upper-case letter and all subsequent letters |
| 57 | * must be upper case. The name must be at least 2 characters long. |
| 58 | * |
| 59 | * <p>Examples: |
| 60 | * <pre> |
| 61 | * aaa Not constant - lower-case letters in the name |
| 62 | * A Not constant - too short |
| 63 | * goog$A Constant - letters after the $ are upper-case. |
| 64 | * AA17 Constant - digits can appear after the first letter |
| 65 | * goog$7A Not constant - first character after the $ must be |
| 66 | * upper case. |
| 67 | * $A Constant - doesn't have to be anything in front of the $ |
| 68 | * </pre> |
| 69 | */ |
| 70 | @Override |
| 71 | public boolean isConstant(String name) { |
| 72 | if (name.length() <= 1) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // In compiled code, '$' is often a namespace delimiter. To allow inlining |
| 77 | // of namespaced constants, we strip off any namespaces here. |
| 78 | int pos = name.lastIndexOf('$'); |
| 79 | if (pos >= 0) { |
| 80 | name = name.substring(pos + 1); |
| 81 | if (name.length() == 0) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return isConstantKey(name); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean isConstantKey(String name) { |
| 91 | if (name.isEmpty() || !Character.isUpperCase(name.charAt(0))) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // hack way of checking that there aren't any lower-case letters |
| 96 | return name.toUpperCase().equals(name); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@inheritDoc} |
| 101 | * |
| 102 | * <p>This enforces Google's convention about enum key names. They must match |
| 103 | * the regular expression {@code [A-Z0-9][A-Z0-9_]*}. |
| 104 | * |
| 105 | * <p>Examples: |
| 106 | * <ul> |
| 107 | * <li>A</li> |
| 108 | * <li>213</li> |
| 109 | * <li>FOO_BAR</li> |
| 110 | * </ul> |
| 111 | */ |
| 112 | @Override |
| 113 | public boolean isValidEnumKey(String key) { |
| 114 | return ENUM_KEY_PATTERN.matcher(key).matches(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | * |
| 120 | * <p>In Google code, parameter names beginning with {@code opt_} are |
| 121 | * treated as optional arguments. |
| 122 | */ |
| 123 | @Override |
| 124 | public boolean isOptionalParameter(Node parameter) { |
| 125 | return parameter.getString().startsWith(OPTIONAL_ARG_PREFIX); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean isVarArgsParameter(Node parameter) { |
| 130 | return VAR_ARGS_NAME.equals(parameter.getString()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * {@inheritDoc} |
| 135 | * |
| 136 | * <p>In Google code, any global name starting with an underscore is |
| 137 | * considered exported. |
| 138 | */ |
| 139 | @Override |
| 140 | public boolean isExported(String name, boolean local) { |
| 141 | return super.isExported(name, local) || |
| 142 | (!local && name.startsWith("_")); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * {@inheritDoc} |
| 147 | * |
| 148 | * <p>In Google code, private names end with an underscore, and exported |
| 149 | * names are never considered private (see {@link #isExported}). |
| 150 | */ |
| 151 | @Override |
| 152 | public boolean isPrivate(String name) { |
| 153 | return name.endsWith("_") && !isExported(name); |
| 154 | } |
| 155 | } |