Skip to content

Commit

Permalink
Extract all bindings in variable declarations
Browse files Browse the repository at this point in the history
Fixes SonarSource#828 and also some other false negatives from ReassignedParameter
  • Loading branch information
Thomas Levy committed Nov 9, 2017
1 parent 2b5d151 commit e508931
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ for (var x in obj) {
}

for (var [a, b] in obj) {
a = foo(); // FN (SYMBOL TABLE should be improved)
a = foo(); // Noncompliant
}

for (let {prop1, prop2} in obj) {
prop1 = foo(); // FN (SYMBOL TABLE should be improved)
prop1 = foo(); // Noncompliant
}

for (let x of obj) {
Expand All @@ -95,16 +95,16 @@ for (z in obj) {
}

for ([a, [b]] in obj) {
a = foo(); // FN
b = foo(); // FN
a = foo(); // Noncompliant
b = foo(); // Noncompliant
}

for ({a, b} in obj) {
a = foo(); // FN
b = foo(); // FN
a = foo(); // Noncompliant
b = foo(); // Noncompliant
}

// illegal code
for (a[1] in obj) {
a = foo();
a = foo(); // Noncompliant, FP but only because of illegal code
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function fun(){
fun(arguments) // OK
}

let unflowed;
unflowed = false;

let flowed: boolean;
flowed = true;

// Node.js-related exclusions
var foo = exports = module.exports = {} // OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ private void addUsages(VariableDeclarationTree tree) {
scope = getFunctionScope();
}

// todo Consider other BindingElementTree types
for (BindingElementTree bindingElement : tree.variables()) {
Symbol.Kind variableKind = getVariableKind(tree);

Expand All @@ -293,11 +292,11 @@ private void addUsages(VariableDeclarationTree tree) {
symbolModel.declareSymbol(identifier.name(), variableKind, scope)
.addUsage(identifier, Usage.Kind.DECLARATION_WRITE);
}
}
if (bindingElement.is(Kind.BINDING_IDENTIFIER)) {
IdentifierTree identifierTree = (IdentifierTree) bindingElement;
symbolModel.declareSymbol(identifierTree.name(), variableKind, scope)
.addUsage(identifierTree, insideForLoopVariable ? Usage.Kind.DECLARATION_WRITE : Usage.Kind.DECLARATION);
} else {
for (IdentifierTree identifier : bindingElement.bindingIdentifiers()) {
symbolModel.declareSymbol(identifier.name(), variableKind, scope)
.addUsage(identifier, insideForLoopVariable ? Usage.Kind.DECLARATION_WRITE : Usage.Kind.DECLARATION);
}
}
}
}
Expand Down

0 comments on commit e508931

Please sign in to comment.