Skip to content

Commit

Permalink
Add fields for recording property getters and setters.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188532250
  • Loading branch information
brad4d authored and blickly committed Mar 12, 2018
1 parent c1a6fd4 commit aa368ec
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/com/google/javascript/jscomp/AbstractCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,75 @@ abstract void updateGlobalVarReferences(Map<Var, ReferenceCollection>

abstract void addComments(String filename, List<Comment> comments);

/** Indicates whether a property has a getter or a setter, or both. */
public enum PropertyAccessKind {
// To save space properties without getters or setters won't appear
// in the maps at all, but NORMAL will be returned by some methods.
NORMAL(0),
GETTER_ONLY(1),
SETTER_ONLY(2),
GETTER_AND_SETTER(3);

final byte flags;

PropertyAccessKind(int flags) {
this.flags = (byte) flags;
}

boolean hasGetter() {
return (flags & 1) != 0;
}

boolean hasSetter() {
return (flags & 2) != 0;
}

boolean hasGetterOrSetter() {
return (flags & 3) != 0;
}

// used to combine information from externs and from sources
PropertyAccessKind unionWith(PropertyAccessKind other) {
int combinedFlags = this.flags | other.flags;
switch (combinedFlags) {
case 0:
return NORMAL;
case 1:
return GETTER_ONLY;
case 2:
return SETTER_ONLY;
case 3:
return GETTER_AND_SETTER;
default:
throw new IllegalStateException("unexpected value: " + combinedFlags);
}
}
}

/**
* Returns a map containing an entry for every property name found in the externs files with
* a getter and / or setter defined.
*
* <p>Property names for which there are no getters or setters will not be in the map.
*/
abstract ImmutableMap<String, PropertyAccessKind> getExternGetterAndSetterProperties();

/** Sets the map of extern properties with getters and setters. */
abstract void setExternGetterAndSetterProperties(
ImmutableMap<String, PropertyAccessKind> externGetterAndSetterProperties);

/**
* Returns a map containing an entry for every property name found in the source AST with
* a getter and / or setter defined.
*
* <p>Property names for which there are no getters or setters will not be in the map.
*/
abstract ImmutableMap<String, PropertyAccessKind> getSourceGetterAndSetterProperties();

/** Sets the map of properties with getters and setters defined in the sources AST. */
abstract void setSourceGetterAndSetterProperties(
ImmutableMap<String, PropertyAccessKind> externGetterAndSetterProperties);

/**
* Returns all the comments from the given file.
*/
Expand Down
24 changes: 24 additions & 0 deletions src/com/google/javascript/jscomp/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ public SourceFile loadSource(String filename) {
private String lastPassName;

private Set<String> externProperties = null;
private ImmutableMap<String, PropertyAccessKind> externGetterAndSetterProperties = null;
private ImmutableMap<String, PropertyAccessKind> sourceGetterAndSetterProperties = null;

private static final Joiner pathJoiner = Joiner.on(File.separator);

Expand Down Expand Up @@ -3268,6 +3270,28 @@ Set<String> getExternProperties() {
return externProperties;
}

@Override
ImmutableMap<String, PropertyAccessKind> getExternGetterAndSetterProperties() {
return externGetterAndSetterProperties;
}

@Override
void setExternGetterAndSetterProperties(
ImmutableMap<String, PropertyAccessKind> externGetterAndSetterProperties) {
this.externGetterAndSetterProperties = externGetterAndSetterProperties;
}

@Override
ImmutableMap<String, PropertyAccessKind> getSourceGetterAndSetterProperties() {
return sourceGetterAndSetterProperties;
}

@Override
void setSourceGetterAndSetterProperties(
ImmutableMap<String, PropertyAccessKind> sourceGetterAndSetterProperties) {
this.sourceGetterAndSetterProperties = sourceGetterAndSetterProperties;
}

/**
* Replaces one file in a hot-swap mode. The given JsAst should be made
* from a new version of a file that already was present in the last compile
Expand Down

0 comments on commit aa368ec

Please sign in to comment.