Skip to content

Commit 016dd3a

Browse files
committed
eslint: test supplying inline config
1 parent 35b2b7c commit 016dd3a

File tree

3 files changed

+52
-114
lines changed

3 files changed

+52
-114
lines changed

lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ private static class State extends NpmFormatterStepStateBase implements Serializ
146146
}
147147

148148
private EslintConfig localCopyFiles(EslintConfig orig) {
149+
if (orig.getEslintConfigPath() == null) {
150+
return orig;
151+
}
149152
// If any config files are provided, we need to make sure they are at the same location as the node modules
150153
// as eslint will try to resolve plugin/config names relatively to the config file location and some
151154
// eslint configs contain relative paths to additional config files (such as tsconfig.json e.g.)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ app.post("/eslint/format", async (req, res) => {
3838
ESLintOptions.overrideConfigFile = ESLintOverrideConfigFile;
3939
}
4040
if (ESLintOverrideConfig) {
41-
ESLintOptions.overrideConfig = ESLintOverrideConfig;
41+
eval("ESLintOptions.overrideConfig = " + ESLintOverrideConfig);
4242
}
4343

4444
console.log("using options: " + JSON.stringify(ESLintOptions));

testlib/src/test/java/com/diffplug/spotless/npm/EslintFormatterStepTest.java

