Skip to content

Commit

Permalink
Merge pull request #141 from Quramy/fix/eslint-recommended-config-path
Browse files Browse the repository at this point in the history
Fix/eslint recommended config path
  • Loading branch information
Quramy authored Dec 30, 2020
2 parents c36c473 + 10f06e7 commit 241db30
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 5 deletions.
15 changes: 15 additions & 0 deletions e2e/projects/simple/eslint-recommended/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
parser: "@typescript-eslint/parser",
extends: ["eslint:recommended"],
parserOptions: {
project: "../tsconfig.json",
},
rules: {
semi: 2,
},
};
1 change: 1 addition & 0 deletions e2e/projects/simple/eslint-recommended/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
3 changes: 2 additions & 1 deletion e2e/projects/ts-eslint-plugin/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
node: true,
},
extends: [
// "plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
Expand All @@ -16,6 +16,7 @@ module.exports = {
"@typescript-eslint",
],
rules: {
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-unused-vars": "error",
},
};
Expand Down
26 changes: 26 additions & 0 deletions e2e/test/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,29 @@ Object {
"type": "event",
}
`;

exports[`LanguageService plugin #getSemanticDiagnostics should return ESLint error when the project uses @typescript-eslint/parser and be configured with 'eslint:recommended' 1`] = `
Object {
"body": Object {
"diagnostics": Array [
Object {
"category": "error",
"code": 30010,
"end": Object {
"line": 1,
"offset": 10,
},
"start": Object {
"line": 1,
"offset": 1,
},
"text": "[no-debugger] Unexpected 'debugger' statement.",
},
],
"file": "<file>",
},
"event": "semanticDiag",
"seq": 0,
"type": "event",
}
`;
14 changes: 14 additions & 0 deletions e2e/test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ describe("LanguageService plugin", () => {
expect(maskFileNameForDiagnostics(found)).toMatchSnapshot();
});

it("should return ESLint error when the project uses @typescript-eslint/parser and be configured with 'eslint:recommended'", async () => {
server = createServer({ projectPath: path.resolve(__dirname, "../projects/simple") });
const { file, fileContent } = server.readFile("./eslint-recommended/main.ts");
server.send({ command: "open", arguments: { file, fileContent, scriptKindName: "TS" } });
await server.waitEvent("projectLoadingFinish");
server.send({ command: "geterr", arguments: { files: [file], delay: 0 } });
await server.waitEvent("semanticDiag");
const found = findEventResponse(server.responses, "semanticDiag");
if (!found) {
throw new assert.AssertionError();
}
expect(maskFileNameForDiagnostics(found)).toMatchSnapshot();
});

it("should return ESLint error when the project is configured with ESLint plugins", async () => {
server = createServer({ projectPath: path.resolve(__dirname, "../projects/ts-eslint-plugin") });
const { file, fileContent } = server.readFile("./main.ts");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "TypeScript language service plugin for ESLint",
"main": "lib/index.js",
"scripts": {
"clean": "rimraf local lib",
"clean": "rimraf local lib \"e2e/**/*.log\"",
"prebuild": "rimraf node_modules/@types/eslint/node_modules/@types/estree",
"build": "tsc -p tsconfig.build.json",
"build:local": "tsc -p tsconfig.build.json --outDir local",
Expand Down
9 changes: 8 additions & 1 deletion src/eslint-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ export class ESLintAdapter {

return [...original, ...translateToDiagnosticsFromESLintResult(eslintResult, sourceFile)];
} catch (error) {
this.logger(error.message ? error.message : "unknow error");
if (error instanceof Error) {
this.logger(error.message);
if (error.stack) {
this.logger(error.stack);
}
} else {
this.logger(error);
}
return original;
}
}
Expand Down
22 changes: 20 additions & 2 deletions src/eslint-config-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ConfigProviderHost = {

export type ESLintConfigProviderOptions = {
host: ConfigProviderHost;
log?: (msg: string) => void;
directoriesToWatch: string[];
};

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

public constructor({ host, directoriesToWatch }: ESLintConfigProviderOptions) {
public constructor({ host, log = () => {}, directoriesToWatch }: ESLintConfigProviderOptions) {
this.host = host;
this.factory = new (getFactroyClass())();
this.log = log;
const eslintRecommendedPath = this.resolveESLintIntrinsicConfigPath("eslint-recommended");
const eslintAllPath = this.resolveESLintIntrinsicConfigPath("eslint-all");
this.factory = new (getFactroyClass())({
eslintAllPath,
eslintRecommendedPath,
});

directoriesToWatch.forEach(directory => {
ESLINTRC_SUFFIX_LIST.map(suffix => path.resolve(directory, suffix)).forEach(eslintrcFilepath => {
Expand All @@ -66,4 +74,14 @@ export class ESLintConfigProvider implements ConfigProvider {
public getConfigForFile(fileName: string) {
return this.factory.getConfigArrayForFile(fileName).extractConfig(fileName);
}

private resolveESLintIntrinsicConfigPath(name: "eslint-all" | "eslint-recommended") {
let ret: string | undefined = undefined;
try {
ret = require.resolve(`eslint/conf/${name}`);
} catch (e) {
this.log(e);
}
return ret;
}
}
1 change: 1 addition & 0 deletions src/plugin-module-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function create(info: ts.server.PluginCreateInfo): ts.LanguageService {

const configProvider = new ESLintConfigProvider({
directoriesToWatch: watchDirs,
log: logger,
host: serverHost,
});

Expand Down
1 change: 1 addition & 0 deletions src/typedef/eslint-internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ declare module "@eslint/eslintrc/lib/cascading-config-array-factory" {
*
*/
export class CascadingConfigArrayFactory {
constructor(options: { eslintRecommendedPath?: string | undefined; eslintAllPath?: string | undefined });
public getConfigArrayForFile(filename: string): ConfigArray<InternalConfig>;
public clearCache(): void;
}
Expand Down

0 comments on commit 241db30

Please sign in to comment.