Skip to content

Commit 4a748bb

Browse files
cknittzth
andauthored
Move rescript-editor-analysis and rescript-tools into compiler repo (#7000)
* Dune: explicitly set package to "rescript" before adding more packages * Add package "analysis" from rescript-vscode repo * Analysis: remove vendored libs that are already in the compiler repo * Add Tast_iterator module from rescript-vscode repo * Remove class stuff from Tast_iterator * Format Tast_iterator * Fix DeadValue.ml * No more Config.uncurried * Analysis: use default print width instead of taking it from syntax cli * Make pexp_attributes mutable * Add package "tools" from rescript-vscode repo * Format OCaml code * Mark Res_multi_printer.default_print_width as live * Format * Do not check in tools/analysis test artifacts * Add rescript-editor-analysis and rescript-tools to rescript npm package * Fix path for analysis/tools binaries in test scripts * Run analysis tests in CI * analysis: get rid of PervasivesU * Nullable is now @unboxed * More pervasives * Analysis tests: use rescript from this repo and adapt * Output changes that are ok * Tools tests: use rescript from this repo * commit OK changes to analysis output * commit more OK changes to test output * get CodeLens to a good enough state * fix jsx prop completion broken by first class regexp literal support * use predef paths so weird type lookups do not happen * commit OK change * commit OK output * Fix gitignore * commit OK output (JSON type changes) * Do not run analysis tests on Windows * Add opam exec * print module name instead of full file name in debug statement * dont use deprecated functions * update generic jsx transform test * update incremental typechecking test * Fix uppercase exotic idents * Don't run analysis tests on Linux ARM * Move tools and analysis tests to tests folder * CHANGELOG * Add rescript-tools cli wrapper script * Remove obsolete Makefile * Ship RescriptTools module with the rescript npm package --------- Co-authored-by: Gabriel Nordeborn <gabbe.nord@gmail.com>
1 parent 682c8a0 commit 4a748bb

File tree

729 files changed

+120476
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

729 files changed

+120476
-12
lines changed

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ jobs:
320320
- name: Check for diffs in tests folder
321321
run: git diff --ignore-cr-at-eol --exit-code tests
322322

323+
- name: Run analysis / tools tests
324+
if: runner.os != 'Windows' && matrix.os != 'buildjet-2vcpu-ubuntu-2204-arm'
325+
run: opam exec -- make -C tests/analysis_tests test && make -C tests/tools_tests test
326+
323327
- name: Run gentype tests
324328
if: runner.os != 'Windows'
325329
run: make -C tests/gentype_tests/typescript-react-example clean test

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ playground/compiler.js
7676

7777
rewatch/target/
7878
rewatch/rewatch
79+
80+
tests/tools_tests/**/*.res.js
81+
tests/tools_tests/lib
82+
tests/analysis_tests*/lib
83+
tests/analysis_tests/**/*.bs.js

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
- Introduce "Unified operators" for arithmetic operators (`+`, `-`, `*`, `/`, `mod`). https://github.com/rescript-lang/rescript-compiler/pull/7057
1818
- Add remainder (`%`, aka modulus) operator. https://github.com/rescript-lang/rescript-compiler/pull/7152
1919

20-
2120
#### :bug: Bug fix
21+
2222
- Fix and clean up boolean and/or optimizations. https://github.com/rescript-lang/rescript-compiler/pull/7134 https://github.com/rescript-lang/rescript-compiler/pull/7151
2323

2424
#### :nail_care: Polish
25+
2526
- Improve code generation for pattern matching of untagged variants. https://github.com/rescript-lang/rescript-compiler/pull/7128
2627
- Improve negation handling in combination with and/or to simplify generated code (especially coming out of pattern matching). https://github.com/rescript-lang/rescript-compiler/pull/7138
2728
- optimize JavaScript code generation by using x == null checks and improving type-based optimizations for string/number literals. https://github.com/rescript-lang/rescript-compiler/pull/7141
@@ -30,6 +31,9 @@
3031
- Further improve boolean optimizations. https://github.com/rescript-lang/rescript-compiler/pull/7149
3132
- Simplify code generated for conditionals. https://github.com/rescript-lang/rescript-compiler/pull/7151
3233

34+
#### :house: Internal
35+
36+
- Move rescript-editor-analysis and rescript-tools into compiler repo. https://github.com/rescript-lang/rescript-compiler/pull/7000
3337

3438
# 12.0.0-alpha.4
3539

@@ -61,6 +65,7 @@
6165
- Fix genType JSX component compilation. https://github.com/rescript-lang/rescript-compiler/pull/7107
6266

6367
#### :nail_care: Polish
68+
6469
- Add some context to error message for unused variables. https://github.com/rescript-lang/rescript-compiler/pull/7050
6570
- Improve error message when passing `children` prop to a component that doesn't accept it. https://github.com/rescript-lang/rescript-compiler/pull/7044
6671
- Improve error messages for pattern matching on option vs non-option, and vice versa. https://github.com/rescript-lang/rescript-compiler/pull/7035
@@ -71,7 +76,6 @@
7176
- Provide additional context in error message when `unit` is expected. https://github.com/rescript-lang/rescript-compiler/pull/7045
7277
- Improve error message when passing an object where a record is expected. https://github.com/rescript-lang/rescript-compiler/pull/7101
7378

74-
7579
#### :house: Internal
7680

7781
- Remove uncurried flag from bsb. https://github.com/rescript-lang/rescript-compiler/pull/7049

analysis.opam

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file is generated by dune, edit dune-project instead
2+
opam-version: "2.0"
3+
synopsis: "ReScript Analysis"
4+
maintainer: ["Hongbo Zhang <bobzhang1988@gmail.com>" "Cristiano Calcagno"]
5+
authors: ["Hongbo Zhang <bobzhang1988@gmail.com>"]
6+
license: "LGPL-3.0-or-later"
7+
homepage: "https://github.com/rescript-lang/rescript-compiler"
8+
bug-reports: "https://github.com/rescript-lang/rescript-compiler/issues"
9+
depends: [
10+
"ocaml" {>= "4.10"}
11+
"cppo" {= "1.6.9"}
12+
"dune"
13+
]
14+
build: [
15+
["dune" "subst"] {pinned}
16+
[
17+
"dune"
18+
"build"
19+
"-p"
20+
name
21+
"-j"
22+
jobs
23+
"@install"
24+
"@runtest" {with-test}
25+
"@doc" {with-doc}
26+
]
27+
]

analysis/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Analysis Library and Binary
2+
3+
This subfolder builds a private command line binary used by the plugin to power a few functionalities such as jump to definition, hover and autocomplete.
4+
5+
The binary reads the `.cmt` and `.cmti` files and analyses them.
6+
7+
For installation & build instructions, see the main CONTRIBUTING.md.
8+
9+
## Overview
10+
11+
See main CONTRIBUTING.md's repo structure. Additionally, `examples/` is a convenience debugging repo. Check out `test.sh` (invoked through `make test`) to see the snapshots testing workflow stored in `tests/`.
12+
13+
## Usage
14+
15+
At root:
16+
```sh
17+
./rescript-editor-analysis.exe --help
18+
19+
# or
20+
21+
dune exec -- rescript-editor-analysis --help
22+
```
23+
24+
## History
25+
26+
This project is based on a fork of [Reason Language Server](https://github.com/jaredly/reason-language-server).

analysis/bin/dune

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(env
2+
(static
3+
(flags
4+
(:standard -ccopt -static))))
5+
6+
(executable
7+
(public_name rescript-editor-analysis)
8+
(package analysis)
9+
(modes byte exe)
10+
(name main)
11+
(libraries analysis))

analysis/bin/main.ml

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
open Analysis
2+
3+
let help =
4+
{|
5+
**Private CLI For rescript-vscode usage only**
6+
7+
API examples:
8+
./rescript-editor-analysis.exe completion src/MyFile.res 0 4 currentContent.res true
9+
./rescript-editor-analysis.exe definition src/MyFile.res 9 3
10+
./rescript-editor-analysis.exe typeDefinition src/MyFile.res 9 3
11+
./rescript-editor-analysis.exe documentSymbol src/Foo.res
12+
./rescript-editor-analysis.exe hover src/MyFile.res 10 2 true
13+
./rescript-editor-analysis.exe references src/MyFile.res 10 2
14+
./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo
15+
./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res
16+
./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25
17+
./rescript-editor-analysis.exe codeLens src/MyFile.res
18+
19+
Dev-time examples:
20+
./rescript-editor-analysis.exe dump src/MyFile.res src/MyFile2.res
21+
./rescript-editor-analysis.exe test src/MyFile.res
22+
23+
Note: positions are zero-indexed (start at 0 0), following LSP.
24+
https://microsoft.github.io/language-server-protocol/specification#position
25+
26+
Options:
27+
completion: compute autocomplete for MyFile.res at line 0 and column 4,
28+
where MyFile.res is being edited and the editor content is in file current.res.
29+
30+
./rescript-editor-analysis.exe completion src/MyFile.res 0 4 current.res
31+
32+
definition: get definition for item in MyFile.res at line 10 column 2:
33+
34+
./rescript-editor-analysis.exe definition src/MyFile.res 10 2
35+
36+
typeDefinition: get type definition for item in MyFile.res at line 10 column 2:
37+
38+
./rescript-editor-analysis.exe typeDefinition src/MyFile.res 10 2
39+
40+
documentSymbol: get all symbols declared in MyFile.res
41+
42+
./rescript-editor-analysis.exe documentSymbol src/MyFile.res
43+
44+
hover: get inferred type for MyFile.res at line 10 column 2 (supporting markdown links):
45+
46+
./rescript-editor-analysis.exe hover src/MyFile.res 10 2 true
47+
48+
references: get all references to item in MyFile.res at line 10 column 2:
49+
50+
./rescript-editor-analysis.exe references src/MyFile.res 10 2
51+
52+
rename: rename all appearances of item in MyFile.res at line 10 column 2 with foo:
53+
54+
./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo
55+
56+
semanticTokens: return token semantic highlighting info for MyFile.res
57+
58+
./rescript-editor-analysis.exe semanticTokens src/MyFile.res
59+
60+
createInterface: print to stdout the interface file for src/MyFile.res
61+
62+
./rescript-editor-analysis.exe createInterface src/MyFile.res lib/bs/src/MyFile.cmi
63+
64+
format: print to stdout the formatted version of the provided file
65+
66+
./rescript-editor-analysis.exe format src/MyFile.res
67+
68+
diagnosticSyntax: print to stdout diagnostic for syntax
69+
70+
./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res
71+
72+
inlayHint: get all inlay Hint between line 0 and 3 declared in MyFile.res. Last argument is maximum of character length for inlay hints
73+
74+
./rescript-editor-analysis.exe inlayHint src/MyFile.res 0 3 25
75+
76+
codeLens: get all code lens entries for file src/MyFile.res
77+
78+
./rescript-editor-analysis.exe codeLens src/MyFile.res
79+
80+
signatureHelp: get signature help if available for position at line 10 column 2 in src/MyFile.res
81+
82+
./rescript-editor-analysis.exe signatureHelp src/MyFile.res 10 2
83+
84+
test: run tests specified by special comments in file src/MyFile.res
85+
86+
./rescript-editor-analysis.exe test src/src/MyFile.res
87+
|}
88+
89+
let main () =
90+
let args = Array.to_list Sys.argv in
91+
let debugLevel, args =
92+
match args with
93+
| _ :: "debug-dump" :: logLevel :: rest ->
94+
( (match logLevel with
95+
| "verbose" -> Debug.Verbose
96+
| "regular" -> Regular
97+
| _ -> Off),
98+
"dummy" :: rest )
99+
| args -> (Off, args)
100+
in
101+
Debug.debugLevel := debugLevel;
102+
let debug = debugLevel <> Debug.Off in
103+
let printHeaderInfo path line col =
104+
if debug then
105+
Printf.printf "Debug level: %s\n%s:%s-%s\n\n"
106+
(match debugLevel with
107+
| Debug.Verbose -> "verbose"
108+
| Regular -> "regular"
109+
| Off -> "off")
110+
path line col
111+
in
112+
match args with
113+
| [_; "cache-project"; rootPath] -> (
114+
Cfg.readProjectConfigCache := false;
115+
let uri = Uri.fromPath rootPath in
116+
match Packages.getPackage ~uri with
117+
| Some package -> Cache.cacheProject package
118+
| None -> print_endline "\"ERR\"")
119+
| [_; "cache-delete"; rootPath] -> (
120+
Cfg.readProjectConfigCache := false;
121+
let uri = Uri.fromPath rootPath in
122+
match Packages.findRoot ~uri (Hashtbl.create 0) with
123+
| Some (`Bs rootPath) -> (
124+
match BuildSystem.getLibBs rootPath with
125+
| None -> print_endline "\"ERR\""
126+
| Some libBs ->
127+
Cache.deleteCache (Cache.targetFileFromLibBs libBs);
128+
print_endline "\"OK\"")
129+
| _ -> print_endline "\"ERR: Did not find root \"")
130+
| [_; "completion"; path; line; col; currentFile] ->
131+
printHeaderInfo path line col;
132+
Commands.completion ~debug ~path
133+
~pos:(int_of_string line, int_of_string col)
134+
~currentFile
135+
| [_; "completionResolve"; path; modulePath] ->
136+
Commands.completionResolve ~path ~modulePath
137+
| [_; "definition"; path; line; col] ->
138+
Commands.definition ~path
139+
~pos:(int_of_string line, int_of_string col)
140+
~debug
141+
| [_; "typeDefinition"; path; line; col] ->
142+
Commands.typeDefinition ~path
143+
~pos:(int_of_string line, int_of_string col)
144+
~debug
145+
| [_; "documentSymbol"; path] -> DocumentSymbol.command ~path
146+
| [_; "hover"; path; line; col; currentFile; supportsMarkdownLinks] ->
147+
Commands.hover ~path
148+
~pos:(int_of_string line, int_of_string col)
149+
~currentFile ~debug
150+
~supportsMarkdownLinks:
151+
(match supportsMarkdownLinks with
152+
| "true" -> true
153+
| _ -> false)
154+
| [
155+
_; "signatureHelp"; path; line; col; currentFile; allowForConstructorPayloads;
156+
] ->
157+
Commands.signatureHelp ~path
158+
~pos:(int_of_string line, int_of_string col)
159+
~currentFile ~debug
160+
~allowForConstructorPayloads:
161+
(match allowForConstructorPayloads with
162+
| "true" -> true
163+
| _ -> false)
164+
| [_; "inlayHint"; path; line_start; line_end; maxLength] ->
165+
Commands.inlayhint ~path
166+
~pos:(int_of_string line_start, int_of_string line_end)
167+
~maxLength ~debug
168+
| [_; "codeLens"; path] -> Commands.codeLens ~path ~debug
169+
| [_; "codeAction"; path; startLine; startCol; endLine; endCol; currentFile]
170+
->
171+
Commands.codeAction ~path
172+
~startPos:(int_of_string startLine, int_of_string startCol)
173+
~endPos:(int_of_string endLine, int_of_string endCol)
174+
~currentFile ~debug
175+
| [_; "codemod"; path; line; col; typ; hint] ->
176+
let typ =
177+
match typ with
178+
| "add-missing-cases" -> Codemod.AddMissingCases
179+
| _ -> raise (Failure "unsupported type")
180+
in
181+
let res =
182+
Codemod.transform ~path
183+
~pos:(int_of_string line, int_of_string col)
184+
~debug ~typ ~hint
185+
|> Json.escape
186+
in
187+
Printf.printf "\"%s\"" res
188+
| [_; "diagnosticSyntax"; path] -> Commands.diagnosticSyntax ~path
189+
| _ :: "reanalyze" :: _ ->
190+
let len = Array.length Sys.argv in
191+
for i = 1 to len - 2 do
192+
Sys.argv.(i) <- Sys.argv.(i + 1)
193+
done;
194+
Sys.argv.(len - 1) <- "";
195+
Reanalyze.cli ()
196+
| [_; "references"; path; line; col] ->
197+
Commands.references ~path
198+
~pos:(int_of_string line, int_of_string col)
199+
~debug
200+
| [_; "rename"; path; line; col; newName] ->
201+
Commands.rename ~path
202+
~pos:(int_of_string line, int_of_string col)
203+
~newName ~debug
204+
| [_; "semanticTokens"; currentFile] ->
205+
SemanticTokens.semanticTokens ~currentFile
206+
| [_; "createInterface"; path; cmiFile] ->
207+
Printf.printf "\"%s\""
208+
(Json.escape (CreateInterface.command ~path ~cmiFile))
209+
| [_; "format"; path] ->
210+
Printf.printf "\"%s\"" (Json.escape (Commands.format ~path))
211+
| [_; "test"; path] -> Commands.test ~path
212+
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help
213+
| _ ->
214+
prerr_endline help;
215+
exit 1
216+
;;
217+
218+
main ()

analysis/dune

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(dirs bin src reanalyze vendor)
2+
3+
(env
4+
(dev
5+
(env-vars
6+
(CPPO_FLAGS -U=RELEASE)))
7+
(release
8+
(env-vars
9+
(CPPO_FLAGS -D=RELEASE))
10+
(ocamlopt_flags
11+
(:standard -O3 -unbox-closures)))
12+
(static
13+
(env-vars
14+
(CPPO_FLAGS -D=RELEASE))
15+
(ocamlopt_flags
16+
(:standard -O3 -unbox-closures))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib
2+
.merlin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "tryit",
3+
"sources": "src",
4+
"bsc-flags": ["-bs-super-errors", "-open Belt"],
5+
"warnings": {
6+
"number": "-32-26-27-33"
7+
},
8+
"bs-dependencies": ["reason-react"],
9+
"reason": { "react-jsx": 3 },
10+
"namespace": "my-namespace",
11+
"reanalyze": {
12+
"analysis": ["dce", "exception"]
13+
}
14+
}

analysis/examples/example-project/package-lock.json

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)