Skip to content

Commit 3ea3cd0

Browse files
authored
feat: support export ast buffer (#317)
1 parent bcd55cf commit 3ea3cd0

File tree

23 files changed

+728
-33
lines changed

23 files changed

+728
-33
lines changed

.github/actions/setup-go/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ runs:
4949
~/.cache/go-build
5050
~/Library/Caches/go-build
5151
~/AppData/Local/go-build
52+
53+
- name: Patch tsgo
54+
shell: bash
55+
run: ./scripts/apply-tsgo-path.sh

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ jobs:
112112
run: pnpm format:check
113113

114114
- name: Build
115-
run: pnpm -r --filter='@rslint/test-tools...' --filter='rslint...' build
115+
run: pnpm run build
116116

117117
- name: Dogfooding
118118
if: runner.os == 'Linux'
@@ -138,11 +138,11 @@ jobs:
138138
139139
- name: Test on Linux
140140
if: runner.os == 'Linux'
141-
run: xvfb-run -a pnpm -r --filter='@rslint/test-tools...' --filter='rslint...' test
141+
run: xvfb-run -a pnpm run test
142142

143143
- name: Test on non-Linux
144144
if: runner.os != 'Linux'
145-
run: pnpm -r --filter='@rslint/test-tools...' --filter='rslint...' test
145+
run: pnpm run test
146146

147147
test-wasm:
148148
name: Test WASM

__patches__/typescript-go.patch

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
diff --git a/_packages/api/src/api.ts b/_packages/api/src/api.ts
2+
index fbfc32391..f8487d67c 100644
3+
--- a/_packages/api/src/api.ts
4+
+++ b/_packages/api/src/api.ts
5+
@@ -16,7 +16,7 @@ import type {
6+
TypeResponse,
7+
} from "./proto.ts";
8+
9+
-export { SymbolFlags, TypeFlags };
10+
+export { SymbolFlags, TypeFlags, RemoteSourceFile, type Node };
11+
12+
export interface APIOptions {
13+
tsserverPath: string;
14+
diff --git a/_packages/api/src/client.ts b/_packages/api/src/client.ts
15+
index 2c9e21e44..a7f59ac2e 100644
16+
--- a/_packages/api/src/client.ts
17+
+++ b/_packages/api/src/client.ts
18+
@@ -29,12 +29,12 @@ export class Client {
19+
);
20+
21+
if (options.fs) {
22+
- for (const [key, callback] of Object.entries(options.fs)) {
23+
- this.channel.registerCallback(key, (_, arg) => {
24+
- const result = callback(JSON.parse(arg));
25+
- return JSON.stringify(result) ?? "";
26+
- });
27+
- }
28+
+ // for (const [key, callback] of Object.entries(options.fs)) {
29+
+ // this.channel.registerCallback(key, (_, arg) => {
30+
+ // const result = callback(JSON.parse(arg));
31+
+ // return JSON.stringify(result) ?? "";
32+
+ // });
33+
+ // }
34+
}
35+
}
36+

cmd/rslint/api.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,14 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
128128
programs = append(programs, program)
129129
}
130130

131-
// Collect diagnostics
131+
// Collect diagnostics and source files
132132
var diagnostics []api.Diagnostic
133133
var diagnosticsLock sync.Mutex
134134
errorsCount := 0
135+
136+
// Track source files for encoding
137+
sourceFiles := make(map[string]*ast.SourceFile)
138+
var sourceFilesLock sync.Mutex
135139

136140
// Create collector function
137141
diagnosticCollector := func(d rule.RuleDiagnostic) {
@@ -181,6 +185,12 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
181185

182186
diagnostics = append(diagnostics, diagnostic)
183187
errorsCount++
188+
189+
// Track source file for encoding
190+
sourceFilesLock.Lock()
191+
filePath := tspath.ConvertToRelativePath(d.SourceFile.FileName(), comparePathOptions)
192+
sourceFiles[filePath] = d.SourceFile
193+
sourceFilesLock.Unlock()
184194
}
185195

186196
// Run linter
@@ -209,13 +219,30 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
209219
if diagnostics == nil {
210220
diagnostics = []api.Diagnostic{}
211221
}
222+
212223
// Create response
213-
return &api.LintResponse{
224+
response := &api.LintResponse{
214225
Diagnostics: diagnostics,
215226
ErrorCount: errorsCount,
216227
FileCount: int(lintedFilesCount),
217228
RuleCount: len(rulesWithOptions),
218-
}, nil
229+
}
230+
// Only include encoded source files if requested
231+
if req.IncludeEncodedSourceFiles {
232+
encodedSourceFiles := make(map[string]api.ByteArray)
233+
for filePath, sourceFile := range sourceFiles {
234+
encoded, err := api.EncodeAST(sourceFile, filePath)
235+
236+
if err != nil {
237+
// Log error but don't fail the entire request
238+
fmt.Fprintf(os.Stderr, "warning: failed to encode source file %s: %v\n", filePath, err)
239+
continue
240+
}
241+
encodedSourceFiles[filePath] = encoded
242+
}
243+
response.EncodedSourceFiles = encodedSourceFiles
244+
}
245+
return response, nil
219246
}
220247

