diff --git a/.gitignore b/.gitignore index 4eeb802..9815f2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules /yarn.lock -/package-lock.json \ No newline at end of file +/package-lock.json +/pnpm-lock.yaml \ No newline at end of file diff --git a/package.json b/package.json index 3209c00..07abb08 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" + } } diff --git a/src/get-eslint-modules.js b/src/get-eslint-modules.js index 5d0d13d..0314a4f 100644 --- a/src/get-eslint-modules.js +++ b/src/get-eslint-modules.js @@ -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; +}; diff --git a/src/only-warn.js b/src/only-warn.js index 07b2090..44de93b 100644 --- a/src/only-warn.js +++ b/src/only-warn.js @@ -1,24 +1,24 @@ -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; + }; } /** @@ -26,20 +26,20 @@ function patch(LinterPrototype) { */ 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 }; diff --git a/tests/only-warn.spec.js b/tests/only-warn.spec.js deleted file mode 100644 index 315b399..0000000 --- a/tests/only-warn.spec.js +++ /dev/null @@ -1,30 +0,0 @@ -const eslint = require('eslint') -const { disable, enable } = require('../src/only-warn') // apply patch - -describe('eslint-plugin-only-warn', () => { - const linter = new eslint.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) - }) -}) diff --git a/tests/only-warn.spec.ts b/tests/only-warn.spec.ts new file mode 100644 index 0000000..4cbf50d --- /dev/null +++ b/tests/only-warn.spec.ts @@ -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); + }); +});