Class GoogleCodingConvention
- java.lang.Object
-
- com.google.javascript.jscomp.CodingConventions.Proxy
-
- com.google.javascript.jscomp.GoogleCodingConvention
-
- All Implemented Interfaces:
CodingConvention
,java.io.Serializable
@Immutable public class GoogleCodingConvention extends CodingConventions.Proxy
This describes the Google-specific JavaScript coding conventions. Within Google, variable names are semantically significant.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.google.javascript.jscomp.CodingConvention
CodingConvention.AssertionFunctionLookup, CodingConvention.AssertionFunctionSpec, CodingConvention.Bind, CodingConvention.Cache, CodingConvention.DelegateRelationship, CodingConvention.ObjectLiteralCast, CodingConvention.SubclassRelationship, CodingConvention.SubclassType
-
-
Field Summary
-
Fields inherited from class com.google.javascript.jscomp.CodingConventions.Proxy
nextConvention
-
-
Constructor Summary
Constructors Constructor Description GoogleCodingConvention()
By default, decorate the ClosureCodingConvention.GoogleCodingConvention(CodingConvention convention)
Decorates a wrapped CodingConvention.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getPackageName(StaticSourceFile source)
boolean
hasPrivacyConvention()
Whether this CodingConvention includes a convention for what private names should look like.boolean
isClassFactoryCall(Node callNode)
Checks if the given method is a call to a class factory, such a factory returns a unique class.boolean
isConstant(java.lang.String name)
This checks whether a given variable name, such as a name in all-caps should be treated as if it had the @const annotation.boolean
isConstantKey(java.lang.String name)
This checks whether a given key of an object literal, such as a name in all-caps should be treated as if it had the @const annotation.boolean
isExported(java.lang.String name, boolean local)
Checks whether a global variable or function name should be treated as exported, or externally referenceable.boolean
isOptionalParameter(Node parameter)
This checks whether a given parameter name should be treated as an optional parameter as far as type checking or function call arg count checking is concerned.boolean
isPrivate(java.lang.String name)
Checks whether a name should be considered private.boolean
isValidEnumKey(java.lang.String key)
This checks that a givenkey
may be used as a key for an enum.boolean
isVarArgsParameter(Node parameter)
This checks whether a given parameter should be treated as a marker for a variable argument list function.-
Methods inherited from class com.google.javascript.jscomp.CodingConventions.Proxy
applyDelegateRelationship, applySingletonGetter, applySubclassRelationship, blockRenamingForProperty, checkForCallingConventionDefinitions, defineDelegateProxyPrototypeProperties, describeCachingCall, describeFunctionBind, describeFunctionBind, extractClassNameIfProvide, extractClassNameIfRequire, extractIsModuleFile, getAbstractMethodName, getAssertionFunctions, getClassesDefinedByCall, getDelegateRelationship, getDelegateSuperclassName, getExportPropertyFunction, getExportSymbolFunction, getGlobalObject, getIndirectlyDeclaredProperties, getObjectLiteralCast, getSingletonGetterClassName, identifyTypeDeclarationCall, isExported, isFunctionCallThatAlwaysThrows, isInlinableFunction, isPropertyRenameFunction, isPropertyTestFunction, isPrototypeAlias, isSuperClassReference
-
-
-
-
Constructor Detail
-
GoogleCodingConvention
public GoogleCodingConvention()
By default, decorate the ClosureCodingConvention.
-
GoogleCodingConvention
public GoogleCodingConvention(CodingConvention convention)
Decorates a wrapped CodingConvention.
-
-
Method Detail
-
isConstant
public boolean isConstant(java.lang.String name)
This checks whether a given variable name, such as a name in all-caps should be treated as if it had the @const annotation.This enforces the Google const name convention, that the first character after the last $ must be an upper-case letter and all subsequent letters must be upper case. The name must be at least 2 characters long.
Examples:
aaa Not constant - lower-case letters in the name A Not constant - too short goog$A Constant - letters after the $ are upper-case. AA17 Constant - digits can appear after the first letter goog$7A Not constant - first character after the $ must be upper case. $A Constant - doesn't have to be anything in front of the $
- Specified by:
isConstant
in interfaceCodingConvention
- Overrides:
isConstant
in classCodingConventions.Proxy
- Parameters:
name
- potentially constant variable name- Returns:
true
if the name should be treated as a constant.
-
isConstantKey
public boolean isConstantKey(java.lang.String name)
Description copied from interface:CodingConvention
This checks whether a given key of an object literal, such as a name in all-caps should be treated as if it had the @const annotation.- Specified by:
isConstantKey
in interfaceCodingConvention
- Overrides:
isConstantKey
in classCodingConventions.Proxy
-
isValidEnumKey
public boolean isValidEnumKey(java.lang.String key)
This checks that a givenkey
may be used as a key for an enum.This enforces Google's convention about enum key names. They must match the regular expression
[A-Z0-9][A-Z0-9_]*
.Examples:
- A
- 213
- FOO_BAR
- Specified by:
isValidEnumKey
in interfaceCodingConvention
- Overrides:
isValidEnumKey
in classCodingConventions.Proxy
- Parameters:
key
- the potential key to an enum- Returns:
true
if thekey
may be used as an enum key,false
otherwise
-
isOptionalParameter
public boolean isOptionalParameter(Node parameter)
This checks whether a given parameter name should be treated as an optional parameter as far as type checking or function call arg count checking is concerned. Note that an optional function parameter may be declared as a simple type and is automatically converted to a union of the declared type and Undefined.In Google code, parameter names beginning with
opt_
are treated as optional arguments.- Specified by:
isOptionalParameter
in interfaceCodingConvention
- Overrides:
isOptionalParameter
in classCodingConventions.Proxy
- Parameters:
parameter
- The parameter's node.- Returns:
true
if the parameter should be treated as an optional parameter.
-
isVarArgsParameter
public boolean isVarArgsParameter(Node parameter)
Description copied from interface:CodingConvention
This checks whether a given parameter should be treated as a marker for a variable argument list function. A VarArgs parameter must be the last parameter in a function declaration.- Specified by:
isVarArgsParameter
in interfaceCodingConvention
- Overrides:
isVarArgsParameter
in classCodingConventions.Proxy
- Parameters:
parameter
- The parameter's node.- Returns:
true
if the parameter should be treated as a variable length parameter.
-
isExported
public boolean isExported(java.lang.String name, boolean local)
Checks whether a global variable or function name should be treated as exported, or externally referenceable.In Google code, any global name starting with an underscore is considered exported.
- Specified by:
isExported
in interfaceCodingConvention
- Overrides:
isExported
in classCodingConventions.Proxy
- Parameters:
name
- A global variable or function name.local
-true
if the name is a local variable.- Returns:
true
if the name should be considered exported.
-
isClassFactoryCall
public boolean isClassFactoryCall(Node callNode)
Description copied from interface:CodingConvention
Checks if the given method is a call to a class factory, such a factory returns a unique class.- Specified by:
isClassFactoryCall
in interfaceCodingConvention
- Overrides:
isClassFactoryCall
in classCodingConventions.Proxy
- Parameters:
callNode
- A CALL node.
-
getPackageName
@GwtIncompatible public java.lang.String getPackageName(StaticSourceFile source)
In Google code, the package name of a source file is its file path. Exceptions: if a source file's parent directory is "test", "tests", or "testing", that directory is stripped from the package name. If a file is generated, strip the "genfiles" prefix to try to match the package of the generating file.
- Specified by:
getPackageName
in interfaceCodingConvention
- Overrides:
getPackageName
in classCodingConventions.Proxy
- Returns:
- the package name for the given source file, or null if no package name is known.
-
isPrivate
public boolean isPrivate(java.lang.String name)
Checks whether a name should be considered private. Private global variables and functions can only be referenced within the source file in which they are declared. Private properties and methods should only be accessed by the class that defines them.In Google code, private names end with an underscore, and exported names are never considered private (see
isExported(java.lang.String, boolean)
).- Specified by:
isPrivate
in interfaceCodingConvention
- Overrides:
isPrivate
in classCodingConventions.Proxy
- Parameters:
name
- The name of a global variable or function, or a method or property.- Returns:
true
if the name should be considered private.
-
hasPrivacyConvention
public boolean hasPrivacyConvention()
Description copied from interface:CodingConvention
Whether this CodingConvention includes a convention for what private names should look like.- Specified by:
hasPrivacyConvention
in interfaceCodingConvention
- Overrides:
hasPrivacyConvention
in classCodingConventions.Proxy
-
-