Skip to content

Commit 1a243ae

Browse files
committed
Remove code duplication.
1 parent be0d468 commit 1a243ae

File tree

3 files changed

+25
-48
lines changed

3 files changed

+25
-48
lines changed

client/src/commands/code_analysis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ let analysisProdPath = path.join(
144144
"rescript-editor-analysis.exe"
145145
);
146146

147-
let getBinaryPath = (): string | null => {
147+
let getAnalysisBinaryPath = (): string | null => {
148148
if (fs.existsSync(analysisDevPath)) {
149149
return analysisDevPath;
150150
} else if (fs.existsSync(analysisProdPath)) {
@@ -162,7 +162,7 @@ export const runCodeAnalysisWithReanalyze = (
162162
let currentDocument = window.activeTextEditor.document;
163163
let cwd = targetDir ?? path.dirname(currentDocument.uri.fsPath);
164164

165-
let binaryPath = getBinaryPath();
165+
let binaryPath = getAnalysisBinaryPath();
166166
if (binaryPath === null) {
167167
window.showErrorMessage("Binary executable not found.", analysisProdPath);
168168
return;

server/src/server.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,16 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
6565
// will be properly defined later depending on the mode (stdio/node-rpc)
6666
let send: (msg: p.Message) => void = (_) => {};
6767

68-
let getBinaryPath = (projectRootPath: p.DocumentUri) =>
68+
let getBinaryDirPath = (projectRootPath: p.DocumentUri) =>
6969
extensionConfiguration.binaryPath === null
7070
? path.join(projectRootPath, c.nodeModulesBinDir)
7171
: extensionConfiguration.binaryPath;
7272

7373
let findRescriptBinary = (projectRootPath: p.DocumentUri) =>
74-
extensionConfiguration.binaryPath === null
75-
? utils.findRescriptBinaryFromProjectRoot(projectRootPath)
76-
: utils.findRescriptBinaryFromConfig(extensionConfiguration.binaryPath);
77-
78-
let findBscBinary = (filePath: p.DocumentUri) =>
79-
extensionConfiguration.binaryPath === null
80-
? utils.findBscBinaryFromProjectRoot(filePath)
81-
: utils.findBscBinaryFromConfig(extensionConfiguration.binaryPath);
74+
utils.findRescriptBinary(getBinaryDirPath(projectRootPath));
8275

76+
let findBscBinary = (projectRootPath: p.DocumentUri) =>
77+
utils.findBscBinary(getBinaryDirPath(projectRootPath));
8378

8479
interface CreateInterfaceRequestParams {
8580
uri: string;
@@ -278,7 +273,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
278273
method: "window/showMessage",
279274
params: {
280275
type: p.MessageType.Error,
281-
message: `Can't find ReScript binary in the directory ${getBinaryPath(
276+
message: `Can't find ReScript binary in the directory ${getBinaryDirPath(
282277
projectRootPath
283278
)}`,
284279
},

server/src/utils.ts

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,51 +38,33 @@ export let findProjectRootOfFile = (
3838
}
3939
};
4040

41-
export let findBscBinaryFromProjectRoot = (
42-
source: p.DocumentUri
43-
): null | p.DocumentUri => {
44-
let dir = path.dirname(source);
45-
// The rescript package's rescript command is a JS wrapper. `rescript format`
46-
// also invokes another JS wrapper. _That_ JS wrapper ultimately calls the
47-
// (unexposed) bsc -format anyway.
48-
let bscNativeReScriptPath = path.join(dir, c.bscNativeReScriptPartialPath);
49-
50-
if (fs.existsSync(bscNativeReScriptPath)) {
51-
return bscNativeReScriptPath;
52-
} else if (dir === source) {
53-
// reached the top
41+
export let findBinary = (
42+
binaryDirPath: p.DocumentUri | null,
43+
binaryName: string
44+
): p.DocumentUri | null => {
45+
if (binaryDirPath == null) {
5446
return null;
47+
}
48+
let binaryPath: p.DocumentUri = path.join(binaryDirPath, binaryName);
49+
if (fs.existsSync(binaryPath)) {
50+
return binaryPath;
5551
} else {
56-
return findBscBinaryFromProjectRoot(dir);
52+
return null;
5753
}
5854
};
5955

60-
export let findBscBinaryFromConfig = (
61-
pathToBinaryDirFromConfig: p.DocumentUri
62-
): null | p.DocumentUri => {
63-
let bscPath = path.join(pathToBinaryDirFromConfig, c.bscBinName);
64-
if (fs.existsSync(bscPath)) {
65-
return bscPath;
66-
}
67-
return null;
56+
export let findRescriptBinary = (
57+
binaryDirPath: p.DocumentUri | string
58+
): p.DocumentUri | null => {
59+
return findBinary(binaryDirPath, c.rescriptBinName);
6860
};
6961

70-
let findBuildBinaryBase = (rescriptPath: string): string | null => {
71-
if (fs.existsSync(rescriptPath)) {
72-
return rescriptPath;
73-
}
74-
return null;
62+
export let findBscBinary = (
63+
binaryDirPath: p.DocumentUri | string
64+
): p.DocumentUri | null => {
65+
return findBinary(binaryDirPath, c.bscBinName);
7566
};
7667

77-
export let findRescriptBinaryFromConfig = (
78-
pathToBinaryDirFromConfig: p.DocumentUri
79-
) =>
80-
findBuildBinaryBase(path.join(pathToBinaryDirFromConfig, c.rescriptBinName));
81-
82-
export let findRescriptBinaryFromProjectRoot = (
83-
projectRootPath: p.DocumentUri
84-
) => findBuildBinaryBase(path.join(projectRootPath, c.rescriptNodePartialPath));
85-
8668
type execResult =
8769
| {
8870
kind: "success";

0 commit comments

Comments
 (0)