Skip to content

Commit

Permalink
Merge pull request #44 from linthtml/refactor/get_rid_of_lodash
Browse files Browse the repository at this point in the history
Get rid of lodash
  • Loading branch information
KamiKillertO committed Jun 13, 2019
2 parents be07988 + a5b8e1d commit 1695d24
Show file tree
Hide file tree
Showing 22 changed files with 646 additions and 1,264 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2018
},
"globals": {
"beforeEach": true,
Expand Down
6 changes: 3 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const lodash = require("lodash");
const { isBool } = require("./validate_option");

const pull = require('lodash.pull');
/**
* The config object stores all possible rules and options and manages
* dependencies based on which options are enabled.
Expand Down Expand Up @@ -222,7 +221,8 @@ Config.prototype.deactivateRule = function(rule) {
};

Config.prototype.removeSubscriber = function(rule, sub) {
if (!lodash.pull(rule.subscribers, sub).length) {
// I've try replacing pull with array.filter but it's not working
if (!pull(rule.subscribers, sub).length) {
this.deactivateRule(rule);
}
};
5 changes: 2 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var lodash = require("lodash"),
Linter = require("./linter");
const Linter = require("./linter");

/* istanbul ignore next */
Object.values =
Expand Down Expand Up @@ -30,7 +29,7 @@ linthtml.defaultLinter = new Linter(linthtml.rules);

linthtml.use = function(plugins) {
plugins.forEach(function(plugin) {
if (lodash.isString(plugin)) {
if (typeof plugin === "string") {
plugin = require(plugin);
}

Expand Down
23 changes: 10 additions & 13 deletions lib/inline_config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const lodash = require("lodash");
const knife = require("./knife");
const presets = require("./presets");
const Issue = require("./issue");
Expand Down Expand Up @@ -27,7 +26,7 @@ module.exports = inlineConfig;
* @param {Object} newBasis - the new options to use.
*/
inlineConfig.prototype.reset = function(newBasis) {
this.current = lodash.cloneDeep(newBasis);
this.current = Object.assign({}, newBasis);
index = 0;
};

Expand Down Expand Up @@ -72,8 +71,11 @@ function applyConfig(config) {
}
}.bind(this)
);

lodash.merge(this.previous, (this.previousPreset = previous));
this.previousPreset = previous;
this.previous = {
...this.previous,
...previous
};
}

/**
Expand All @@ -83,16 +85,11 @@ function applyConfig(config) {
*/
inlineConfig.prototype.getOptsAtIndex = function(newIndex) {
if (newIndex !== 0 && newIndex <= index) {
throw new Error(
"Cannot get options for index " +
newIndex +
" when index " +
index +
" has already been checked"
);
throw new Error(`Cannot get options for index "${newIndex}" when index "${index}" has already been checked"`);
} else {
lodash
.compact(this.indexConfigs.slice(index + 1, newIndex + 1))
this.indexConfigs
.slice(index + 1, newIndex + 1)
.filter(x => !!x)
.forEach(applyConfig, this);
index = newIndex;
}
Expand Down
27 changes: 13 additions & 14 deletions lib/knife/apply_rules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var lodash = require("lodash");
const { flatten } = require('../utils/arrays');


function addRuleToIssue(issue, ruleName) {
if (Array.isArray(issue)) {
Expand All @@ -10,20 +11,18 @@ function addRuleToIssue(issue, ruleName) {
}
}

module.exports = {
applyRules: function(rules, element, opts) {
if (!rules) {
return [];
}
module.exports = function(rules, element, opts) {
if (!rules) {
return [];
}

return lodash.flattenDeep(
rules.map(function(rule) {
var issues = rule.lint.call(rule, element, opts);
rules = rules.map(rule => {
var issues = rule.lint.call(rule, element, opts);

addRuleToIssue(issues, rule.name);
addRuleToIssue(issues, rule.name);

return issues;
})
);
}
return issues;
});
return flatten(rules);
};

29 changes: 15 additions & 14 deletions lib/knife/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var bulk = require("bulk-require");
var lodash = require("lodash");

// import all the js files in the directory
var utilExports = bulk(__dirname, "!(index).js");
var utils = {};

// mixin all the functions from the exports into utils
lodash.values(utilExports).forEach(function(u) {
lodash.mixin(utils, u);
});

// export utils
module.exports = utils;
module.exports = {
applyRules: require('./apply_rules'),
parseHtmlAttrs: require('./attr_parse').parseHtmlAttrs,
inputIndices: require('./attr_parse').inputIndices,
booleanAttrs: require('./boolean_attrs').booleanAttrs,
isBooleanAttr: require('./boolean_attrs').isBooleanAttr,
isLabeable: require('./is_labeable'),
isVoidElement: require('./is_void_element'),
checkLangTag: require('./lang_tag'),
matchFilter: require('./match_filter'),
getLineColFunc: require('./relative_line_col'),
shred: require('./shred'),
isSelfClosing: require('./tag_utils').isSelfClosing,
hasNonEmptyAttr: require('./tag_utils').hasNonEmptyAttr
};
6 changes: 2 additions & 4 deletions lib/knife/is_labeable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var lodash = require("lodash");

// these elements are *labelable elements* according to the HTML spec
var elems = [
"button",
Expand All @@ -18,8 +16,8 @@ var elems = [
* @param {Object} ele - an html element from the htmlparser2 parser
* @returns {Boolean} whether or not `ele` is labelable
*/
module.exports.isLabeable = function(ele) {
if (ele.type !== "tag" || !lodash.includes(elems, ele.name)) {
module.exports = function(ele) {
if (ele.type !== "tag" || !elems.includes(ele.name)) {
// element isn't a tag or isn't a labeable element
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/knife/is_void_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ var elems = [
* @param {String} tagName - a name of a html tag
* @returns {Boolean} whether or not `tagName` is a void element
*/
module.exports.isVoidElement = function(tagName) {
module.exports = function(tagName) {
return elems.indexOf(tagName.toLowerCase()) !== -1;
};
3 changes: 2 additions & 1 deletion lib/knife/lang_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ function checkCountry(code) {
// language code and YY is a valid country code.
// Return 1 if the tag is invalid, or 2 if it is valid but has
// unconventional capitalization.
module.exports.checkLangTag = function(l) {
module.exports = function(l) {
if (!l || l.length === 0) {
return 0;
}
Expand All @@ -420,6 +420,7 @@ module.exports.checkLangTag = function(l) {
lang = l.slice(0, n);
country = l.slice(n + 1, l.length);
}
// WHAT????
return checkLang(lang) && checkCountry(country)
? 0
: checkLang(lang.toLowerCase()) && checkCountry(country.toUpperCase())
Expand Down
2 changes: 1 addition & 1 deletion lib/knife/match_filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports.matchFilter = function(data, rule) {
module.exports = function(data, rule) {
if (!rule.filter) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/knife/relative_line_col.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Given the raw html text, produce a function that transforms an index
// into a line and column number. Indices must be passed to the resulting
// function in order.
module.exports.getLineColFunc = function(htmlText, lineCol, offset) {
module.exports = function(htmlText, lineCol, offset) {
if (offset === undefined) {
offset = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/knife/shred.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {String} html - your html.
* @returns {String[]} the array of line objects.
*/
module.exports.shred = function(html) {
module.exports = function(html) {
// Take the HTML string
// Return an array of {line, line number, index}
var row = 1,
Expand Down
11 changes: 5 additions & 6 deletions lib/linter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const lodash = require("lodash");
const Parser = require("./parser");
const knife = require("./knife");
const presets = require("./presets");
const Config = require("./config");
const InlineConfig = require("./inline_config");

const { flatten } = require("./utils/arrays");
/**
* A linter is configured with a set of rules that are fed the raw
* html and ast nodes.
Expand Down Expand Up @@ -145,17 +144,17 @@ Linter.prototype.lint = function(html) {
}

if (opts.maxerr) {
issues = lodash.take(issues, opts.maxerr);
issues = issues.slice(0, opts.maxerr);
}

return Promise.all(issues).then(function(resolved) {
return lodash.flattenDeep(resolved).map(recoverLineCol);
return flatten(resolved).map(recoverLineCol);
});
};

Linter.getOptions = function(args) {
var optList = Array.prototype.slice.call(args, 1);
optList = lodash.flattenDeep(optList);
optList = flatten(optList);

if (optList[optList.length - 1] !== "nodefault") {
optList.unshift("default");
Expand All @@ -173,7 +172,7 @@ Linter.prototype.lintDom = function(dom, opts) {
};

Linter.prototype.resetRules = function(opts) {
return lodash.flattenDeep(
return flatten(
this.rules.getAllRules().map(function(rule) {
var r = rule.end && rule.end(opts);
return r ? r : [];
Expand Down
Loading

0 comments on commit 1695d24

Please sign in to comment.