@eslint/compat
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -0,1 +1,2 @@ | ||
export type FlatConfig = import("eslint").Linter.FlatConfig; | ||
export type FixupPluginDefinition = import("eslint").ESLint.Plugin; | ||
@@ -7,2 +8,13 @@ export type FixupRuleDefinition = import("eslint").Rule.RuleModule; | ||
/** | ||
* @fileoverview Ignore file utilities for the compat package. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
/** @typedef {import("eslint").Linter.FlatConfig} FlatConfig */ | ||
/** | ||
* Converts an ESLint ignore pattern to a minimatch pattern. | ||
* @param {string} pattern The .eslintignore or .gitignore pattern to convert. | ||
* @returns {string} The converted pattern. | ||
*/ | ||
export function convertIgnorePatternToMinimatch(pattern: string): string; | ||
/** | ||
* Takes the given configuration and creates a new configuration with all of the | ||
@@ -28,1 +40,8 @@ * rules wrapped to provide the missing methods on the `context` object. | ||
export function fixupRule(ruleDefinition: FixupRuleDefinition | FixupLegacyRuleDefinition): FixupRuleDefinition; | ||
/** | ||
* Reads an ignore file and returns an object with the ignore patterns. | ||
* @param {string} ignoreFilePath The absolute path to the ignore file. | ||
* @returns {FlatConfig} An object with an `ignores` property that is an array of ignore patterns. | ||
* @throws {Error} If the ignore file path is not an absolute path. | ||
*/ | ||
export function includeIgnoreFile(ignoreFilePath: string): FlatConfig; |
// @ts-self-types="./index.d.ts" | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
/** | ||
* @filedescription Object Schema | ||
* @filedescription Functions to fix up rules to provide missing methods on the `context` object. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
//----------------------------------------------------------------------------- | ||
// Imports | ||
//----------------------------------------------------------------------------- | ||
//----------------------------------------------------------------------------- | ||
// Types | ||
@@ -105,3 +104,3 @@ //----------------------------------------------------------------------------- | ||
const ruleCreate = context => { | ||
function ruleCreate(context) { | ||
// if getScope is already there then no need to create old methods | ||
@@ -165,2 +164,3 @@ if ("getScope" in context) { | ||
if (methodName.startsWith("on")) { | ||
// eslint-disable-next-line no-loop-func -- intentionally updating shared `currentNode` variable | ||
visitor[methodName] = (...args) => { | ||
@@ -176,2 +176,3 @@ currentNode = | ||
// eslint-disable-next-line no-loop-func -- intentionally updating shared `currentNode` variable | ||
visitor[methodName] = (...args) => { | ||
@@ -185,3 +186,3 @@ currentNode = args[0]; | ||
return visitor; | ||
}; | ||
} | ||
@@ -246,9 +247,9 @@ const newRuleDefinition = { | ||
return configs.map(config => { | ||
if (!config.plugins) { | ||
return config; | ||
return configs.map(configItem => { | ||
if (!configItem.plugins) { | ||
return configItem; | ||
} | ||
const newPlugins = Object.fromEntries( | ||
Object.entries(config.plugins).map(([pluginName, plugin]) => [ | ||
Object.entries(configItem.plugins).map(([pluginName, plugin]) => [ | ||
pluginName, | ||
@@ -260,3 +261,3 @@ fixupPluginRules(plugin), | ||
return { | ||
...config, | ||
...configItem, | ||
plugins: newPlugins, | ||
@@ -267,2 +268,71 @@ }; | ||
export { fixupConfigRules, fixupPluginRules, fixupRule }; | ||
/** | ||
* @fileoverview Ignore file utilities for the compat package. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
//----------------------------------------------------------------------------- | ||
// Types | ||
//----------------------------------------------------------------------------- | ||
/** @typedef {import("eslint").Linter.FlatConfig} FlatConfig */ | ||
//----------------------------------------------------------------------------- | ||
// Exports | ||
//----------------------------------------------------------------------------- | ||
/** | ||
* Converts an ESLint ignore pattern to a minimatch pattern. | ||
* @param {string} pattern The .eslintignore or .gitignore pattern to convert. | ||
* @returns {string} The converted pattern. | ||
*/ | ||
function convertIgnorePatternToMinimatch(pattern) { | ||
const isNegated = pattern.startsWith("!"); | ||
const negatedPrefix = isNegated ? "!" : ""; | ||
const patternToTest = (isNegated ? pattern.slice(1) : pattern).trimEnd(); | ||
// special cases | ||
if (["", "**", "/**", "**/"].includes(patternToTest)) { | ||
return `${negatedPrefix}${patternToTest}`; | ||
} | ||
const firstIndexOfSlash = patternToTest.indexOf("/"); | ||
const matchEverywherePrefix = | ||
firstIndexOfSlash < 0 || firstIndexOfSlash === patternToTest.length - 1 | ||
? "**/" | ||
: ""; | ||
const patternWithoutLeadingSlash = | ||
firstIndexOfSlash === 0 ? patternToTest.slice(1) : patternToTest; | ||
const matchInsideSuffix = patternToTest.endsWith("/**") ? "/*" : ""; | ||
return `${negatedPrefix}${matchEverywherePrefix}${patternWithoutLeadingSlash}${matchInsideSuffix}`; | ||
} | ||
/** | ||
* Reads an ignore file and returns an object with the ignore patterns. | ||
* @param {string} ignoreFilePath The absolute path to the ignore file. | ||
* @returns {FlatConfig} An object with an `ignores` property that is an array of ignore patterns. | ||
* @throws {Error} If the ignore file path is not an absolute path. | ||
*/ | ||
function includeIgnoreFile(ignoreFilePath) { | ||
if (!path.isAbsolute(ignoreFilePath)) { | ||
throw new Error("The ignore file location must be an absolute path."); | ||
} | ||
const ignoreFile = fs.readFileSync(ignoreFilePath, "utf8"); | ||
const lines = ignoreFile.split(/\r?\n/u); | ||
return { | ||
name: "Imported .gitignore patterns", | ||
ignores: lines | ||
.map(line => line.trim()) | ||
.filter(line => line && !line.startsWith("#")) | ||
.map(convertIgnorePatternToMinimatch), | ||
}; | ||
} | ||
export { convertIgnorePatternToMinimatch, fixupConfigRules, fixupPluginRules, fixupRule, includeIgnoreFile }; |
{ | ||
"name": "@eslint/compat", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Compatibility utilities for ESLint", | ||
"type": "module", | ||
"main": "dist/esm/index.js", | ||
"types": "dist/esm/index.d.ts", | ||
"exports": { | ||
@@ -29,3 +31,4 @@ "require": { | ||
"test:jsr": "npx jsr@latest publish --dry-run", | ||
"test": "mocha tests/*.js" | ||
"test": "mocha tests/*.js", | ||
"test:coverage": "c8 npm test" | ||
}, | ||
@@ -50,2 +53,3 @@ "repository": { | ||
"@types/eslint": "^8.56.10", | ||
"c8": "^9.1.0", | ||
"eslint": "^9.0.0", | ||
@@ -52,0 +56,0 @@ "mocha": "^10.4.0", |
@@ -36,2 +36,3 @@ # ESLint Compatibility Utilities | ||
- `fixupConfigRules(configs)` - wraps all plugins found in an array of config objects using `fixupPluginRules()` | ||
- `includeIgnoreFile(path)` - reads an ignore file (like `.gitignore`) and converts the patterns into the correct format for the config file | ||
@@ -146,4 +147,57 @@ ### Fixing Rules | ||
### Including Ignore Files | ||
If you were using an alternate ignore file in ESLint v8.x, such as using `--ignore-path .gitignore` on the command line, you can include those patterns programmatically in your config file using the `includeIgnoreFile()` function. For example: | ||
```js | ||
// eslint.config.js - ESM example | ||
import { includeIgnoreFile } from "@eslint/compat"; | ||
import path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const gitignorePath = path.resolve(__dirname, ".gitignore"); | ||
export default [ | ||
includeIgnoreFile(gitignorePath), | ||
{ | ||
// your overrides | ||
}, | ||
]; | ||
``` | ||
Or in CommonJS: | ||
```js | ||
// eslint.config.js - CommonJS example | ||
const { includeIgnoreFile } = require("@eslint/compat"); | ||
const path = require("node:path"); | ||
const gitignorePath = path.resolve(__dirname, ".gitignore"); | ||
module.exports = [ | ||
includeIgnoreFile(gitignorePath), | ||
{ | ||
// your overrides | ||
}, | ||
]; | ||
``` | ||
**Limitation:** This works without modification when the ignore file is in the same directory as your config file. If the ignore file is in a different directory, you may need to modify the patterns manually. | ||
## License | ||
Apache 2.0 | ||
## Sponsors | ||
The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. [Become a Sponsor](https://eslint.org/donate) to get your logo on our README and website. | ||
<!-- NOTE: This section is autogenerated. Do not manually edit.--> | ||
<!--sponsorsstart--> | ||
<h3>Platinum Sponsors</h3> | ||
<p><a href="https://automattic.com"><img src="https://images.opencollective.com/automattic/d0ef3e1/logo.png" alt="Automattic" height="undefined"></a></p><h3>Gold Sponsors</h3> | ||
<p><a href="#"><img src="https://images.opencollective.com/guest-bf377e88/avatar.png" alt="Eli Schleifer" height="96"></a> <a href="https://engineering.salesforce.com"><img src="https://images.opencollective.com/salesforce/ca8f997/logo.png" alt="Salesforce" height="96"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="96"></a></p><h3>Silver Sponsors</h3> | ||
<p><a href="https://www.jetbrains.com/"><img src="https://images.opencollective.com/jetbrains/fe76f99/logo.png" alt="JetBrains" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a> <a href="https://www.workleap.com"><img src="https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4" alt="Workleap" height="64"></a></p><h3>Bronze Sponsors</h3> | ||
<p><a href="https://www.notion.so"><img src="https://images.opencollective.com/notion/bf3b117/logo.png" alt="notion" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104?v=4" alt="Nx" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a></p> | ||
<!--sponsorsend--> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45680
597
202
6