Skip to content

Commit

Permalink
chore: Updated deps & applied formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bfanger committed Jan 23, 2024
1 parent f61ade5 commit 3036bd9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/yarn.lock
/package-lock.json
/package-lock.json
/pnpm-lock.yaml
35 changes: 15 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"name": "eslint-plugin-only-warn",
"version": "1.1.0",
"license": "MIT",
"description": "Downgrade errors to warnings",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin"
],
"main": "src/only-warn.js",
"scripts": {
"test": "npm run lint && jest",
"lint": "prettier --check --loglevel=warn src tests && eslint src tests",
"format": "prettier --write --loglevel=warn src tests && eslint --fix src tests",
"jest:watch": "jest --watch"
},
"author": "Bob Fanger",
"homepage": "https://github.com/bfanger/eslint-plugin-only-warn",
"repository": {
"type": "git",
"url": "https://github.com/bfanger/eslint-plugin-only-warn.git"
},
"prettier": {
"semi": false,
"singleQuote": true
"engines": {
"node": ">=18"
},
"main": "src/only-warn.js",
"scripts": {
"lint": "prettier --check src tests && eslint src tests",
"format": "prettier --write src tests && eslint --fix src tests",
"test": "vitest run",
"test:watch": "vitest"
},
"eslintConfig": {
"extends": "eslint:recommended",
Expand All @@ -38,15 +38,10 @@
"prettier/prettier": "error"
}
},
"dependencies": {},
"devDependencies": {
"eslint": "^7.31.0",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^27.0.6",
"prettier": "^2.3.2"
},
"engines": {
"node": ">=6"
},
"license": "MIT"
"eslint": "^8.56.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.4",
"vitest": "^1.2.1"
}
}
22 changes: 11 additions & 11 deletions src/get-eslint-modules.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const path = require('path')
const path = require("path");

const SEARCH_STR = `${path.sep}node_modules${path.sep}eslint${path.sep}`
const SEARCH_STR = `${path.sep}node_modules${path.sep}eslint${path.sep}`;

module.exports = function getEslintModules() {
const map = {}
const map = {};
Object.keys(require.cache).forEach((modulePath) => {
const pos = modulePath.indexOf(SEARCH_STR)
const pos = modulePath.indexOf(SEARCH_STR);
if (pos !== -1) {
const eslintPath = modulePath.substr(0, pos + SEARCH_STR.length)
const eslintPath = modulePath.substr(0, pos + SEARCH_STR.length);
if (!map[eslintPath]) {
map[eslintPath] = require(eslintPath)
map[eslintPath] = require(eslintPath);
}
}
})
});

let modules = Object.values(map)
let modules = Object.values(map);
if (modules.length === 0) {
modules = [require('eslint')]
modules = [require("eslint")];
}
return modules
}
return modules;
};
30 changes: 15 additions & 15 deletions src/only-warn.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
const getEslintModules = require('./get-eslint-modules')
const getEslintModules = require("./get-eslint-modules");

const unpatchedVerify = Symbol('verify')
const unpatchedVerify = Symbol("verify");

/**
* Patch the verify method and downgrade the errors to warnings.
*/
function patch(LinterPrototype) {
if (LinterPrototype[unpatchedVerify]) {
return
return;
}
LinterPrototype[unpatchedVerify] = LinterPrototype.verify
LinterPrototype[unpatchedVerify] = LinterPrototype.verify;
LinterPrototype.verify = function () {
const messages = LinterPrototype[unpatchedVerify].apply(this, arguments)
const messages = LinterPrototype[unpatchedVerify].apply(this, arguments);
messages.forEach((message) => {
if (!message.fatal && message.severity === 2) {
message.severity = 1
message.severity = 1;
}
})
return messages
}
});
return messages;
};
}

/**
* Remove the patch
*/
function unpatch(LinterPrototype) {
if (LinterPrototype[unpatchedVerify]) {
LinterPrototype.verify = LinterPrototype[unpatchedVerify]
delete LinterPrototype[unpatchedVerify]
LinterPrototype.verify = LinterPrototype[unpatchedVerify];
delete LinterPrototype[unpatchedVerify];
}
}

function enable() {
for (const eslint of getEslintModules()) {
patch((eslint.Linter && eslint.Linter.prototype) || eslint.linter)
patch((eslint.Linter && eslint.Linter.prototype) || eslint.linter);
}
}
function disable() {
for (const eslint of getEslintModules()) {
unpatch((eslint.Linter && eslint.Linter.prototype) || eslint.linter)
unpatch((eslint.Linter && eslint.Linter.prototype) || eslint.linter);
}
}
enable()
module.exports = { enable, disable }
enable();
module.exports = { enable, disable };
30 changes: 0 additions & 30 deletions tests/only-warn.spec.js

This file was deleted.

31 changes: 31 additions & 0 deletions tests/only-warn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Linter } from "eslint";
import { disable, enable } from "../src/only-warn"; // apply patch
import { describe, it, expect } from "vitest";

describe("eslint-plugin-only-warn", () => {
const linter = new Linter();
const config = {
rules: { semi: 2 }, // error on missing `;`
};
const sourceCode = "var foo";
it("should downgrade error(2) to warn(1)", () => {
const messages = linter.verify(sourceCode, config);
expect(messages[0].severity).toBe(1);
});

const sourceCodeFatalError = "var foo = ( => {}";
it("should not downgrade fatal error(2)", () => {
const messages = linter.verify(sourceCodeFatalError, config);
expect(messages[0].fatal).toBe(true);
expect(messages[0].severity).toBe(2);
});

it("can be temporarly disabled", () => {
disable();
const messages1 = linter.verify(sourceCode, config);
expect(messages1[0].severity).toBe(2);
enable();
const messages2 = linter.verify(sourceCode, config);
expect(messages2[0].severity).toBe(1);
});
});

0 comments on commit 3036bd9

Please sign in to comment.