Skip to content

Commit

Permalink
makes the member accesses of parameters danger.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jun 3, 2019
1 parent e0d8a02 commit 4e845fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/rules/require-atomic-updates.js
Expand Up @@ -55,14 +55,20 @@ function getWriteExpr(reference) {

/**
* Checks if an expression is a variable that can only be observed within the given function.
* @param {escope.Variable} variable The variable to check
* @param {Variable|null} variable The variable to check
* @param {boolean} isMemberAccess If `true` then this is a member access.
* @returns {boolean} `true` if the variable is local to the given function, and is never referenced in a closure.
*/
function isLocalVariableWithoutEscape(variable) {
function isLocalVariableWithoutEscape(variable, isMemberAccess) {
if (!variable) {
return false; // A global variable which was not defined.
}

// If the reference is a property access and the variable is a parameter, it handles the variable is not local.
if (isMemberAccess && variable.defs.some(d => d.type === "Parameter")) {
return false;
}

const functionScope = variable.scope.variableScope;

return variable.references.every(reference =>
Expand Down Expand Up @@ -208,6 +214,7 @@ module.exports = {
const name = reference.identifier.name;
const variable = reference.resolved;
const writeExpr = getWriteExpr(reference);
const isMemberAccess = reference.identifier.parent.type === "MemberExpression";

// Add a fresh read variable.
if (reference.isRead() && !(writeExpr && writeExpr.parent.operator === "=")) {
Expand All @@ -220,7 +227,7 @@ module.exports = {
*/
if (writeExpr &&
writeExpr.parent.right === writeExpr && // ← exclude variable declarations.
!isLocalVariableWithoutEscape(variable)
!isLocalVariableWithoutEscape(variable, isMemberAccess)
) {
let refs = assignmentReferences.get(writeExpr);

Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/require-atomic-updates.js
Expand Up @@ -190,6 +190,10 @@ ruleTester.run("require-atomic-updates", rule, {
code: "let foo; async function x() { foo += bar + await amount; }",
errors: [VARIABLE_ERROR]
},
{
code: "async function x() { let foo; bar(() => foo); foo += await amount; }",
errors: [VARIABLE_ERROR]
},
{
code: "let foo; function* x() { foo += yield baz }",
errors: [VARIABLE_ERROR]
Expand Down

0 comments on commit 4e845fd

Please sign in to comment.