-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Updated deps & applied formatting
- Loading branch information
Showing
6 changed files
with
74 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |