Skip to content

Commit

Permalink
Merge pull request #60 from phanect/fix/eslint-update
Browse files Browse the repository at this point in the history
Fix compatibility error caused by ESLint 8.40.0 update
  • Loading branch information
phanect committed May 7, 2023
2 parents 740a6da + a79df35 commit ed56efd
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
name: default
steps:
- init
- run:
name: Install this version of eslint-plugin-editorconfig
command: npm pack && npm install "eslint-plugin-editorconfig-$(jq --raw-output .version package.json).tgz"
- run: npm test
- run: npm run lint

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ All the citation in the docs is from the backend ESLint rule document otherwise
| [editorconfig/linebreak-style](docs/rules/linebreak-style.md) | Enforce EditorConfig rules for linebreak style ||
| [editorconfig/no-trailing-spaces](docs/rules/no-trailing-spaces.md) | Enforce EditorConfig rules for trailing spaces ||

## License
## License & Credit

[MIT](https://vjpr.mit-license.org)

This plugin includes code derived from [klona](https://github.com/lukeed/klona).
6 changes: 2 additions & 4 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const editorconfig = require("editorconfig");
const { Linter } = require("eslint");
const { klona } = require("klona/lite");
const { clone } = require("./clone.js");

module.exports.buildRule = ({ baseRuleName, description, omitFirstOption, getESLintOption }) => {
const jsBaseRule = klona(new Linter().getRules().get(baseRuleName));
Expand All @@ -31,7 +32,6 @@ module.exports.buildRule = ({ baseRuleName, description, omitFirstOption, getESL
const filename = context.getFilename();
const ecParams = editorconfig.parseSync(context.getFilename(filename));
const { enabled, eslintOption } = getESLintOption(ecParams);
const _context = klona(context);

let baseRule;

Expand All @@ -50,9 +50,7 @@ module.exports.buildRule = ({ baseRuleName, description, omitFirstOption, getESL
baseRule = jsBaseRule;
}

if (eslintOption) {
_context.options.unshift(eslintOption);
}
const _context = eslintOption ? clone(context, { options: [ eslintOption, ...context.options ]}) : context;

return enabled ? baseRule.create(_context) : {};
},
Expand Down
80 changes: 80 additions & 0 deletions lib/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* eslint no-use-before-define: "off" */
/*
* This file is a modified version of klona/full (https://github.com/lukeed/klona).
* Licensed under MIT.
*/
"use strict";

function set(obj, key, val, overwrite) {
const overwriteKeys = Object.keys(overwrite);
if (overwriteKeys.includes(key)) {
obj[key] = overwrite[key];
return;
}

if (typeof val.value === "object") {
val.value = klona(val.value);
}
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
Object.defineProperty(obj, key, val);
} else {
obj[key] = val.value;
}
}

function klona(x, overwrite = {}) {
if (typeof x !== "object") {
return x;
}

const str = Object.prototype.toString.call(x);
let i = 0;
let k;
let list;
let tmp;

if (str === "[object Object]") {
tmp = Object.create(x.__proto__ || null);
} else if (str === "[object Array]") {
tmp = Array(x.length);
} else if (str === "[object Set]") {
tmp = new Set;
x.forEach((val) => {
tmp.add(klona(val));
});
} else if (str === "[object Map]") {
tmp = new Map;
x.forEach((val, key) => {
tmp.set(klona(key), klona(val));
});
} else if (str === "[object Date]") {
tmp = new Date(+x);
} else if (str === "[object RegExp]") {
tmp = new RegExp(x.source, x.flags);
} else if (str === "[object DataView]") {
tmp = new x.constructor(klona(x.buffer));
} else if (str === "[object ArrayBuffer]") {
tmp = x.slice(0);
} else if (str.slice(-6) === "Array]") {
// ArrayBuffer.isView(x)
// ~> `new` bcuz `Buffer.slice` => ref
tmp = new x.constructor(x);
}

if (tmp) {
for (list=Object.getOwnPropertySymbols(x); i < list.length; i++) {
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
}

for (i=0, list=Object.getOwnPropertyNames(x); i < list.length; i++) {
if (Object.hasOwnProperty.call(tmp, k=list[i]) && tmp[k] === x[k]) {
continue;
}
set(tmp, k, Object.getOwnPropertyDescriptor(x, k), overwrite);
}
}

return tmp || x;
}

module.exports = { clone: klona };
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-editorconfig",
"version": "4.0.2",
"version": "4.0.3",
"description": "An ESLint plugin to enforce EditorConfig rules",
"main": "main.js",
"scripts": {
Expand All @@ -24,15 +24,15 @@
},
"homepage": "https://github.com/phanect/eslint-plugin-editorconfig",
"dependencies": {
"editorconfig": "^0.15.0",
"eslint": "^8.0.1",
"editorconfig": "^1.0.2",
"eslint": "^8.40.0",
"klona": "^2.0.4"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint-config-phanective": "latest",
"mocha": "^9.1.3"
"mocha": "^10.2.0"
},
"engines": {
"node": ">=14",
Expand Down

0 comments on commit ed56efd

Please sign in to comment.