Skip to content

Commit

Permalink
Modify S3498: Migrate To LayC (#3232)
Browse files Browse the repository at this point in the history
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
  • Loading branch information
yassin-kammoun-sonarsource authored Oct 10, 2023
1 parent 3690f3f commit 50aa10e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions rules/S3498/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
== Why is this an issue?

When an already-defined variable is given the same name within a new object, object-shorthand syntax is preferred as being more compact. Similarly, object-shorthand is also preferred for the definition of functions in object literals.
In JavaScript, object shorthand syntax is a more concise way to define properties on objects. It was introduced to make object literals more readable and expressive.

In the shorthand syntax, if a variable exists in the scope with the same name as the object key you're defining, you can omit the key-value pair and just write the variable name. The interpreter will automatically understand that the key and the variable are linked.

=== Noncompliant code example
Using object shorthand syntax can make your code cleaner and easier to read. It can also reduce the chance of making errors, as you don't have to repeat yourself by writing the variable name twice.

[source,javascript]
[source,javascript,diff-id=1,diff-type=noncompliant]
----
let a = 1;
Expand All @@ -17,10 +18,9 @@ let myObj = {
}
----

You can omit the property name and the colon if it is the same as the local variable name. Similarly, you can omit the `function` keyword for method definitions.

=== Compliant solution

[source,javascript]
[source,javascript,diff-id=1,diff-type=compliant]
----
let a = 1;
Expand All @@ -32,7 +32,12 @@ let myObj = {
}
----

== Resources
=== Documentation

* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer[MDN - Object initializer]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions[MDN - Property definitions]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#method_definitions[MDN - Method definitions]

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

Expand Down

0 comments on commit 50aa10e

Please sign in to comment.