Skip to content

Commit 818234c

Browse files
ota-meshiJounQin
andauthored
Fix wrong auto-config when nesting config with markdown in jsonc/auto rule (#180)
* Fix wrong auto-config when nesting config with markdown in `jsonc/auto` rule * fix * Update tests-integrations/fixtures/eslint-plugin-markdown/package.json Co-authored-by: JounQin <[email protected]> * add test for json in md in md * fix lint error Co-authored-by: JounQin <[email protected]>
1 parent 3e2351d commit 818234c

File tree

19 files changed

+298
-68
lines changed

19 files changed

+298
-68
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
/tests/fixtures/**/*.json
1111
/tests-integrations/fixtures/eslint-plugin-markdown/test.md
1212
/tests-integrations/fixtures/vue-eslint-parser-option/test.json
13+
/tests-integrations/fixtures/eslint-plugin-markdown-nest/test.md
14+
/tests-integrations/fixtures/eslint-plugin-markdown-nest-for-v6/test.md
1315
/assets
1416
/index.d.ts
1517
!/.github

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ dist
106106
/typings/eslint/lib
107107
/dist-ts
108108
/index.d.ts
109+
/tests-integrations/fixtures/**/package-lock.json
109110

110111
# docs
111112
/404.html

lib/utils/get-auto-jsonc-rules-config.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Linter } from "eslint"
22
import { existsSync, statSync } from "fs"
3-
import { dirname, resolve } from "path"
3+
import { dirname, extname, resolve } from "path"
44
import type { RuleModule } from "../types"
55

66
let configResolver: (filePath: string) => Linter.Config, ruleNames: Set<string>
@@ -37,7 +37,6 @@ function getConfigResolver(): (filePath: string) => Linter.Config {
3737
),
3838
eslintAllPath: require.resolve("../../conf/eslint-all.js"),
3939
})
40-
4140
return (configResolver = (filePath: string) => {
4241
const absolutePath = resolve(process.cwd(), filePath)
4342
return configArrayFactory
@@ -49,12 +48,29 @@ function getConfigResolver(): (filePath: string) => Linter.Config {
4948
// ignore
5049
}
5150
try {
51+
// For ESLint v6
52+
5253
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special
5354
const eslint = require("eslint")
5455
const engine = new eslint.CLIEngine({})
5556
engine.addPlugin("eslint-plugin-jsonc", plugin)
5657
return (configResolver = (filePath) => {
57-
return engine.getConfigForFile(filePath)
58+
// Adjust the file name to avoid a crash.
59+
// https://github.com/ota-meshi/eslint-plugin-jsonc/issues/28
60+
let targetFilePath = filePath
61+
const ext = extname(filePath)
62+
while (!isValidFilename(targetFilePath)) {
63+
const dir = dirname(targetFilePath)
64+
if (dir === targetFilePath) {
65+
return {}
66+
}
67+
targetFilePath = dir
68+
if (ext && extname(targetFilePath) !== ext) {
69+
targetFilePath += ext
70+
}
71+
}
72+
73+
return engine.getConfigForFile(targetFilePath)
5874
})
5975
} catch {
6076
// ignore
@@ -64,7 +80,7 @@ function getConfigResolver(): (filePath: string) => Linter.Config {
6480
}
6581

6682
/**
67-
* Checks if the given file name can get the configuration.
83+
* Checks if the given file name can get the configuration (for ESLint v6).
6884
*/
6985
function isValidFilename(filename: string) {
7086
const dir = dirname(filename)
@@ -83,14 +99,6 @@ function isValidFilename(filename: string) {
8399
* @param filename
84100
*/
85101
function getConfig(filename: string): Linter.Config {
86-
while (!isValidFilename(filename)) {
87-
const dir = dirname(filename)
88-
if (dir === filename) {
89-
return {}
90-
}
91-
// eslint-disable-next-line no-param-reassign -- ignore
92-
filename = dir
93-
}
94102
return getConfigResolver()(filename)
95103
}
96104

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"test": "npm run test:base",
3030
"test:nyc": "nyc --reporter=lcov npm run test:base",
3131
"test:debug": "mocha --require ts-node/register/transpile-only \"tests/lib/**/*.ts\" --reporter dot",
32-
"pretest:integrations": "npm run build:ts",
32+
"pretest:integrations": "npm run build:ts && npm pack",
3333
"test:integrations": "mocha --require ts-node/register \"tests-integrations/lib/**/*.ts\" --reporter dot --timeout 120000",
3434
"update": "ts-node --transpile-only ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
3535
"update-only": "ts-node --transpile-only ./tools/update.ts",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"root": true,
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 2015
6+
},
7+
"extends": [
8+
"plugin:markdown/recommended"
9+
],
10+
"rules": {
11+
"quotes": ["error", "single"]
12+
},
13+
"overrides": [
14+
{
15+
"files": ["*.json"],
16+
"extends": [
17+
"plugin:jsonc/recommended-with-json"
18+
],
19+
"rules": { "jsonc/auto": "error" }
20+
}
21+
]
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "test-for-eslint-plugin-jsonc-with-md-nest",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"eslint": "^6.8.0",
14+
"eslint-plugin-jsonc": "file:../../../eslint-plugin-jsonc-2.3.0.tgz",
15+
"eslint-plugin-markdown": "^2.2.1"
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```json
2+
{
3+
"quotes": 'value'
4+
}
5+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"root": true,
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 2015
6+
},
7+
"extends": [
8+
"plugin:markdown/recommended"
9+
],
10+
"rules": {
11+
"quotes": ["error", "single"]
12+
},
13+
"overrides": [
14+
{
15+
"files": ["*.json"],
16+
"extends": [
17+
"plugin:jsonc/recommended-with-json"
18+
],
19+
"rules": { "jsonc/auto": "error" }
20+
}
21+
]
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "test-for-eslint-plugin-jsonc-with-md-nest",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"eslint": "^8.19.0",
14+
"eslint-plugin-jsonc": "file:../../../eslint-plugin-jsonc-2.3.0.tgz",
15+
"eslint-plugin-markdown": "^2.2.1"
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```json
2+
{
3+
"quotes": 'value'
4+
}
5+
```
6+
7+
````md
8+
```json
9+
{
10+
"quotes": 'value'
11+
}
12+
```
13+
````

0 commit comments

Comments
 (0)