Replace assign/clone helpers with object spread syntax.

This commit is contained in:
David Anson 2019-05-05 22:27:01 -07:00
parent 684416a902
commit 1b8b15693f
6 changed files with 51 additions and 55 deletions

View file

@ -1,7 +1,7 @@
{ {
"parser": "espree", "parser": "espree",
"parserOptions": { "parserOptions": {
"ecmaVersion": 6 "ecmaVersion": 2018
}, },
"env": { "env": {
"node": true, "node": true,

1
.gitignore vendored
View file

@ -5,4 +5,5 @@ demo/markdownlint-browser.min.js
lib-es3 lib-es3
node_modules node_modules
npm-debug.log npm-debug.log
.nyc_output
.vscode .vscode

View file

@ -24,20 +24,6 @@ module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// readFile options for reading with the UTF-8 encoding // readFile options for reading with the UTF-8 encoding
module.exports.utf8Encoding = { "encoding": "utf8" }; module.exports.utf8Encoding = { "encoding": "utf8" };
// Applies key/value pairs from src to dst, returning dst
function assign(dst, src) {
Object.keys(src).forEach(function forKey(key) {
dst[key] = src[key];
});
return dst;
}
module.exports.assign = assign;
// Clones the key/value pairs of obj, returning the clone
module.exports.clone = function clone(obj) {
return assign({}, obj);
};
// Returns true iff the input is a number // Returns true iff the input is a number
module.exports.isNumber = function isNumber(obj) { module.exports.isNumber = function isNumber(obj) {
return typeof obj === "number"; return typeof obj === "number";

View file

@ -271,7 +271,7 @@ function getEnabledRulesPerLineNumber(
if (!noInlineConfig) { if (!noInlineConfig) {
let match = helpers.inlineCommentRe.exec(line); let match = helpers.inlineCommentRe.exec(line);
if (match) { if (match) {
enabledRules = helpers.clone(enabledRules); enabledRules = { ...enabledRules };
while (match) { while (match) {
forMatch(match); forMatch(match);
match = helpers.inlineCommentRe.exec(line); match = helpers.inlineCommentRe.exec(line);
@ -613,7 +613,10 @@ function readConfig(file, parsers, callback) {
if (errr) { if (errr) {
return callback(errr); return callback(errr);
} }
return callback(null, helpers.assign(extendsConfig, config)); return callback(null, {
...extendsConfig,
...config
});
}); });
} }
return callback(null, config); return callback(null, config);
@ -639,9 +642,13 @@ function readConfigSync(file, parsers) {
const configExtends = config.extends; const configExtends = config.extends;
if (configExtends) { if (configExtends) {
delete config.extends; delete config.extends;
return helpers.assign( return {
readConfigSync(path.resolve(path.dirname(file), configExtends), parsers), ...readConfigSync(
config); path.resolve(path.dirname(file), configExtends),
parsers
),
...config
};
} }
return config; return config;
} }

View file

@ -13,7 +13,7 @@
"bugs": "https://github.com/DavidAnson/markdownlint/issues", "bugs": "https://github.com/DavidAnson/markdownlint/issues",
"scripts": { "scripts": {
"test": "nodeunit test/markdownlint-test.js", "test": "nodeunit test/markdownlint-test.js",
"test-cover": "istanbul cover node_modules/nodeunit/bin/nodeunit test/markdownlint-test.js", "test-cover": "nyc node_modules/nodeunit/bin/nodeunit test/markdownlint-test.js",
"test-extra": "nodeunit test/markdownlint-test-extra.js", "test-extra": "nodeunit test/markdownlint-test-extra.js",
"debug": "node debug node_modules/nodeunit/bin/nodeunit", "debug": "node debug node_modules/nodeunit/bin/nodeunit",
"lint": "eslint lib helpers test schema && eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0, no-extend-native: 0, max-statements: 0, no-console: 0, no-var: 0\" demo && eslint --rule \"no-console: 0, no-invalid-this: 0, no-shadow: 0, object-property-newline: 0\" example", "lint": "eslint lib helpers test schema && eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0, no-extend-native: 0, max-statements: 0, no-console: 0, no-var: 0\" demo && eslint --rule \"no-console: 0, no-invalid-this: 0, no-shadow: 0, object-property-newline: 0\" example",
@ -34,7 +34,6 @@
"cpy-cli": "~2.0.0", "cpy-cli": "~2.0.0",
"eslint": "~5.15.3", "eslint": "~5.15.3",
"glob": "~7.1.3", "glob": "~7.1.3",
"istanbul": "~0.4.5",
"js-yaml": "~3.13.0", "js-yaml": "~3.13.0",
"markdown-it-for-inline": "~0.1.1", "markdown-it-for-inline": "~0.1.1",
"markdown-it-katex": "~2.0.3", "markdown-it-katex": "~2.0.3",
@ -42,6 +41,7 @@
"markdown-it-sup": "~1.0.0", "markdown-it-sup": "~1.0.0",
"markdownlint-rule-helpers": "~0.1.0", "markdownlint-rule-helpers": "~0.1.0",
"nodeunit": "~0.11.3", "nodeunit": "~0.11.3",
"nyc": "~14.1.0",
"rimraf": "~2.6.3", "rimraf": "~2.6.3",
"toml": "~3.0.0", "toml": "~3.0.0",
"tv4": "~1.3.0", "tv4": "~1.3.0",

View file

@ -47,8 +47,10 @@ function createTestForFile(file) {
}) })
.then( .then(
function lintWithConfig(config) { function lintWithConfig(config) {
const mergedConfig = const mergedConfig = {
helpers.assign(helpers.clone(defaultConfig), config); ...defaultConfig,
...config
};
return promisify(markdownlint, { return promisify(markdownlint, {
"files": [ file ], "files": [ file ],
"config": mergedConfig, "config": mergedConfig,
@ -1651,11 +1653,11 @@ module.exports.configMultiple = function configMultiple(test) {
markdownlint.readConfig("./test/config/config-grandparent.json", markdownlint.readConfig("./test/config/config-grandparent.json",
function callback(err, actual) { function callback(err, actual) {
test.ifError(err); test.ifError(err);
const expected = helpers.assign( const expected = {
helpers.assign( ...require("./config/config-child.json"),
helpers.assign({}, require("./config/config-child.json")), ...require("./config/config-parent.json"),
require("./config/config-parent.json")), ...require("./config/config-grandparent.json")
require("./config/config-grandparent.json")); };
delete expected.extends; delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct."); test.deepEqual(actual, expected, "Config object not correct.");
test.done(); test.done();
@ -1729,11 +1731,11 @@ module.exports.configMultipleYaml = function configMultipleYaml(test) {
[ require("js-yaml").safeLoad ], [ require("js-yaml").safeLoad ],
function callback(err, actual) { function callback(err, actual) {
test.ifError(err); test.ifError(err);
const expected = helpers.assign( const expected = {
helpers.assign( ...require("./config/config-child.json"),
helpers.assign({}, require("./config/config-child.json")), ...require("./config/config-parent.json"),
require("./config/config-parent.json")), ...require("./config/config-grandparent.json")
require("./config/config-grandparent.json")); };
delete expected.extends; delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct."); test.deepEqual(actual, expected, "Config object not correct.");
test.done(); test.done();
@ -1747,11 +1749,11 @@ module.exports.configMultipleHybrid = function configMultipleHybrid(test) {
[ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ], [ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ],
function callback(err, actual) { function callback(err, actual) {
test.ifError(err); test.ifError(err);
const expected = helpers.assign( const expected = {
helpers.assign( ...require("./config/config-child.json"),
helpers.assign({}, require("./config/config-child.json")), ...require("./config/config-parent.json"),
require("./config/config-parent.json")), ...require("./config/config-grandparent.json")
require("./config/config-grandparent.json")); };
delete expected.extends; delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct."); test.deepEqual(actual, expected, "Config object not correct.");
test.done(); test.done();
@ -1796,11 +1798,11 @@ module.exports.configMultipleSync = function configMultipleSync(test) {
test.expect(1); test.expect(1);
const actual = const actual =
markdownlint.readConfigSync("./test/config/config-grandparent.json"); markdownlint.readConfigSync("./test/config/config-grandparent.json");
const expected = helpers.assign( const expected = {
helpers.assign( ...require("./config/config-child.json"),
helpers.assign({}, require("./config/config-child.json")), ...require("./config/config-parent.json"),
require("./config/config-parent.json")), ...require("./config/config-grandparent.json")
require("./config/config-grandparent.json")); };
delete expected.extends; delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct."); test.deepEqual(actual, expected, "Config object not correct.");
test.done(); test.done();
@ -1877,11 +1879,11 @@ module.exports.configMultipleYamlSync = function configMultipleYamlSync(test) {
test.expect(1); test.expect(1);
const actual = markdownlint.readConfigSync( const actual = markdownlint.readConfigSync(
"./test/config/config-grandparent.yaml", [ require("js-yaml").safeLoad ]); "./test/config/config-grandparent.yaml", [ require("js-yaml").safeLoad ]);
const expected = helpers.assign( const expected = {
helpers.assign( ...require("./config/config-child.json"),
helpers.assign({}, require("./config/config-child.json")), ...require("./config/config-parent.json"),
require("./config/config-parent.json")), ...require("./config/config-grandparent.json")
require("./config/config-grandparent.json")); };
delete expected.extends; delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct."); test.deepEqual(actual, expected, "Config object not correct.");
test.done(); test.done();
@ -1893,11 +1895,11 @@ function configMultipleHybridSync(test) {
const actual = markdownlint.readConfigSync( const actual = markdownlint.readConfigSync(
"./test/config/config-grandparent-hybrid.yaml", "./test/config/config-grandparent-hybrid.yaml",
[ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ]); [ JSON.parse, require("toml").parse, require("js-yaml").safeLoad ]);
const expected = helpers.assign( const expected = {
helpers.assign( ...require("./config/config-child.json"),
helpers.assign({}, require("./config/config-child.json")), ...require("./config/config-parent.json"),
require("./config/config-parent.json")), ...require("./config/config-grandparent.json")
require("./config/config-grandparent.json")); };
delete expected.extends; delete expected.extends;
test.deepEqual(actual, expected, "Config object not correct."); test.deepEqual(actual, expected, "Config object not correct.");
test.done(); test.done();
@ -2306,7 +2308,7 @@ module.exports.customRulesBadProperty = function customRulesBadProperty(test) {
].forEach(function forProperty(property) { ].forEach(function forProperty(property) {
const propertyName = property[0]; const propertyName = property[0];
property[1].forEach(function forPropertyValue(propertyValue) { property[1].forEach(function forPropertyValue(propertyValue) {
const badRule = helpers.clone(customRules.anyBlockquote); const badRule = { ...customRules.anyBlockquote };
badRule[propertyName] = propertyValue; badRule[propertyName] = propertyValue;
const options = { const options = {
"customRules": [ badRule ] "customRules": [ badRule ]