Lines changed: 48 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -85,129 +85,64 @@ void formattingUsingRulesetsFile(String ruleSetName) throws Exception {
8585
}
8686
}
8787
}
88-
/*
89-
@NpmTest
90-
@Nested
91-
class PrettierFormattingOfFileTypesIsWorking extends NpmFormatterStepCommonTests {
92-
93-
@ParameterizedTest(name = "{index}: prettier can be applied to {0}")
94-
@ValueSource(strings = {"html", "typescript", "json", "javascript-es5", "javascript-es6", "css", "scss", "markdown", "yaml"})
95-
void formattingUsingConfigFile(String fileType) throws Exception {
96-
String filedir = "npm/prettier/filetypes/" + fileType + "/";
97-
98-
final File prettierRc = createTestFile(filedir + ".prettierrc.yml");
99-
final String dirtyFile = filedir + fileType + ".dirty";
100-
final String cleanFile = filedir + fileType + ".clean";
101-
102-
final FormatterStep formatterStep = PrettierFormatterStep.create(
103-
PrettierFormatterStep.defaultDevDependencies(),
104-
TestProvisioner.mavenCentral(),
105-
buildDir(),
106-
npmPathResolver(),
107-
new PrettierConfig(prettierRc, null));
108-
109-
try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
110-
stepHarness.testResource(dirtyFile, cleanFile);
111-
}
112-
}
113-
}
11488

11589
@NpmTest
11690
@Nested
117-
class SpecificPrettierFormatterStepTests extends NpmFormatterStepCommonTests {
91+
class EslintInlineConfigFormattingStepTest extends NpmFormatterStepCommonTests {
11892

119-
@Test
120-
void parserInferenceBasedOnExplicitFilepathIsWorking() throws Exception {
121-
String filedir = "npm/prettier/filetypes/json/";
122-
123-
final String dirtyFile = filedir + "json.dirty";
124-
final String cleanFile = filedir + "json.clean";
125-
126-
final FormatterStep formatterStep = PrettierFormatterStep.create(
127-
PrettierFormatterStep.defaultDevDependencies(),
128-
TestProvisioner.mavenCentral(),
129-
buildDir(),
130-
npmPathResolver(),
131-
new PrettierConfig(null, ImmutableMap.of("filepath", "anyname.json"))); // should select parser based on this name
132-
133-
try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
134-
stepHarness.testResource(dirtyFile, cleanFile);
135-
}
136-
}
13793

13894
@Test
139-
void parserInferenceBasedOnFilenameIsWorking() throws Exception {
140-
String filedir = "npm/prettier/filename/";
141-
142-
final String dirtyFile = filedir + "dirty.json";
143-
final String cleanFile = filedir + "clean.json";
95+
void formattingUsingInlineXoConfig() throws Exception {
96+
String filedir = "npm/eslint/typescript/standard_rules_xo/";
97+
98+
String testDir = "formatting_ruleset_xo_inline_config/";
99+
100+
101+
final String esLintConfig = String.join("\n",
102+
"{",
103+
" env: {",
104+
" browser: true,",
105+
" es2021: true,",
106+
" },",
107+
" extends: 'xo/browser',",
108+
" overrides: [",
109+
" {",
110+
" extends: [",
111+
" 'xo-typescript',",
112+
" ],",
113+
" files: [",
114+
" '*.ts',",
115+
" '*.tsx',",
116+
" ],",
117+
" },",
118+
" ],",
119+
" parser: '@typescript-eslint/parser',",
120+
" parserOptions: {",
121+
" ecmaVersion: 'latest',",
122+
" sourceType: 'module',",
123+
" project: './tsconfig.json',",
124+
" },",
125+
" rules: {",
126+
" },",
127+
"}"
128+
);
129+
130+
File tsconfigFile = setFile(testDir + "tsconfig.json").toResource(filedir + "tsconfig.json");
131+
final String dirtyFile = filedir + "typescript.dirty";
132+
File dirtyFileFile = setFile(testDir + "test.ts").toResource(dirtyFile);
133+
final String cleanFile = filedir + "typescript.clean";
144134

145-
final FormatterStep formatterStep = PrettierFormatterStep.create(
146-
PrettierFormatterStep.defaultDevDependencies(),
147-
TestProvisioner.mavenCentral(),
148-
buildDir(),
149-
npmPathResolver(),
150-
new PrettierConfig(null, Collections.emptyMap()));
135+
final FormatterStep formatterStep = EslintFormatterStep.create(
136+
combine(EslintFormatterStep.PopularStyleGuide.XO_TYPESCRIPT.devDependencies(), EslintFormatterStep.defaultDevDependenciesForTypescript()),
137+
TestProvisioner.mavenCentral(),
138+
projectDir(),
139+
buildDir(),
140+
npmPathResolver(),
141+
new EslintTypescriptConfig(null, esLintConfig, tsconfigFile));
151142

152143
try (StepHarnessWithFile stepHarness = StepHarnessWithFile.forStep(formatterStep)) {
153-
stepHarness.testResource(new File("test.json"), dirtyFile, cleanFile);
154-
}
155-
}
156-
157-
@Test
158-
void verifyPrettierErrorMessageIsRelayed() throws Exception {
159-
FormatterStep formatterStep = PrettierFormatterStep.create(
160-
PrettierFormatterStep.defaultDevDependenciesWithPrettier("2.0.5"),
161-
TestProvisioner.mavenCentral(),
162-
buildDir(),
163-
npmPathResolver(),
164-
new PrettierConfig(null, ImmutableMap.of("parser", "postcss")));
165-
try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
166-
stepHarness.testResourceException("npm/prettier/filetypes/scss/scss.dirty", exception -> {
167-
exception.hasMessageContaining("HTTP 501");
168-
exception.hasMessageContaining("Couldn't resolve parser \"postcss\"");
169-
});
144+
stepHarness.testResource(dirtyFileFile, dirtyFile, cleanFile);
170145
}
171146
}
172147
}
173-
174-
@NpmTest
175-
@Nested
176-
class PrettierFormattingOptionsAreWorking extends NpmFormatterStepCommonTests {
177-
178-
private static final String FILEDIR = "npm/prettier/config/";
179-
180-
void runFormatTest(PrettierConfig config, String cleanFileNameSuffix) throws Exception {
181-
182-
final String dirtyFile = FILEDIR + "typescript.dirty";
183-
final String cleanFile = FILEDIR + "typescript." + cleanFileNameSuffix + ".clean";
184-
185-
final FormatterStep formatterStep = PrettierFormatterStep.create(
186-
PrettierFormatterStep.defaultDevDependencies(),
187-
TestProvisioner.mavenCentral(),
188-
buildDir(),
189-
npmPathResolver(),
190-
config); // should select parser based on this name
191-
192-
try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
193-
stepHarness.testResource(dirtyFile, cleanFile);
194-
}
195-
}
196-
197-
@Test
198-
void defaultsAreApplied() throws Exception {
199-
runFormatTest(new PrettierConfig(null, ImmutableMap.of("parser", "typescript")), "defaults");
200-
}
201-
202-
@Test
203-
void configFileOptionsAreApplied() throws Exception {
204-
runFormatTest(new PrettierConfig(createTestFile(FILEDIR + ".prettierrc.yml"), null), "configfile");
205-
}
206-
207-
@Test
208-
void configFileOptionsCanBeOverriden() throws Exception {
209-
runFormatTest(new PrettierConfig(createTestFile(FILEDIR + ".prettierrc.yml"), ImmutableMap.of("printWidth", 300)), "override");
210-
}
211-
212-
}*/
213148
}

0 commit comments

Comments
 (0)