Skip to content

Commit 9eb0ef8

Browse files
Support rescript.json
1 parent 5c05b17 commit 9eb0ef8

File tree

9 files changed

+67
-32
lines changed

9 files changed

+67
-32
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The only 2 themes we don't (and can't) support, due to their lack of coloring, a
5050
5151
## 💡 Features
5252

53-
- Supports `.res`, `.resi` and `bsconfig.json`.
53+
- Supports `.res`, `.resi`, `rescript.json` and the legacy config file `bsconfig.json`.
5454
- Syntax highlighting.
5555
- Formatting.
5656
- Build diagnostics.
@@ -100,8 +100,8 @@ You'll find all ReScript specific settings under the scope `rescript.settings`.
100100
| Setting | Description |
101101
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
102102
| Prompt to Start Build | If there's no ReScript build running already in the opened project, the extension will prompt you and ask if you want to start a build automatically. You can turn off this automatic prompt via the setting `rescript.settings.askToStartBuild`. |
103-
| ReScript Binary Path | The extension will look for the existence of a `node_modules/.bin/rescript` file and use its directory as the `binaryPath`. If it does not find it at the project root (which is where the nearest `bsconfig.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.binaryPath` |
104-
| ReScript Platform Path | The extension will look for the existence of a `node_modules/rescript` directory and use the subdirectory corresponding to the current platform as the `platformPath`. If it does not find it at the project root (which is where the nearest `bsconfig.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.platformPath` |
103+
| ReScript Binary Path | The extension will look for the existence of a `node_modules/.bin/rescript` file and use its directory as the `binaryPath`. If it does not find it at the project root (which is where the nearest `rescript.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.binaryPath` |
104+
| ReScript Platform Path | The extension will look for the existence of a `node_modules/rescript` directory and use the subdirectory corresponding to the current platform as the `platformPath`. If it does not find it at the project root (which is where the nearest `rescript.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.platformPath` |
105105
| Inlay Hints (experimental) | This allows an editor to place annotations inline with text to display type hints. Enable using `rescript.settings.inlayHints.enable: true` |
106106
| Code Lens (experimental) | This tells the editor to add code lenses to function definitions, showing its full type above the definition. Enable using `rescript.settings.codeLens: true` |
107107
| Signature Help | This tells the editor to show signature help when you're writing function calls. Enable using `rescript.settings.signatureHelp.enabled: true` |
@@ -140,7 +140,7 @@ The Code Analyzer is a mode in the extension that runs additional code analysis
140140
141141
### Configuring the Code Analyzer
142142

143-
You'll need to configure what code analysis you want to run, and what (if any) directories you want to ignore. Configuration is done via adding `reanalyze` in `bsconfig.json`. You'll get autocomplete for what configuration options are valid. You can also read [all about configuring `reanalyze` here](https://github.com/rescript-association/reanalyze#configuration-via-bsconfigjson).
143+
You'll need to configure what code analysis you want to run, and what (if any) directories you want to ignore. Configuration is done via adding `reanalyze` in `rescript.json`. You'll get autocomplete for what configuration options are valid. You can also read [all about configuring `reanalyze` here](https://github.com/rescript-association/reanalyze#configuration-via-bsconfigjson).
144144

145145
### Usage
146146

analysis/reanalyze/src/Paths.ml

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ open Common
22
module StringMap = Map_string
33

44
let bsconfig = "bsconfig.json"
5+
let rescriptJson = "rescript.json"
56

67
let readFile filename =
78
try
@@ -79,13 +80,13 @@ module Config = struct
7980
| Some False -> RunConfig.transitive false
8081
| _ -> ()
8182

82-
(* Read the config from bsconfig.json and apply it to runConfig and suppress and unsuppress *)
83+
(* Read the config from rescript.json/bsconfig.json and apply it to runConfig and suppress and unsuppress *)
8384
let processBsconfig () =
8485
Lazy.force setReScriptProjectRoot;
86+
let rescriptFile = Filename.concat runConfig.projectRoot rescriptJson in
8587
let bsconfigFile = Filename.concat runConfig.projectRoot bsconfig in
86-
match readFile bsconfigFile with
87-
| None -> ()
88-
| Some text -> (
88+
89+
let processText text =
8990
match Json.parse text with
9091
| None -> ()
9192
| Some json -> (
@@ -97,7 +98,15 @@ module Config = struct
9798
readTransitive conf
9899
| None ->
99100
(* if no "analysis" specified, default to dce *)
100-
RunConfig.dce ()))
101+
RunConfig.dce ())
102+
in
103+
104+
match readFile rescriptFile with
105+
| Some text -> processText text
106+
| None -> (
107+
match readFile bsconfigFile with
108+
| Some text -> processText text
109+
| None -> ())
101110
end
102111

103112
(**

analysis/reanalyze/src/Reanalyze.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ let cli () =
139139
"root_path Run all the analyses for all the .cmt files under the root \
140140
path" );
141141
("-ci", Unit (fun () -> Cli.ci := true), "Internal flag for use in CI");
142-
("-config", Unit setConfig, "Read the analysis mode from bsconfig.json");
142+
( "-config",
143+
Unit setConfig,
144+
"Read the analysis mode from rescript.json/bsconfig.json" );
143145
("-dce", Unit (fun () -> setDCE None), "Eperimental DCE");
144146
("-debug", Unit (fun () -> Cli.debug := true), "Print debug information");
145147
( "-dce-cmt",

analysis/src/FindFiles.ml

+14-5
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ let findDependencyFiles base config =
233233
Json.bind
234234
(ModuleResolution.resolveNodeModulePath ~startPath:base name)
235235
(fun path ->
236-
let innerPath = path /+ "bsconfig.json" in
237-
match Files.readFile innerPath with
238-
| Some text -> (
236+
let rescriptJsonPath = path /+ "rescript.json" in
237+
let bsconfigJsonPath = path /+ "bsconfig.json" in
238+
239+
let parseText text =
239240
match Json.parse text with
240241
| Some inner -> (
241242
let namespace = getNamespace inner in
@@ -259,9 +260,17 @@ let findDependencyFiles base config =
259260
~path ~sourceDirectories ~libBs
260261
in
261262
Some (compiledDirectories, projectFiles))
262-
| None -> None)
263-
| None -> None)
263+
| None -> None
264+
in
265+
266+
match Files.readFile rescriptJsonPath with
267+
| Some text -> parseText text
268+
| None -> (
269+
match Files.readFile bsconfigJsonPath with
270+
| Some text -> parseText text
271+
| None -> None))
264272
in
273+
265274
match result with
266275
| Some (files, directories) -> (files, directories)
267276
| None ->

analysis/src/Packages.ml

+20-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ let makePathsForModule ~projectFilesAndPaths ~dependenciesFilesAndPaths =
1212
pathsForModule
1313

1414
let newBsPackage ~rootPath =
15-
let bsconfig = Filename.concat rootPath "bsconfig.json" in
16-
match Files.readFile bsconfig with
17-
| None ->
18-
Log.log ("Unable to read " ^ bsconfig);
19-
None
20-
| Some raw -> (
15+
let rescriptJson = Filename.concat rootPath "rescript.json" in
16+
let bsconfigJson = Filename.concat rootPath "bsconfig.json" in
17+
18+
let parseRaw raw =
2119
let libBs = BuildSystem.getLibBs rootPath in
2220
match Json.parse raw with
2321
| Some config -> (
@@ -156,15 +154,28 @@ let newBsPackage ~rootPath =
156154
});
157155
uncurried;
158156
})))
159-
| None -> None)
157+
| None -> None
158+
in
159+
160+
match Files.readFile rescriptJson with
161+
| Some raw -> parseRaw raw
162+
| None -> (
163+
Log.log ("Unable to read " ^ rescriptJson);
164+
match Files.readFile bsconfigJson with
165+
| Some raw -> parseRaw raw
166+
| None ->
167+
Log.log ("Unable to read " ^ bsconfigJson);
168+
None)
160169

161170
let findRoot ~uri packagesByRoot =
162171
let path = Uri.toPath uri in
163172
let rec loop path =
164173
if path = "/" then None
165174
else if Hashtbl.mem packagesByRoot path then Some (`Root path)
166-
else if Files.exists (Filename.concat path "bsconfig.json") then
167-
Some (`Bs path)
175+
else if
176+
Files.exists (Filename.concat path "rescript.json")
177+
|| Files.exists (Filename.concat path "bsconfig.json")
178+
then Some (`Bs path)
168179
else
169180
let parent = Filename.dirname path in
170181
if parent = path then (* reached root *) None else loop parent

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
],
4141
"jsonValidation": [
4242
{
43-
"fileMatch": "bsconfig.json",
43+
"fileMatch": ["bsconfig.json", "rescript.json"],
4444
"url": "https://raw.githubusercontent.com/rescript-lang/rescript-compiler/master/docs/docson/build-schema.json"
4545
}
4646
],

server/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export let rescriptNodePartialPath = path.join(
4242

4343
export let bsbLock = ".bsb.lock";
4444
export let bsconfigPartialPath = "bsconfig.json";
45+
export let rescriptJsonPartialPath = "rescript.json";
4546
export let compilerDirPartialPath = path.join("lib", "bs");
4647
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");
4748
export let resExt = ".res";

server/src/lookup.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ export const findFilePathFromProjectRoot = (
4646
return findFilePathFromProjectRoot(parentDir, filePartialPath);
4747
};
4848

49-
export const readBsConfig = (projDir: p.DocumentUri): BuildSchema | null => {
49+
export const readConfig = (projDir: p.DocumentUri): BuildSchema | null => {
5050
try {
51-
let bsconfigFile = fs.readFileSync(
52-
path.join(projDir, c.bsconfigPartialPath),
51+
let rescriptJson = path.join(projDir, c.rescriptJsonPartialPath);
52+
let bsconfigJson = path.join(projDir, c.bsconfigPartialPath);
53+
54+
let configFile = fs.readFileSync(
55+
fs.existsSync(rescriptJson) ? rescriptJson : bsconfigJson,
5356
{ encoding: "utf-8" }
5457
);
5558

56-
let result: BuildSchema = JSON.parse(bsconfigFile);
59+
let result: BuildSchema = JSON.parse(configFile);
5760
return result;
5861
} catch (e) {
5962
return null;
@@ -108,7 +111,7 @@ export const getFilenameFromBsconfig = (
108111
projDir: string,
109112
partialFilePath: string
110113
): string | null => {
111-
let bsconfig = readBsConfig(projDir);
114+
let bsconfig = readConfig(projDir);
112115

113116
if (!bsconfig) {
114117
return null;
@@ -135,7 +138,7 @@ export const getFilenameFromRootBsconfig = (
135138
return null;
136139
}
137140

138-
let rootBsconfig = readBsConfig(path.dirname(rootBsConfigPath));
141+
let rootBsconfig = readConfig(path.dirname(rootBsConfigPath));
139142

140143
if (!rootBsconfig) {
141144
return null;

server/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export const toCamelCase = (text: string): string => {
195195
export const getNamespaceNameFromBsConfig = (
196196
projDir: p.DocumentUri
197197
): execResult => {
198-
let bsconfig = lookup.readBsConfig(projDir);
198+
let bsconfig = lookup.readConfig(projDir);
199199
let result = "";
200200

201201
if (!bsconfig) {

0 commit comments

Comments
 (0)