Skip to content

Commit b1c8391

Browse files
authored
Fix crash when used with eslint-plugin-markdown. (#29)
* Fix crash when used with eslint-plugin-markdown. * fix * update
1 parent f8bef3c commit b1c8391

File tree

10 files changed

+151
-3
lines changed

10 files changed

+151
-3
lines changed

.github/workflows/NodeCI.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,42 @@ jobs:
2020
run: npm run build
2121
- name: Lint
2222
run: npm run lint
23+
test-for-win-integrations:
24+
runs-on: windows-latest
25+
steps:
26+
- uses: actions/checkout@v2
27+
- name: Use Node.js 14
28+
uses: actions/setup-node@v1
29+
with:
30+
node-version: 14
31+
- name: Install Packages
32+
run: npm install
33+
- name: Test
34+
run: npm run test:integrations
35+
test-for-mac-integrations:
36+
runs-on: macos-latest
37+
steps:
38+
- uses: actions/checkout@v2
39+
- name: Use Node.js 14
40+
uses: actions/setup-node@v1
41+
with:
42+
node-version: 14
43+
- name: Install Packages
44+
run: npm install
45+
- name: Test
46+
run: npm run test:integrations
47+
test-for-ubuntu-integrations:
48+
runs-on: ubuntu-latest
49+
steps:
50+
- uses: actions/checkout@v2
51+
- name: Use Node.js 14
52+
uses: actions/setup-node@v1
53+
with:
54+
node-version: 14
55+
- name: Install Packages
56+
run: npm install
57+
- name: Test
58+
run: npm run test:integrations
2359
test:
2460
runs-on: ubuntu-latest
2561
strategy:

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Linter, CLIEngine } from "eslint"
2+
import { existsSync, statSync } from "fs"
3+
import { dirname } from "path"
24
import type { RuleModule } from "../types"
35

46
let engine: CLIEngine, ruleNames: Set<string>
@@ -17,11 +19,34 @@ function getCLIEngine() {
1719
return engine
1820
}
1921

22+
/**
23+
* Checks if the given file name can get the configuration.
24+
*/
25+
function isValidFilename(filename: string) {
26+
const dir = dirname(filename)
27+
if (existsSync(dir) && statSync(dir).isDirectory()) {
28+
if (existsSync(filename) && statSync(filename).isDirectory()) {
29+
return false
30+
}
31+
return true
32+
}
33+
34+
return false
35+
}
36+
2037
/**
2138
* Get config for the given filename
2239
* @param filename
2340
*/
24-
function getConfig(filename: string) {
41+
function getConfig(filename: string): Linter.Config {
42+
while (!isValidFilename(filename)) {
43+
const dir = dirname(filename)
44+
if (dir === filename) {
45+
return {}
46+
}
47+
// eslint-disable-next-line no-param-reassign -- ignore
48+
filename = dir
49+
}
2550
return getCLIEngine().getConfigForFile(filename)
2651
}
2752

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"test": "npm run test:base",
2626
"test:nyc": "nyc --reporter=lcov npm run test:base",
2727
"test:debug": "mocha --require ts-node/register --inspect \"tests/lib/**/*.ts\" --reporter dot",
28+
"pretest:integrations": "npm run build:ts",
29+
"test:integrations": "mocha --require ts-node/register \"tests-integrations/lib/**/*.ts\" --reporter dot --timeout 60000",
2830
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
2931
"new": "ts-node ./tools/new-rule.ts",
3032
"predocs:watch": "npm run build:ts",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"root": true,
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 2015
6+
},
7+
"extends": [
8+
"plugin:markdown/recommended",
9+
"plugin:jsonc/recommended-with-json"
10+
],
11+
"rules": {
12+
"array-bracket-newline": "error",
13+
"jsonc/auto": "error"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
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",
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": "^7.17.0",
14+
"eslint-plugin-markdown": "^2.0.0-0",
15+
"eslint-plugin-jsonc": "file:../../../"
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
```json
3+
{
4+
"arr": ["a", "b"
5+
]
6+
}
7+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cp from "child_process"
2+
import assert from "assert"
3+
import path from "path"
4+
import plugin from "../../lib/index"
5+
6+
// -----------------------------------------------------------------------------
7+
// Tests
8+
// -----------------------------------------------------------------------------
9+
10+
const TEST_CWD = path.join(__dirname, "../fixtures/eslint-plugin-markdown")
11+
12+
const ESLINT = path.join(TEST_CWD, `./node_modules/eslint`)
13+
14+
describe("Integration with eslint-plugin-markdown", () => {
15+
let originalCwd: string
16+
17+
before(() => {
18+
originalCwd = process.cwd()
19+
process.chdir(TEST_CWD)
20+
cp.execSync("npm i", { stdio: "inherit" })
21+
})
22+
after(() => {
23+
process.chdir(originalCwd)
24+
})
25+
26+
it("should lint without errors", () => {
27+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
28+
// @ts-ignore
29+
const eslint = require(ESLINT)
30+
/* eslint-enable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
31+
const engine = new eslint.CLIEngine({
32+
cwd: TEST_CWD,
33+
extensions: [".js", ".json", ".yaml"],
34+
})
35+
engine.addPlugin("eslint-plugin-jsonc", plugin)
36+
const r = engine.executeOnFiles(["./test.md"])
37+
38+
assert.strictEqual(r.results.length, 1)
39+
assert.strictEqual(r.results[0].messages.length, 1)
40+
assert.strictEqual(
41+
r.results[0].messages[0].message,
42+
"[jsonc/array-bracket-newline] There should be no linebreak before ']'.",
43+
)
44+
})
45+
})

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "./tsconfig.json",
3-
"exclude": ["tests/**/*", "tools/**/*", "typings/**/*",],
3+
"exclude": ["tests/**/*", "tests-integrations/lib/**/*", "tools/**/*", "typings/**/*",],
44
"compilerOptions": {
55
"removeComments": true, /* Do not emit comments to output. */
66
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
},
2020
"esModuleInterop": true
2121
},
22-
"include": ["lib/**/*", "tests/lib/**/*", "tools/**/*", "typings/**/*"],
22+
"include": ["lib/**/*", "tests/lib/**/*", "tests-integrations/lib/**/*", "tools/**/*", "typings/**/*"],
2323
"exclude": ["dist/**/*"]
2424
}

0 commit comments

Comments
 (0)