== Why is this an issue?

ECMAScript 2015 introduced the ability to extract and assign multiple data points from an object or array simultaneously. This is called "destructuring", and it allows you to condense boilerplate code so you can concentrate on logic. 


This rule raises an issue when multiple pieces of data are extracted out of the same object or array and assigned to variables.


=== Noncompliant code example

[source,javascript]
----
function foo (obj1, obj2, array) {
  var a = obj1.a;  // Noncompliant
  var b = obj1.b;

  var name = obj2.name;  // ignored; there's only one extraction-and-assignment

  var zero = array[0];  // Noncompliant
  var one = array[1];
}
----


=== Compliant solution

[source,javascript]
----
function foo (obj1, obj2, array) {
  var {a, b} = obj1;

  var {name} = obj2;  // this syntax works because var name and property name are the same

  var [zero, one] = array;
}
----



ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Use destructuring syntax for these assignments from "xxx".


=== Highlighting

* Primary: first assignment from object/array
* Secondary: all subsequent assignments from same object/array
** message: 'Replace this assignment.'


'''
== Comments And Links
(visible only on this page)

=== on 6 Apr 2016, 13:20:40 Christian wrote:
Sorry for hijacking this Issue, but I found a small problem with ES2015 destructuring and am not able to create a new issue in this tracker...


The parser currently dies when using destructuring with default values provided. The following sample throws an error in Version 2.11:



----
    let {
      headlines = [],
      breadCrumb = [],
      [this.myProp]: data=[],
      translations = {}
    } = response.data;
----

Destructuring has the possibility to use default values, as well as custom property names (as in the this.myProp line).


The following error is encountered:


{noformat}

ERROR: Error during Sonar runner execution

org.sonar.runner.impl.RunnerException: Unable to execute Sonar

	at org.sonar.runner.impl.BatchLauncher$1.delegateExecution(BatchLauncher.java:91)

	at org.sonar.runner.impl.BatchLauncher$1.run(BatchLauncher.java:75)

	at java.security.AccessController.doPrivileged(Native Method)

	at org.sonar.runner.impl.BatchLauncher.doExecute(BatchLauncher.java:69)

	at org.sonar.runner.impl.BatchLauncher.execute(BatchLauncher.java:50)

	at org.sonar.runner.api.EmbeddedRunner.doExecute(EmbeddedRunner.java:102)

	at org.sonar.runner.api.Runner.execute(Runner.java:90)

	at org.sonar.runner.Main.executeTask(Main.java:70)

	at org.sonar.runner.Main.execute(Main.java:59)

	at org.sonar.runner.Main.main(Main.java:41)

Caused by: org.sonar.squidbridge.api.AnalysisException: Unable to analyse file: /var/lib/jenkins/jobs/xyz/workspace/src/Test.js

	at org.sonar.plugins.javascript.JavaScriptSquidSensor.analyse(JavaScriptSquidSensor.java:195)

	at org.sonar.plugins.javascript.JavaScriptSquidSensor.analyseFiles(JavaScriptSquidSensor.java:161)

	at org.sonar.plugins.javascript.JavaScriptSquidSensor.analyse(JavaScriptSquidSensor.java:152)

	at org.sonar.batch.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:79)

	at org.sonar.batch.phases.SensorsExecutor.execute(SensorsExecutor.java:70)

	at org.sonar.batch.phases.PhaseExecutor.execute(PhaseExecutor.java:119)

	at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:194)

	at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:93)

	at org.sonar.api.platform.ComponentContainer.execute(ComponentContainer.java:78)

	at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:233)

	at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:228)

	at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:221)

	at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:93)

	at org.sonar.api.platform.ComponentContainer.execute(ComponentContainer.java:78)

	at org.sonar.batch.scan.ScanTask.scan(ScanTask.java:64)

	at org.sonar.batch.scan.ScanTask.execute(ScanTask.java:51)

	at org.sonar.batch.bootstrap.TaskContainer.doAfterStart(TaskContainer.java:125)

	at org.sonar.api.platform.ComponentContainer.startComponents(ComponentContainer.java:93)

	at org.sonar.api.platform.ComponentContainer.execute(ComponentContainer.java:78)

	at org.sonar.batch.bootstrap.BootstrapContainer.executeTask(BootstrapContainer.java:173)

	at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:95)

	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67)

	at org.sonar.runner.batch.IsolatedLauncher.execute(IsolatedLauncher.java:45)

	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

	at java.lang.reflect.Method.invoke(Method.java:606)

	at org.sonar.runner.impl.BatchLauncher$1.delegateExecution(BatchLauncher.java:87)

{noformat}



=== on 6 Apr 2016, 13:23:34 Ann Campbell wrote:
\[~cs] the proper way to raise this is in the SonarQube Google group, not - as you said - to "hijack" an issue.

endif::env-github,rspecator-view[]
