Skip to content

Commit 9633967

Browse files
committed
eslint: add support for standard-with-typescript ruleset
1 parent 4f5e4d3 commit 9633967

File tree

6 files changed

+105
-41
lines changed

6 files changed

+105
-41
lines changed

lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,50 @@
11
const {ESLint} = require("eslint");
22

33
app.post("/eslint/format", async (req, res) => {
4+
try {
5+
const format_data = req.body;
46

5-
const format_data = req.body;
6-
7-
const ESLintOverrideConfig = format_data.eslint_override_config;
7+
const ESLintOverrideConfig = format_data.eslint_override_config;
88

9-
const ESLintOverrideConfigFile = format_data.eslint_override_config_file;
9+
const ESLintOverrideConfigFile = format_data.eslint_override_config_file;
1010

11-
if (!ESLintOverrideConfig && !ESLintOverrideConfigFile) {
12-
res.status(501).send("Error while formatting: No config provided");
13-
return;
14-
}
15-
16-
const filePath = format_data.file_path;
11+
if (!ESLintOverrideConfig && !ESLintOverrideConfigFile) {
12+
res.status(501).send("Error while formatting: No config provided");
13+
return;
14+
}
1715

18-
if (!filePath) {
19-
res.status(501).send("Error while formatting: No file path provided");
20-
return;
21-
}
16+
const filePath = format_data.file_path;
2217

23-
const ESLintOptions = {
24-
fix: true,
25-
useEslintrc: false, // would result in (gradle) cache issues
26-
resolvePluginsRelativeTo: format_data.node_modules_dir
27-
// baseConfig: {
28-
// parserOptions: {
29-
// tsconfigRootDir: '../../',
30-
// }
31-
// }
32-
};
18+
if (!filePath) {
19+
res.status(501).send("Error while formatting: No file path provided");
20+
return;
21+
}
3322

34-
if (format_data.ts_config_root_dir) {
35-
ESLintOptions.baseConfig = {
36-
parserOptions: {
37-
tsconfigRootDir: format_data.ts_config_root_dir
38-
}
23+
const ESLintOptions = {
24+
fix: true,
25+
useEslintrc: false, // would result in (gradle) cache issues
26+
resolvePluginsRelativeTo: format_data.node_modules_dir
3927
};
40-
// res.status(501).send("Resolved ts config root dir: " + format_data.ts_config_root_dir);
41-
}
4228

29+
if (format_data.ts_config_root_dir) {
30+
ESLintOptions.baseConfig = {
31+
parserOptions: {
32+
tsconfigRootDir: format_data.ts_config_root_dir
33+
}
34+
};
35+
}
4336

44-
if (ESLintOverrideConfigFile) {
45-
ESLintOptions.overrideConfigFile = ESLintOverrideConfigFile;
46-
}
47-
if (ESLintOverrideConfig) {
48-
ESLintOptions.overrideConfig = ESLintOverrideConfig;
49-
}
5037

51-
const eslint = new ESLint(ESLintOptions);
38+
if (ESLintOverrideConfigFile) {
39+
ESLintOptions.overrideConfigFile = ESLintOverrideConfigFile;
40+
}
41+
if (ESLintOverrideConfig) {
42+
ESLintOptions.overrideConfig = ESLintOverrideConfig;
43+
}
44+
45+
const eslint = new ESLint(ESLintOptions);
5246

5347

54-
try {
5548
console.log("using options: " + JSON.stringify(ESLintOptions));
5649
console.log("format input", format_data.file_content);
5750
const lintTextOptions = {

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void useEslint() throws IOException {
164164
@Test
165165
void useXoStandardRules() throws IOException {
166166
setFile(".eslintrc.js").toResource("npm/eslint/typescript/standard_rules_xo/.eslintrc.js");
167-
setFile("tsconfig.json").toResource("npm/eslint/typescript/standard_rules_xo/tsconfig.json"); // needs to be copied to!
167+
setFile("tsconfig.json").toResource("npm/eslint/typescript/standard_rules_xo/tsconfig.json");
168168
setFile("build.gradle").toLines(
169169
"plugins {",
170170
" id 'com.diffplug.spotless'",
@@ -173,11 +173,31 @@ void useXoStandardRules() throws IOException {
173173
"spotless {",
174174
" typescript {",
175175
" target 'test.ts'",
176-
" eslint().styleGuide('xo-typescript').configFile('.eslintrc.js')//.tsconfigFile('tsconfig.json')", // TODO TODO maybe can skip the additional config files alltogether, instead provide tsConfigRootDir to eslint-serve.js via call
176+
" eslint().styleGuide('xo-typescript').configFile('.eslintrc.js')",
177177
" }",
178178
"}");
179179
setFile("test.ts").toResource("npm/eslint/typescript/standard_rules_xo/typescript.dirty");
180180
gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
181181
assertFile("test.ts").sameAsResource("npm/eslint/typescript/standard_rules_xo/typescript.clean");
182182
}
183+
184+
@Test
185+
void useStandardWithTypescriptRules() throws IOException {
186+
setFile(".eslintrc.js").toResource("npm/eslint/typescript/standard_rules_standard_with_typescript/.eslintrc.js");
187+
setFile("tsconfig.json").toResource("npm/eslint/typescript/standard_rules_standard_with_typescript/tsconfig.json");
188+
setFile("build.gradle").toLines(
189+
"plugins {",
190+
" id 'com.diffplug.spotless'",
191+
"}",
192+
"repositories { mavenCentral() }",
193+
"spotless {",
194+
" typescript {",
195+
" target 'test.ts'",
196+
" eslint().styleGuide('standard-with-typescript').configFile('.eslintrc.js')",
197+
" }",
198+
"}");
199+
setFile("test.ts").toResource("npm/eslint/typescript/standard_rules_standard_with_typescript/typescript.dirty");
200+
gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
201+
assertFile("test.ts").sameAsResource("npm/eslint/typescript/standard_rules_standard_with_typescript/typescript.clean");
202+
}
183203
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true
5+
},
6+
extends: 'standard-with-typescript',
7+
overrides: [
8+
],
9+
parserOptions: {
10+
ecmaVersion: 'latest',
11+
sourceType: 'module',
12+
project: './tsconfig.json',
13+
},
14+
rules: {
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compileOnSave": true,
3+
"compilerOptions": {
4+
"module": "ES6",
5+
"noImplicitAny": true,
6+
"outDir": "build/ide/",
7+
"sourceMap": true,
8+
"skipLibCheck": true,
9+
"target": "ES6",
10+
"baseUrl": "./",
11+
"paths": {
12+
},
13+
"lib": ["es7", "dom", "es2017"]
14+
},
15+
"include": [
16+
"**/*"
17+
]
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class MyController extends AbstractController implements DisposeAware, CallbackAware {
2+
public myValue: string[]
3+
4+
constructor (private readonly myService: Service, name: string, private readonly field: any) { super(name) }
5+
6+
// ...
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class MyController extends AbstractController implements DisposeAware, CallbackAware {
2+
3+
public myValue:string[];
4+
5+
constructor(private myService:Service,name:string,private field:any){ super(name) ;}
6+
7+
8+
//...
9+
10+
}

0 commit comments

Comments
 (0)