Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Let no-restricted-properties check destructuring (fixes #7147) #7151

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 63 additions & 21 deletions lib/rules/no-restricted-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,73 @@ module.exports = {
}
});

return {
MemberExpression(node) {
const objectName = node.object && node.object.name;
const propertyName = astUtils.getStaticPropertyName(node);
const matchedObject = restrictedProperties.get(objectName);
const matchedObjectProperty = matchedObject ? matchedObject.get(propertyName) : globallyRestrictedObjects.get(objectName);
const globalMatchedProperty = globallyRestrictedProperties.get(propertyName);

if (matchedObjectProperty) {
const message = matchedObjectProperty.message ? " " + matchedObjectProperty.message : "";

context.report(node, "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}", {
objectName,
propertyName,
message
});
} else if (globalMatchedProperty) {
const message = globalMatchedProperty.message ? " " + globalMatchedProperty.message : "";
/**
* Checks to see whether a property access is restricted, and reports it if so.
* @param {ASTNode} node The node to report
* @param {string} objectName The name of the object
* @param {string} propertyName The name of the property
* @returns {undefined}
*/
function checkPropertyAccess(node, objectName, propertyName) {
if (propertyName === null) {
return;
}
const matchedObject = restrictedProperties.get(objectName);
const matchedObjectProperty = matchedObject ? matchedObject.get(propertyName) : globallyRestrictedObjects.get(objectName);
const globalMatchedProperty = globallyRestrictedProperties.get(propertyName);

if (matchedObjectProperty) {
const message = matchedObjectProperty.message ? " " + matchedObjectProperty.message : "";

context.report(node, "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}", {
objectName,
propertyName,
message
});
} else if (globalMatchedProperty) {
const message = globalMatchedProperty.message ? " " + globalMatchedProperty.message : "";

context.report(node, "'{{propertyName}}' is restricted from being used.{{message}}", {
propertyName,
message
});
}
}

context.report(node, "'{{propertyName}}' is restricted from being used.{{message}}", {
propertyName,
message
/**
* Checks property accesses in a destructuring assignment expression, e.g. `var foo; ({foo} = bar);`
* @param {ASTNode} node An AssignmentExpression or AssignmentPattern node
* @returns {undefined}
*/
function checkDestructuringAssignment(node) {
if (node.right.type === "Identifier") {
const objectName = node.right.name;

if (node.left.type === "ObjectPattern") {
node.left.properties.forEach(property => {
checkPropertyAccess(node.left, objectName, astUtils.getStaticPropertyName(property));
});
}
}
}

return {
MemberExpression(node) {
checkPropertyAccess(node, node.object && node.object.name, astUtils.getStaticPropertyName(node));
},
VariableDeclarator(node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And AssignmentExpression/AssignmentPattern can access properties by destructuring.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR to check for an AssignmentExpression, but what is an example of an AssignmentPattern?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const obj = {a: 1}
function foo({a} = obj) { // obj.a
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to check for AssignmentPatterns as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mysticatea does this rule flag this?

function foo(obj) {
    return obj.a;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nzakas: Yes; it reports any reference to the a property of anything named obj (assuming the config is [2, {"object": "obj", "property": "a"}] ).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, just wanted to double-check. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It's checked as a MemberExpression.

if (node.init && node.init.type === "Identifier") {
const objectName = node.init.name;

if (node.id.type === "ObjectPattern") {
node.id.properties.forEach(property => {
checkPropertyAccess(node.id, objectName, astUtils.getStaticPropertyName(property));
});
}
}
},
AssignmentExpression: checkDestructuringAssignment,
AssignmentPattern: checkDestructuringAssignment
};
}
};
128 changes: 128 additions & 0 deletions tests/lib/rules/no-restricted-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,74 @@ ruleTester.run("no-restricted-properties", rule, {
options: [{
object: "foo"
}]
}, {
code: "let bar = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
}, {
code: "let {baz: bar} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let {unrelated} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let {baz: {bar: qux}} = foo;", property: "bar",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let {bar} = foo.baz;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let {baz: bar} = foo;",
options: [{ property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let baz; ({baz: bar} = foo)",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let bar;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let bar; ([bar = 5] = foo);",
options: [{ object: "foo", property: "1" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "function qux({baz: bar} = foo) {}",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let [bar, baz] = foo;",
options: [{ object: "foo", property: "1" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let [, bar] = foo;",
options: [{ object: "foo", property: "0" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let [, bar = 5] = foo;",
options: [{ object: "foo", property: "1" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let bar; ([bar = 5] = foo);",
options: [{ object: "foo", property: "0" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "function qux([bar] = foo) {}",
options: [{ object: "foo", property: "0" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "function qux([, bar] = foo) {}",
options: [{ object: "foo", property: "0" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "function qux([, bar] = foo) {}",
options: [{ object: "foo", property: "1" }],
parserOptions: { ecmaVersion: 6 }
}
],

Expand Down Expand Up @@ -199,6 +267,66 @@ ruleTester.run("no-restricted-properties", rule, {
message: "'require.resolve' is restricted from being used.",
type: "MemberExpression"
}]
}, {
code: "let {bar} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let {bar: baz} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let {'bar': baz} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let {bar: {baz: qux}} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let {bar} = foo;",
options: [{ object: "foo" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let {bar: baz} = foo;",
options: [{ object: "foo" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let {bar} = foo;",
options: [{ property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let bar; ({bar} = foo);",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "let bar; ({bar: baz = 1} = foo);",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "function qux({bar} = foo) {}",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "function qux({bar: baz} = foo) {}",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'foo.bar' is restricted from being used.", type: "ObjectPattern" }]
}, {
code: "var {['foo']: qux, bar} = baz",
options: [{ object: "baz", property: "foo" }],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "'baz.foo' is restricted from being used.", type: "ObjectPattern" }]
}
]
});