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

Reduce install size by 30% #2876

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 7 additions & 4 deletions index.js
@@ -1,8 +1,5 @@
'use strict';

const fromEntries = require('object.fromentries');
const entries = require('object.entries');
Copy link
Member

Choose a reason for hiding this comment

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

this plugin supports node 4, which lacks Object.entries, so we still need the polyfill.


/* eslint-disable global-require */
const allRules = {
'boolean-prop-naming': require('./lib/rules/boolean-prop-naming'),
Expand Down Expand Up @@ -97,8 +94,14 @@ const allRules = {
};
/* eslint-enable */

const fromEntries = Object.fromEntries || function fromEntries(entries) {
const obj = {};
for (const [key, value] of entries) obj[key] = value;
return obj;
}
Comment on lines +97 to +101
Copy link
Member

Choose a reason for hiding this comment

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

inlining a polyfill (one that's not matching the spec) is a nonstarter.

Copy link
Author

Choose a reason for hiding this comment

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

Why not rewrite it to avoid the expensive polyfill then?

Copy link
Member

Choose a reason for hiding this comment

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

The polyfill isn't expensive, it's the cheapest possible way to correctly implement the method.


function filterRules(rules, predicate) {
return fromEntries(entries(rules).filter((entry) => predicate(entry[1])));
return fromEntries(Object.entries(rules).filter((entry) => predicate(entry[1])));
}

function configureAsError(rules) {
Expand Down
10 changes: 4 additions & 6 deletions lib/util/Components.js
Expand Up @@ -6,8 +6,6 @@
'use strict';

const doctrine = require('doctrine');
const arrayIncludes = require('array-includes');
const values = require('object.values');
Comment on lines -9 to -10
Copy link
Member

Choose a reason for hiding this comment

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

node 4 also lacks these functions.


const variableUtil = require('./variable');
const pragmaUtil = require('./pragma');
Expand Down Expand Up @@ -562,7 +560,7 @@ function componentRule(rule, context) {
*/
getDetectedComponents() {
const list = components.list();
return values(list).filter((val) => {
return Object.values(list).filter((val) => {
if (val.node.type === 'ClassDeclaration') {
return true;
}
Expand Down Expand Up @@ -590,7 +588,7 @@ function componentRule(rule, context) {
nodeWrapsComponent(node) {
const childComponent = this.getNameOfWrappedComponent(node.arguments);
const componentList = this.getDetectedComponents();
return !!childComponent && arrayIncludes(componentList, childComponent);
return !!childComponent && componentList.includes(childComponent);
},

isPragmaComponentWrapper(node) {
Expand All @@ -600,11 +598,11 @@ function componentRule(rule, context) {
const propertyNames = ['forwardRef', 'memo'];
const calleeObject = node.callee.object;
if (calleeObject && node.callee.property) {
return arrayIncludes(propertyNames, node.callee.property.name)
return propertyNames.includes(node.callee.property.name)
&& calleeObject.name === pragma
&& !this.nodeWrapsComponent(node);
}
return arrayIncludes(propertyNames, node.callee.name) && this.isDestructuredFromPragmaImport(node.callee.name);
return propertyNames.includes(node.callee.name) && this.isDestructuredFromPragmaImport(node.callee.name);
},

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/util/defaultProps.js
Expand Up @@ -4,7 +4,6 @@

'use strict';

const fromEntries = require('object.fromentries');
const astUtil = require('./ast');
const propsUtil = require('./props');
const variableUtil = require('./variable');
Expand Down Expand Up @@ -90,7 +89,10 @@ module.exports = function defaultPropsInstructions(context, components, utils) {
const newDefaultProps = Object.assign(
{},
defaults,
fromEntries(defaultProps.map((prop) => [prop.name, prop]))
defaultProps.reduce((obj, prop) => {
obj[prop.name] = prop;
return obj;
}, {})
);

components.set(component.node, {
Expand Down