-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
jsonc/auto
rule works even in flat config (#298)
- Loading branch information
Showing
15 changed files
with
241 additions
and
37 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-jsonc": minor | ||
--- | ||
|
||
feat: `jsonc/auto` rule works even in flat config |
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
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
15 changes: 15 additions & 0 deletions
15
lib/utils/get-auto-jsonc-rules-config/calculate-config-for-file.ts
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,15 @@ | ||
import type { Linter } from "eslint"; | ||
// @ts-expect-error -- ignore | ||
import { createSyncFn } from "synckit"; | ||
|
||
const getSync = createSyncFn(require.resolve("./worker")); | ||
|
||
/** | ||
* Synchronously calculateConfigForFile | ||
*/ | ||
export function calculateConfigForFile( | ||
cwd: string, | ||
fileName: string, | ||
): Pick<Linter.Config, "rules"> { | ||
return getSync(cwd, fileName); | ||
} |
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
61 changes: 61 additions & 0 deletions
61
lib/utils/get-auto-jsonc-rules-config/should-use-flat-config.ts
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,61 @@ | ||
/** copied from https://github.com/eslint/eslint/blob/v8.56.0/lib/eslint/flat-eslint.js#L1119 */ | ||
|
||
import path from "path"; | ||
import fs from "fs"; | ||
|
||
const FLAT_CONFIG_FILENAME = "eslint.config.js"; | ||
/** | ||
* Returns whether flat config should be used. | ||
* @returns {Promise<boolean>} Whether flat config should be used. | ||
*/ | ||
export function shouldUseFlatConfig(cwd: string): boolean { | ||
// eslint-disable-next-line no-process-env -- ignore | ||
switch (process.env.ESLINT_USE_FLAT_CONFIG) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
default: | ||
// If neither explicitly enabled nor disabled, then use the presence | ||
// of a flat config file to determine enablement. | ||
return Boolean(findFlatConfigFile(cwd)); | ||
} | ||
} | ||
|
||
/** | ||
* Searches from the current working directory up until finding the | ||
* given flat config filename. | ||
* @param {string} cwd The current working directory to search from. | ||
* @returns {string|undefined} The filename if found or `undefined` if not. | ||
*/ | ||
function findFlatConfigFile(cwd: string) { | ||
return findUp(FLAT_CONFIG_FILENAME, { cwd }); | ||
} | ||
|
||
/** We used https://github.com/sindresorhus/find-up/blob/b733bb70d3aa21b22fa011be8089110d467c317f/index.js#L94 as a reference */ | ||
function findUp(name: string, options: { cwd: string }) { | ||
let directory = path.resolve(options.cwd); | ||
const { root } = path.parse(directory); | ||
const stopAt = path.resolve(directory, root); | ||
|
||
// eslint-disable-next-line no-constant-condition -- ignore | ||
while (true) { | ||
const target = path.resolve(directory, name); | ||
const stat = fs.existsSync(target) | ||
? fs.statSync(target, { | ||
throwIfNoEntry: false, | ||
}) | ||
: null; | ||
if (stat?.isFile()) { | ||
return target; | ||
} | ||
|
||
if (directory === stopAt) { | ||
break; | ||
} | ||
|
||
directory = path.dirname(directory); | ||
} | ||
|
||
return null; | ||
} |
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,10 @@ | ||
// @ts-expect-error -- ignore | ||
import { runAsWorker } from "synckit"; | ||
import { getESLint } from "eslint-compat-utils/eslint"; | ||
const ESLint = getESLint(); | ||
|
||
runAsWorker(async (cwd: string, fileName: string) => { | ||
const eslint = new ESLint({ cwd }); | ||
const config = await eslint.calculateConfigForFile(fileName); | ||
return { rules: config.rules }; | ||
}); |
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
30 changes: 30 additions & 0 deletions
30
tests/fixtures/integrations/eslint-plugin/test-auto-rule-with-flat-config01/eslint.config.js
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,30 @@ | ||
let plugin; | ||
try { | ||
plugin = require("../../../../../lib/index"); | ||
} catch (e) { | ||
// @ts-ignore -- ignore | ||
plugin = require("../../../../../dist/index"); | ||
} | ||
const parser = require("jsonc-eslint-parser"); | ||
|
||
module.exports = [ | ||
{ | ||
plugins: { | ||
jsonc: plugin, | ||
}, | ||
rules: { | ||
indent: "error", | ||
"no-unused-vars": "off", | ||
"no-multi-spaces": "error", | ||
"no-multiple-empty-lines": "error", | ||
"jsonc/auto": "error", | ||
"jsonc/no-comments": "error", | ||
}, | ||
}, | ||
{ | ||
files: ["**/*.json"], | ||
languageOptions: { | ||
parser, | ||
}, | ||
}, | ||
]; |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/integrations/eslint-plugin/test-auto-rule-with-flat-config01/src/test.js
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,3 @@ | ||
var a = { | ||
foo: 'bar' | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/integrations/eslint-plugin/test-auto-rule-with-flat-config01/src/test.json
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,3 @@ | ||
{ | ||
"foo": "bar" | ||
} |
Oops, something went wrong.