Skip to content

Commit 241db30

Browse files
authored
Merge pull request #141 from Quramy/fix/eslint-recommended-config-path
Fix/eslint recommended config path
2 parents c36c473 + 10f06e7 commit 241db30

File tree

10 files changed

+89
-5
lines changed

10 files changed

+89
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es6: true,
5+
node: true,
6+
},
7+
parser: "@typescript-eslint/parser",
8+
extends: ["eslint:recommended"],
9+
parserOptions: {
10+
project: "../tsconfig.json",
11+
},
12+
rules: {
13+
semi: 2,
14+
},
15+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debugger;

e2e/projects/ts-eslint-plugin/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
node: true,
66
},
77
extends: [
8-
// "plugin:@typescript-eslint/recommended",
8+
"plugin:@typescript-eslint/recommended",
99
],
1010
parser: "@typescript-eslint/parser",
1111
parserOptions: {
@@ -16,6 +16,7 @@ module.exports = {
1616
"@typescript-eslint",
1717
],
1818
rules: {
19+
"@typescript-eslint/explicit-module-boundary-types": "off",
1920
"@typescript-eslint/no-unused-vars": "error",
2021
},
2122
};

e2e/test/__snapshots__/e2e.test.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,29 @@ Object {
110110
"type": "event",
111111
}
112112
`;
113+
114+
exports[`LanguageService plugin #getSemanticDiagnostics should return ESLint error when the project uses @typescript-eslint/parser and be configured with 'eslint:recommended' 1`] = `
115+
Object {
116+
"body": Object {
117+
"diagnostics": Array [
118+
Object {
119+
"category": "error",
120+
"code": 30010,
121+
"end": Object {
122+
"line": 1,
123+
"offset": 10,
124+
},
125+
"start": Object {
126+
"line": 1,
127+
"offset": 1,
128+
},
129+
"text": "[no-debugger] Unexpected 'debugger' statement.",
130+
},
131+
],
132+
"file": "<file>",
133+
},
134+
"event": "semanticDiag",
135+
"seq": 0,
136+
"type": "event",
137+
}
138+
`;

e2e/test/e2e.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ describe("LanguageService plugin", () => {
6565
expect(maskFileNameForDiagnostics(found)).toMatchSnapshot();
6666
});
6767

68+
it("should return ESLint error when the project uses @typescript-eslint/parser and be configured with 'eslint:recommended'", async () => {
69+
server = createServer({ projectPath: path.resolve(__dirname, "../projects/simple") });
70+
const { file, fileContent } = server.readFile("./eslint-recommended/main.ts");
71+
server.send({ command: "open", arguments: { file, fileContent, scriptKindName: "TS" } });
72+
await server.waitEvent("projectLoadingFinish");
73+
server.send({ command: "geterr", arguments: { files: [file], delay: 0 } });
74+
await server.waitEvent("semanticDiag");
75+
const found = findEventResponse(server.responses, "semanticDiag");
76+
if (!found) {
77+
throw new assert.AssertionError();
78+
}
79+
expect(maskFileNameForDiagnostics(found)).toMatchSnapshot();
80+
});
81+
6882
it("should return ESLint error when the project is configured with ESLint plugins", async () => {
6983
server = createServer({ projectPath: path.resolve(__dirname, "../projects/ts-eslint-plugin") });
7084
const { file, fileContent } = server.readFile("./main.ts");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "TypeScript language service plugin for ESLint",
55
"main": "lib/index.js",
66
"scripts": {
7-
"clean": "rimraf local lib",
7+
"clean": "rimraf local lib \"e2e/**/*.log\"",
88
"prebuild": "rimraf node_modules/@types/eslint/node_modules/@types/estree",
99
"build": "tsc -p tsconfig.build.json",
1010
"build:local": "tsc -p tsconfig.build.json --outDir local",

src/eslint-adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ export class ESLintAdapter {
144144

145145
return [...original, ...translateToDiagnosticsFromESLintResult(eslintResult, sourceFile)];
146146
} catch (error) {
147-
this.logger(error.message ? error.message : "unknow error");
147+
if (error instanceof Error) {
148+
this.logger(error.message);
149+
if (error.stack) {
150+
this.logger(error.stack);
151+
}
152+
} else {
153+
this.logger(error);
154+
}
148155
return original;
149156
}
150157
}

src/eslint-config-provider.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type ConfigProviderHost = {
2828

2929
export type ESLintConfigProviderOptions = {
3030
host: ConfigProviderHost;
31+
log?: (msg: string) => void;
3132
directoriesToWatch: string[];
3233
};
3334

@@ -47,10 +48,17 @@ export interface ConfigProvider {
4748
export class ESLintConfigProvider implements ConfigProvider {
4849
private readonly host: ConfigProviderHost;
4950
private readonly factory: CascadingConfigArrayFactory;
51+
private readonly log: (msg: string) => void;
5052

51-
public constructor({ host, directoriesToWatch }: ESLintConfigProviderOptions) {
53+
public constructor({ host, log = () => {}, directoriesToWatch }: ESLintConfigProviderOptions) {
5254
this.host = host;
53-
this.factory = new (getFactroyClass())();
55+
this.log = log;
56+
const eslintRecommendedPath = this.resolveESLintIntrinsicConfigPath("eslint-recommended");
57+
const eslintAllPath = this.resolveESLintIntrinsicConfigPath("eslint-all");
58+
this.factory = new (getFactroyClass())({
59+
eslintAllPath,
60+
eslintRecommendedPath,
61+
});
5462

5563
directoriesToWatch.forEach(directory => {
5664
ESLINTRC_SUFFIX_LIST.map(suffix => path.resolve(directory, suffix)).forEach(eslintrcFilepath => {
@@ -66,4 +74,14 @@ export class ESLintConfigProvider implements ConfigProvider {
6674
public getConfigForFile(fileName: string) {
6775
return this.factory.getConfigArrayForFile(fileName).extractConfig(fileName);
6876
}
77+
78+
private resolveESLintIntrinsicConfigPath(name: "eslint-all" | "eslint-recommended") {
79+
let ret: string | undefined = undefined;
80+
try {
81+
ret = require.resolve(`eslint/conf/${name}`);
82+
} catch (e) {
83+
this.log(e);
84+
}
85+
return ret;
86+
}
6987
}

src/plugin-module-factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
3939

4040
const configProvider = new ESLintConfigProvider({
4141
directoriesToWatch: watchDirs,
42+
log: logger,
4243
host: serverHost,
4344
});
4445

src/typedef/eslint-internal.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ declare module "@eslint/eslintrc/lib/cascading-config-array-factory" {
4343
*
4444
*/
4545
export class CascadingConfigArrayFactory {
46+
constructor(options: { eslintRecommendedPath?: string | undefined; eslintAllPath?: string | undefined });
4647
public getConfigArrayForFile(filename: string): ConfigArray<InternalConfig>;
4748
public clearCache(): void;
4849
}

0 commit comments

Comments
 (0)