Skip to content

Commit a106685

Browse files
author
Josh Goldberg
authored
Stopped erroring when .vscode/settings.json doesn't exist (#304)
* Stopped erroring when .vscode/settings.json doesn't exist * Still error if the file is specified
1 parent a8b3f5c commit a106685

File tree

6 files changed

+119
-60
lines changed

6 files changed

+119
-60
lines changed

src/cli/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const findConfigurationDependencies = {
7474
};
7575

7676
const findEditorConfigurationDependencies: FindEditorConfigurationDependencies = {
77+
fileSystem: fsFileSystem,
7778
importer: boundImporter,
7879
};
7980

src/cli/runCli.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { version } from "../../package.json";
66
import { Logger } from "../adapters/logger";
77
import { SansDependencies } from "../binding";
88
import { convertConfig } from "../conversion/convertConfig";
9-
import { DEFAULT_VSCODE_SETTINGS_PATH } from "../input/vsCodeSettings";
109
import { ResultStatus, ResultWithStatus, TSLintToESLintSettings } from "../types";
1110

1211
export type RunCliDependencies = {
@@ -25,11 +24,7 @@ export const runCli = async (
2524
.option("--package [package]", "package configuration file to convert using")
2625
.option("--tslint [tslint]", "tslint configuration file to convert using")
2726
.option("--typescript [typescript]", "typescript configuration file to convert using")
28-
.option(
29-
"--editor [editor]",
30-
"editor configuration file to convert using",
31-
DEFAULT_VSCODE_SETTINGS_PATH,
32-
)
27+
.option("--editor [editor]", "editor configuration file to convert")
3328
.option("-V --version", "output the package version");
3429

3530
const parsedArgv = {

src/conversion/convertEditorConfig.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ const createStubDependencies = (
1919
});
2020

2121
describe("convertEditorConfig", () => {
22+
it("returns a success result when there is no original configuration", async () => {
23+
// Arrange
24+
const dependencies = createStubDependencies({
25+
findEditorConfiguration: async () => undefined,
26+
});
27+
28+
// Act
29+
const result = await convertEditorConfig(dependencies, stubSettings);
30+
31+
// Assert
32+
expect(result).toEqual({
33+
status: ResultStatus.Succeeded,
34+
});
35+
});
36+
2237
it("returns the failure result when finding the original configurations fails", async () => {
2338
// Arrange
2439
const error = new Error();
@@ -28,7 +43,10 @@ describe("convertEditorConfig", () => {
2843
};
2944

3045
const dependencies = createStubDependencies({
31-
findEditorConfiguration: async () => error,
46+
findEditorConfiguration: async () => ({
47+
configPath: "",
48+
result: error,
49+
}),
3250
});
3351

3452
// Act
@@ -59,14 +77,12 @@ describe("convertEditorConfig", () => {
5977
// Arrange
6078
const originalConfig = {
6179
"typescript.tsdk": "node_modules/typescript/lib",
62-
"editor.tabSize": 4,
63-
"editor.codeActionsOnSave": {
64-
"source.organizeImports": false,
65-
},
6680
};
6781

6882
const dependencies = createStubDependencies({
69-
findEditorConfiguration: jest.fn().mockResolvedValue(originalConfig),
83+
findEditorConfiguration: jest.fn().mockResolvedValue({
84+
result: originalConfig,
85+
}),
7086
});
7187

7288
// Act
@@ -113,20 +129,4 @@ describe("convertEditorConfig", () => {
113129
status: ResultStatus.Succeeded,
114130
});
115131
});
116-
117-
it("uses VS Code default settings path if editor config parameter is undefined", async () => {
118-
// Arrange
119-
const expectedEditorPath = ".vscode/settings.json";
120-
const settings = {
121-
config: "./eslintrc.js",
122-
};
123-
124-
const dependencies = createStubDependencies();
125-
126-
// Act
127-
await convertEditorConfig(dependencies, settings);
128-
129-
// Assert
130-
expect(dependencies.findEditorConfiguration).toHaveBeenCalledWith(expectedEditorPath);
131-
});
132132
});

src/conversion/convertEditorConfig.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { SansDependencies } from "../binding";
22
import { writeConversionResults } from "../creation/writeEditorConfigConversionResults";
33
import { convertEditorSettings } from "../editorSettings/convertEditorSettings";
44
import { findEditorConfiguration } from "../input/findEditorConfiguration";
5-
import { DEFAULT_VSCODE_SETTINGS_PATH } from "../input/vsCodeSettings";
65
import { reportEditorSettingConversionResults } from "../reporting/reportEditorSettingConversionResults";
76
import { ResultStatus, ResultWithStatus, TSLintToESLintSettings } from "../types";
87

@@ -20,26 +19,26 @@ export const convertEditorConfig = async (
2019
dependencies: ConvertEditorConfigDependencies,
2120
settings: TSLintToESLintSettings,
2221
): Promise<ResultWithStatus> => {
23-
const editorConfigPath = settings.editor ? settings.editor : DEFAULT_VSCODE_SETTINGS_PATH;
24-
const originalEditorConfiguration = await dependencies.findEditorConfiguration(
25-
editorConfigPath,
26-
);
27-
if (originalEditorConfiguration instanceof Error) {
22+
const conversion = await dependencies.findEditorConfiguration(settings.editor);
23+
if (conversion === undefined) {
24+
return {
25+
status: ResultStatus.Succeeded,
26+
};
27+
}
28+
29+
if (conversion.result instanceof Error) {
2830
return {
29-
errors: [originalEditorConfiguration],
31+
errors: [conversion.result],
3032
status: ResultStatus.Failed,
3133
};
3234
}
3335

34-
const settingConversionResults = dependencies.convertEditorSettings(
35-
originalEditorConfiguration,
36-
);
36+
const settingConversionResults = dependencies.convertEditorSettings(conversion.result);
3737

38-
const outputPath = editorConfigPath;
3938
const fileWriteError = await dependencies.writeConversionResults(
40-
outputPath,
39+
conversion.configPath,
4140
settingConversionResults,
42-
originalEditorConfiguration,
41+
conversion.result,
4342
);
4443
if (fileWriteError !== undefined) {
4544
return {

src/input/findEditorConfiguration.test.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { createStubFileSystem } from "../adapters/fileSystem.stub";
12
import {
23
findEditorConfiguration,
34
FindEditorConfigurationDependencies,
45
} from "./findEditorConfiguration";
6+
import { DEFAULT_VSCODE_SETTINGS_PATH } from "./vsCodeSettings";
57

68
const stubConfigPath = "temp/";
79

@@ -10,10 +12,46 @@ export const createStubImporter = (filePath = "") =>
1012

1113
const createStubDependencies = (overrides: Partial<FindEditorConfigurationDependencies> = {}) => ({
1214
importer: createStubImporter(stubConfigPath),
15+
fileSystem: createStubFileSystem(),
1316
...overrides,
1417
});
1518

1619
describe("findEditorConfiguration", () => {
20+
it("returns undefined when the file is not specified and does not exist", async () => {
21+
// Arrange
22+
const dependencies = createStubDependencies({
23+
fileSystem: {
24+
fileExists: async () => false,
25+
},
26+
});
27+
28+
// Act
29+
const result = await findEditorConfiguration(dependencies, undefined);
30+
31+
// Assert
32+
expect(result).toEqual(undefined);
33+
});
34+
35+
it("returns an error when the file is specified and does not exist", async () => {
36+
// Arrange
37+
const dependencies = createStubDependencies({
38+
fileSystem: {
39+
fileExists: async () => false,
40+
},
41+
});
42+
43+
// Act
44+
const result = await findEditorConfiguration(dependencies, stubConfigPath);
45+
46+
// Assert
47+
expect(result).toEqual({
48+
configPath: stubConfigPath,
49+
result: expect.objectContaining({
50+
message: `Could not find editor configuration under '${stubConfigPath}'.`,
51+
}),
52+
});
53+
});
54+
1755
it("returns an error when importer returns one", async () => {
1856
// Arrange
1957
const message = "error";
@@ -27,11 +65,12 @@ describe("findEditorConfiguration", () => {
2765
const result = await findEditorConfiguration(dependencies, stubConfigPath);
2866

2967
// Assert
30-
expect(result).toEqual(
31-
expect.objectContaining({
68+
expect(result).toEqual({
69+
configPath: stubConfigPath,
70+
result: expect.objectContaining({
3271
message,
3372
}),
34-
);
73+
});
3574
});
3675

3776
it("reads from the given configuration path when one is provided", async () => {
@@ -46,25 +85,30 @@ describe("findEditorConfiguration", () => {
4685
expect(dependencies.importer).toHaveBeenLastCalledWith(configPath);
4786
});
4887

49-
it("defaults to VS Code editor settings path when config path isn't provided", async () => {
88+
it("parses object from the default VS Code configuration path when the file is not specified and read successfully", async () => {
5089
// Arrange
51-
const dependencies = createStubDependencies();
90+
const originalConfig = {
91+
"typescript.tsdk": "node_modules/typescript/lib",
92+
};
93+
94+
const dependencies = createStubDependencies({
95+
importer: async () => originalConfig,
96+
});
5297

5398
// Act
54-
await findEditorConfiguration(dependencies, undefined);
99+
const result = await findEditorConfiguration(dependencies, undefined);
55100

56101
// Assert
57-
expect(dependencies.importer).toHaveBeenLastCalledWith(".vscode/settings.json");
102+
expect(result).toEqual({
103+
configPath: DEFAULT_VSCODE_SETTINGS_PATH,
104+
result: originalConfig,
105+
});
58106
});
59107

60-
it("parses object from configuration path when read successfully", async () => {
108+
it("parses object from configuration path when the file is specified and read successfully", async () => {
61109
// Arrange
62110
const originalConfig = {
63111
"typescript.tsdk": "node_modules/typescript/lib",
64-
"editor.tabSize": 4,
65-
"editor.codeActionsOnSave": {
66-
"source.organizeImports": false,
67-
},
68112
};
69113

70114
const dependencies = createStubDependencies({
@@ -75,6 +119,9 @@ describe("findEditorConfiguration", () => {
75119
const result = await findEditorConfiguration(dependencies, stubConfigPath);
76120

77121
// Assert
78-
expect(result).toEqual(originalConfig);
122+
expect(result).toEqual({
123+
configPath: stubConfigPath,
124+
result: originalConfig,
125+
});
79126
});
80127
});

src/input/findEditorConfiguration.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FileSystem } from "../adapters/fileSystem";
12
import { SansDependencies } from "../binding";
23
import { EditorConfiguration } from "./editorConfiguration";
34
import { findRawConfiguration } from "./findRawConfiguration";
@@ -6,18 +7,34 @@ import { importer } from "./importer";
67
import { DEFAULT_VSCODE_SETTINGS_PATH } from "./vsCodeSettings";
78

89
export type FindEditorConfigurationDependencies = {
10+
fileSystem: Pick<FileSystem, "fileExists">;
911
importer: SansDependencies<typeof importer>;
1012
};
1113

1214
export const findEditorConfiguration = async (
1315
dependencies: FindEditorConfigurationDependencies,
14-
config: string | undefined,
15-
): Promise<DeepPartial<EditorConfiguration> | Error> => {
16-
const filePath = config ?? DEFAULT_VSCODE_SETTINGS_PATH;
17-
const rawConfiguration = await findRawConfiguration<DeepPartial<EditorConfiguration>>(
16+
specifiedConfigPath: string | undefined,
17+
) => {
18+
const attemptingConfigPath = specifiedConfigPath ?? DEFAULT_VSCODE_SETTINGS_PATH;
19+
20+
if (!(await dependencies.fileSystem.fileExists(attemptingConfigPath))) {
21+
return specifiedConfigPath === undefined
22+
? undefined
23+
: {
24+
configPath: attemptingConfigPath,
25+
result: new Error(
26+
`Could not find editor configuration under '${attemptingConfigPath}'.`,
27+
),
28+
};
29+
}
30+
31+
const result = await findRawConfiguration<DeepPartial<EditorConfiguration>>(
1832
dependencies.importer,
19-
filePath,
33+
attemptingConfigPath,
2034
);
2135

22-
return rawConfiguration;
36+
return {
37+
configPath: attemptingConfigPath,
38+
result,
39+
};
2340
};

0 commit comments

Comments
 (0)