Skip to content

Commit

Permalink
Create individual Features for ARRAY_DESTRUCTURING and OBJECT_DESTRUC…
Browse files Browse the repository at this point in the history
…TURING

This will it easier to inject runtime libraries only needed for array
destructuring in Es6InjectRuntimeLibraries

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=214699989
  • Loading branch information
lauraharker authored and brad4d committed Sep 27, 2018
1 parent e99f548 commit f32aacc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/AstValidator.java
Expand Up @@ -1129,7 +1129,7 @@ private void validateGetPropGetElemInLHS(Token contextType, Node n) {
} }


private void validateArrayPattern(Token type, Node n) { private void validateArrayPattern(Token type, Node n) {
validateFeature(Feature.DESTRUCTURING, n); validateFeature(Feature.ARRAY_DESTRUCTURING, n);
validateNodeType(Token.ARRAY_PATTERN, n); validateNodeType(Token.ARRAY_PATTERN, n);
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
switch (c.getToken()) { switch (c.getToken()) {
Expand All @@ -1149,7 +1149,7 @@ private void validateArrayPattern(Token type, Node n) {
} }


private void validateObjectPattern(Token type, Node n) { private void validateObjectPattern(Token type, Node n) {
validateFeature(Feature.DESTRUCTURING, n); validateFeature(Feature.OBJECT_DESTRUCTURING, n);
validateNodeType(Token.OBJECT_PATTERN, n); validateNodeType(Token.OBJECT_PATTERN, n);
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
switch (c.getToken()) { switch (c.getToken()) {
Expand Down
Expand Up @@ -83,7 +83,10 @@ private Es6RewriteDestructuring(Builder builder) {
case REWRITE_ALL_OBJECT_PATTERNS: case REWRITE_ALL_OBJECT_PATTERNS:
this.featuresToTriggerRunningPass = this.featuresToTriggerRunningPass =
FeatureSet.BARE_MINIMUM.with( FeatureSet.BARE_MINIMUM.with(
Feature.DEFAULT_PARAMETERS, Feature.DESTRUCTURING, Feature.ARRAY_PATTERN_REST); Feature.DEFAULT_PARAMETERS,
Feature.ARRAY_DESTRUCTURING,
Feature.ARRAY_PATTERN_REST,
Feature.OBJECT_DESTRUCTURING);


// If OBJECT_PATTERN_REST were to be present in featuresToTriggerRunningPass and not the // If OBJECT_PATTERN_REST were to be present in featuresToTriggerRunningPass and not the
// input language featureSet (such as ES6=>ES5) the pass would be skipped. // input language featureSet (such as ES6=>ES5) the pass would be skipped.
Expand Down
Expand Up @@ -42,7 +42,7 @@ public final class Es6SplitVariableDeclarations extends
NodeTraversal.AbstractPostOrderCallback implements HotSwapCompilerPass { NodeTraversal.AbstractPostOrderCallback implements HotSwapCompilerPass {
private final AbstractCompiler compiler; private final AbstractCompiler compiler;
private static final FeatureSet transpiledFeatures = private static final FeatureSet transpiledFeatures =
FeatureSet.BARE_MINIMUM.with(Feature.DESTRUCTURING); FeatureSet.BARE_MINIMUM.with(Feature.ARRAY_DESTRUCTURING, Feature.OBJECT_DESTRUCTURING);


public Es6SplitVariableDeclarations(AbstractCompiler compiler) { public Es6SplitVariableDeclarations(AbstractCompiler compiler) {
this.compiler = compiler; this.compiler = compiler;
Expand Down
10 changes: 6 additions & 4 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -1026,7 +1026,7 @@ Node processArrayLiteral(ArrayLiteralExpressionTree tree) {
} }


Node processArrayPattern(ArrayPatternTree tree) { Node processArrayPattern(ArrayPatternTree tree) {
maybeWarnForFeature(tree, Feature.DESTRUCTURING); maybeWarnForFeature(tree, Feature.ARRAY_DESTRUCTURING);


Node node = newNode(Token.ARRAY_PATTERN); Node node = newNode(Token.ARRAY_PATTERN);
for (ParseTree child : tree.elements) { for (ParseTree child : tree.elements) {
Expand All @@ -1043,7 +1043,7 @@ Node processArrayPattern(ArrayPatternTree tree) {
} }


Node processObjectPattern(ObjectPatternTree tree) { Node processObjectPattern(ObjectPatternTree tree) {
maybeWarnForFeature(tree, Feature.DESTRUCTURING); maybeWarnForFeature(tree, Feature.OBJECT_DESTRUCTURING);


Node node = newNode(Token.OBJECT_PATTERN); Node node = newNode(Token.OBJECT_PATTERN);
for (ParseTree child : tree.fields) { for (ParseTree child : tree.fields) {
Expand Down Expand Up @@ -1527,8 +1527,10 @@ Node processRestParameter(RestParameterTree tree) {
maybeWarnForFeature(tree, Feature.REST_PARAMETERS); maybeWarnForFeature(tree, Feature.REST_PARAMETERS);


Node assignmentTarget = transformNodeWithInlineJsDoc(tree.assignmentTarget); Node assignmentTarget = transformNodeWithInlineJsDoc(tree.assignmentTarget);
if (assignmentTarget.isDestructuringPattern()) { if (assignmentTarget.isObjectPattern()) {
maybeWarnForFeature(tree.assignmentTarget, Feature.DESTRUCTURING); maybeWarnForFeature(tree.assignmentTarget, Feature.OBJECT_DESTRUCTURING);
} else if (assignmentTarget.isArrayPattern()) {
maybeWarnForFeature(tree.assignmentTarget, Feature.ARRAY_DESTRUCTURING);
} }
return newNode(Token.REST, assignmentTarget); return newNode(Token.REST, assignmentTarget);
} }
Expand Down
Expand Up @@ -81,7 +81,8 @@ public final class FeatureSet implements Serializable {
.without(Feature.CLASS_EXTENDS) .without(Feature.CLASS_EXTENDS)
.without(Feature.CLASS_GETTER_SETTER) .without(Feature.CLASS_GETTER_SETTER)
.without(Feature.DEFAULT_PARAMETERS) .without(Feature.DEFAULT_PARAMETERS)
.without(Feature.DESTRUCTURING) .without(Feature.ARRAY_DESTRUCTURING)
.without(Feature.OBJECT_DESTRUCTURING)
.without(Feature.MODULES) .without(Feature.MODULES)
.without(Feature.NEW_TARGET); .without(Feature.NEW_TARGET);


Expand Down Expand Up @@ -127,7 +128,8 @@ public enum Feature {
COMPUTED_PROPERTIES("computed property", LangVersion.ES6), COMPUTED_PROPERTIES("computed property", LangVersion.ES6),
CONST_DECLARATIONS("const declaration", LangVersion.ES6), CONST_DECLARATIONS("const declaration", LangVersion.ES6),
DEFAULT_PARAMETERS("default parameter", LangVersion.ES6), DEFAULT_PARAMETERS("default parameter", LangVersion.ES6),
DESTRUCTURING("destructuring", LangVersion.ES6), ARRAY_DESTRUCTURING("array destructuring", LangVersion.ES6),
OBJECT_DESTRUCTURING("object destructuring", LangVersion.ES6),
EXTENDED_OBJECT_LITERALS("extended object literal", LangVersion.ES6), EXTENDED_OBJECT_LITERALS("extended object literal", LangVersion.ES6),
FOR_OF("for-of loop", LangVersion.ES6), FOR_OF("for-of loop", LangVersion.ES6),
GENERATORS("generator", LangVersion.ES6), GENERATORS("generator", LangVersion.ES6),
Expand Down
20 changes: 13 additions & 7 deletions test/com/google/javascript/jscomp/AstValidatorTest.java
Expand Up @@ -492,13 +492,19 @@ public void testFeatureValidation_defaultParameters() {
} }


@Test @Test
public void testFeatureValidation_destructuring() { public void testFeatureValidation_arrayDestructuring() {
testFeatureValidation("var x, {a, b} = obj;", Feature.DESTRUCTURING); testFeatureValidation("var x, [a, b] = arr;", Feature.ARRAY_DESTRUCTURING);
testFeatureValidation("var x, [a, b] = arr;", Feature.DESTRUCTURING); testFeatureValidation("x = 0, [a, b] = obj;", Feature.ARRAY_DESTRUCTURING);
testFeatureValidation("(x = 0, {a, b} = obj);", Feature.DESTRUCTURING); testFeatureValidation("for ([a, b] of c) {}", Feature.ARRAY_DESTRUCTURING);
testFeatureValidation("x = 0, [a, b] = obj;", Feature.DESTRUCTURING); testFeatureValidation("function f([a, b]) {}", Feature.ARRAY_DESTRUCTURING);
testFeatureValidation("for ({a, b} of c) {}", Feature.DESTRUCTURING); }
testFeatureValidation("for ([a, b] of c) {}", Feature.DESTRUCTURING);
@Test
public void testFeatureValidation_objectDestructuring() {
testFeatureValidation("var x, {a, b} = obj;", Feature.OBJECT_DESTRUCTURING);
testFeatureValidation("(x = 0, {a, b} = obj);", Feature.OBJECT_DESTRUCTURING);
testFeatureValidation("for ({a, b} of c) {}", Feature.OBJECT_DESTRUCTURING);
testFeatureValidation("function f({a, b}) {}", Feature.OBJECT_DESTRUCTURING);
} }


@Test @Test
Expand Down

0 comments on commit f32aacc

Please sign in to comment.