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

Fix "no-global-jquery" #114

Merged
merged 2 commits into from
Aug 1, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/no-global-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {

VariableDeclarator(node) {
if (emberImportAliasName) {
if (utils.isMemberExpression(node.init)) {
if (node.init && utils.isMemberExpression(node.init)) {
// assignment of type const $ = Ember.$;
destructuredAssignment = node.id.name;
} else {
Expand Down
14 changes: 14 additions & 0 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
isMemberExpression,
isCallExpression,
isObjectExpression,
isObjectPattern,
isArrayExpression,
isFunctionExpression,
isArrowFunctionExpression,
Expand Down Expand Up @@ -103,6 +104,16 @@ function isObjectExpression(node) {
return node !== undefined && node.type === 'ObjectExpression';
}

/**
* Check whether or not a node is an ObjectPattern.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ObjectPattern.
*/
function isObjectPattern(node) {
return node !== undefined && node.type === 'ObjectPattern';
}

/**
* Check whether or not a node is an ArrayExpression.
*
Expand Down Expand Up @@ -333,12 +344,15 @@ function getPropertyValue(node, path) {
* @return {Array} list of object pattern bindings
*/
function collectObjectPatternBindings(node, initialObjToBinding) {
if (!isObjectPattern(node.id)) return [];

const identifiers = Object.keys(initialObjToBinding);
const objBindingName = node.init.name;
const bindingIndex = identifiers.indexOf(objBindingName);
if (bindingIndex === -1) return [];

const binding = identifiers[bindingIndex];

return node.id.properties
.filter(props => initialObjToBinding[binding].indexOf(props.key.name) > -1)
.map(props => props.value.name);
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/no-global-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ const MESSAGE = rule.meta.message;

ruleTester.run('no-global-jquery', rule, {
valid: [
{
code: `
import Ember from 'ember';

let something;

export default Ember.Component({
actions: {
valid() {
something = Ember.$('.invalid1');
}
}
});`,
parserOptions,
},
{
code: `
import Ember from 'ember';

const Em = Ember;

export default Ember.Component({
actions: {
valid() {
this.inv1 = Em.$('.invalid1');
}
}
});`,
parserOptions,
},
{
code: `
export default Ember.Component({
Expand Down