Skip to content

Commit

Permalink
Merge pull request #738 from bmish/set-not-array
Browse files Browse the repository at this point in the history
Use sets instead of arrays for better performance
  • Loading branch information
bmish authored Mar 27, 2020
2 parents 712a9ec + c6f1ea2 commit ae0e187
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lib/rules/classic-decorator-no-classic-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const disallowedMethods = [
const disallowedMethods = new Set([
'get',
'set',
'getProperties',
Expand All @@ -12,7 +12,7 @@ const disallowedMethods = [
'notifyPropertyChange',
'cacheFor',
'proto',
];
]);

function disallowedMethodErrorMessage(name) {
return `The this.${name}() method is a classic ember object method, and can't be used in octane classes. You can refactor this usage to use a utility version instead (e.g. get(this, 'foo')), or to use native/modern syntax instead. Alternatively, you can add the @classic decorator to this class to continue using classic APIs.`;
Expand Down Expand Up @@ -61,7 +61,7 @@ module.exports = {
return;
}

if (disallowedMethods.includes(node.property.name)) {
if (disallowedMethods.has(node.property.name)) {
context.report({
node,
message: disallowedMethodErrorMessage(node.property.name),
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/named-functions-in-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ module.exports = {

function hasPromiseExpression(node) {
const callee = node.callee;
const promisesMethods = ['then', 'catch', 'finally'];
const promisesMethods = new Set(['then', 'catch', 'finally']);

return (
types.isCallExpression(callee.object) &&
types.isIdentifier(callee.property) &&
promisesMethods.includes(callee.property.name)
promisesMethods.has(callee.property.name)
);
}
11 changes: 7 additions & 4 deletions lib/rules/no-function-prototype-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ module.exports = {
create(context) {
const message = "Don't use Ember's function prototype extensions";

const functionPrototypeExtensionNames = ['property', 'observes', 'observesBefore', 'on'];
const functionPrototypeExtensionNames = new Set([
'property',
'observes',
'observesBefore',
'on',
]);

const isFunctionPrototypeExtension = function(property) {
return (
types.isIdentifier(property) && functionPrototypeExtensionNames.includes(property.name)
);
return types.isIdentifier(property) && functionPrototypeExtensionNames.has(property.name);
};

const report = function(node) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-global-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ module.exports = {

return {
ImportDeclaration(node) {
const validImportAlias = ['ember', 'jquery'];
const validImportAlias = new Set(['ember', 'jquery']);
// Track if the 'jquery' and/or 'ember' module (if an application
// is not using the new modules import syntax) is being imported.
// Will exclude non-ember or non-jquery imports such as:
// - `import Foo from 'bar';`
// - `import { readOnly } from '@ember/object/computed';`
if (validImportAlias.includes(node.source.value)) {
if (validImportAlias.has(node.source.value)) {
const isJqueryImport = node.source.value === 'jquery';
if (isJqueryImport) {
hasJqueryImport = true;
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-on-calls-in-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {

create(context) {
const message = "Don't use .on() for component lifecycle events.";
const lifecycleHooks = [
const lifecycleHooks = new Set([
'init',
'didUpdateAttrs',
'didReceiveAttrs',
Expand All @@ -36,7 +36,7 @@ module.exports = {
'willDestroyElement',
'willClearRender',
'didDestroyElement',
];
]);

const isOnCall = function(node) {
if (!node.value) {
Expand All @@ -48,7 +48,7 @@ module.exports = {

return (
types.isCallExpression(value) &&
lifecycleHooks.includes(args[0]) &&
lifecycleHooks.has(args[0]) &&
((types.isIdentifier(callee) && callee.name === 'on') ||
(types.isMemberExpression(callee) &&
types.isIdentifier(callee.object) &&
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/use-ember-get-and-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {

const avoidedMethods = ['get', 'set', 'getProperties', 'setProperties', 'getWithDefault'];

const testMethodsToSkip = ['get', 'set'];
const testMethodsToSkip = new Set(['get', 'set']);

const directoriesToSkipCompletely = ['mirage'];

Expand Down Expand Up @@ -124,7 +124,7 @@ module.exports = {
isFileInDirectory('tests') &&
isThisExpression(callee.object) &&
isIdentifier(method) &&
testMethodsToSkip.includes(method.name)
testMethodsToSkip.has(method.name)
) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ function isObserverProp(node) {
}

function isComputedProp(node) {
const allowedMemberExpNames = ['volatile', 'readOnly', 'property', 'meta'];
const allowedMemberExpNames = new Set(['volatile', 'readOnly', 'property', 'meta']);
if (types.isMemberExpression(node.callee) && types.isCallExpression(node.callee.object)) {
return (
isModule(node.callee.object, 'computed') &&
types.isIdentifier(node.callee.property) &&
allowedMemberExpNames.includes(node.callee.property.name)
allowedMemberExpNames.has(node.callee.property.name)
);
}
return isModule(node, 'computed');
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/new-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { isIdentifier, isObjectPattern, isVariableDeclarator } = require('./types');

const EMBER_NAMESPACES = ['inject.controller', 'inject.service'];
const EMBER_NAMESPACES = new Set(['inject.controller', 'inject.service']);

// Both the Controller module and Service module each make available the
// "inject" namespace. In order to make it more readable, so it's more
Expand All @@ -11,7 +11,7 @@ const EMBER_NAMESPACES = ['inject.controller', 'inject.service'];
// can properly populate the import specifier to report the ESLint error.
// Ex: import { inject as service } from '@ember/service';
function isNamespace(obj) {
return EMBER_NAMESPACES.includes(`${obj.parent}.${obj.key}`);
return EMBER_NAMESPACES.has(`${obj.parent}.${obj.key}`);
}

function isNamedExport(obj) {
Expand Down

0 comments on commit ae0e187

Please sign in to comment.