221248
// HandleApplyFixes handles apply fixes requests in IPC mode

internal/api/api.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type LintRequest struct {
6868
RuleOptions map[string]interface{} `json:"ruleOptions,omitempty"`
6969
FileContents map[string]string `json:"fileContents,omitempty"` // Map of file paths to their contents for VFS
7070
LanguageOptions *LanguageOptions `json:"languageOptions,omitempty"` // Override languageOptions from config file
71+
IncludeEncodedSourceFiles bool `json:"includeEncodedSourceFiles,omitempty"` // Whether to include encoded source files in response
7172
}
7273

7374
// LanguageOptions contains language-specific configuration options
@@ -101,13 +102,15 @@ type ParserOptions struct {
101102
ProjectService bool `json:"projectService"`
102103
Project ProjectPaths `json:"project,omitempty"`
103104
}
105+
type ByteArray []byte
104106

105107
// LintResponse represents a lint response from Go to JS
106108
type LintResponse struct {
107-
Diagnostics []Diagnostic `json:"diagnostics"`
108-
ErrorCount int `json:"errorCount"`
109-
FileCount int `json:"fileCount"`
110-
RuleCount int `json:"ruleCount"`
109+
Diagnostics []Diagnostic `json:"diagnostics"`
110+
ErrorCount int `json:"errorCount"`
111+
FileCount int `json:"fileCount"`
112+
RuleCount int `json:"ruleCount"`
113+
EncodedSourceFiles map[string]ByteArray `json:"encodedSourceFiles,omitempty"`
111114
}
112115

113116
// ApplyFixesRequest represents a request to apply fixes from JS to Go
@@ -339,6 +342,8 @@ func (s *Service) sendResponse(id int, data interface{}) {
339342
}
340343
}
341344

345+
346+
342347
// sendError sends an error message
343348
func (s *Service) sendError(id int, message string) {
344349
msg := &Message{

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"license": "MIT",
1515
"scripts": {
16-
"build": "pnpm -r build",
16+
"build": "pnpm -r --filter=!@typescript/api --filter=!@typescript/ast --filter='@rslint/test-tools...' --filter='rslint...' build",
1717
"build:npm": "zx scripts/build-npm.mjs",
1818
"build:website": "pnpm -F @rslint/website... -r build",
1919
"check-spell": "pnpx cspell",
@@ -23,7 +23,7 @@
2323
"publish:ovsx": "zx scripts/publish-marketplace.mjs --marketplace=ovsx",
2424
"format": "prettier --ignore-path=.prettierignore --config=.prettierrc.json --write .",
2525
"format:check": "prettier --ignore-path=.prettierignore --config=.prettierrc.json --check .",
26-
"test": "pnpm -r test",
26+
"test": "pnpm -r --filter=!@typescript/api --filter=!@typescript/ast --filter='@rslint/test-tools...' --filter='rslint...' test",
2727
"test:go": "go test ./internal/...",
2828
"typecheck": "pnpm tsc -b tsconfig.json",
2929
"lint": "rslint",

packages/rslint-api/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.rlib

packages/rslint-api/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# rslint-api
2+
3+
reexport tsgo api

packages/rslint-api/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@rslint/api",
3+
"version": "0.1.12",
4+
"description": "Rslint AST library",
5+
"main": "dist/index.mjs",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "rslib build",
9+
"compile": "tsc -p tsconfig.build.json"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"packageManager": "[email protected]",
15+
"devDependencies": {
16+
"@typescript/api": "workspace:*",
17+
"@rslib/core": "0.12.4",
18+
"@types/node": "24.3.0"
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineConfig } from '@rslib/core';
2+
3+
export default defineConfig({
4+
lib: [
5+
{
6+
format: 'esm',
7+
dts: {
8+
bundle: true,
9+
},
10+
},
11+
],
12+
source: {
13+
tsconfigPath: './tsconfig.build.json',
14+
},
15+
resolve: {
16+
alias: {
17+
'@typescript/libsyncrpc': false,
18+
},
19+
},
20+
tools: {
21+
rspack(config) {
22+
if (!config.resolve?.conditionNames) {
23+
config.resolve.conditionNames = ['...'];
24+
}
25+
config.resolve.conditionNames.unshift('@typescript/source');
26+
return config;
27+
},
28+
},
29+
});

0 commit comments

Comments
 (0)