tsz-cli 0.1.9

CLI binaries for the tsz TypeScript compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Diagnostic handlers for tsz-server.
//!
//! Handles semantic, syntactic, and suggestion diagnostic commands.

use super::{Server, TsServerRequest, TsServerResponse};

use tsz::checker::diagnostics::DiagnosticCategory;
use tsz::lsp::position::LineMap;
use tsz::parser::ParserState;

pub(crate) struct DiagnosticFormatInput<'a> {
    pub(crate) start_offset: u32,
    pub(crate) length: u32,
    pub(crate) message: &'a str,
    pub(crate) code: u32,
    pub(crate) category: DiagnosticCategory,
    pub(crate) line_map: &'a LineMap,
    pub(crate) content: &'a str,
    pub(crate) include_line_position: bool,
}

impl Server {
    pub(super) fn extract_auto_import_file_exclude_patterns(
        request: &TsServerRequest,
    ) -> Option<Vec<String>> {
        request
            .arguments
            .get("preferences")
            .and_then(|p| p.get("autoImportFileExcludePatterns"))
            .and_then(serde_json::Value::as_array)
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(std::string::ToString::to_string))
                    .collect()
            })
    }

    pub(super) fn extract_auto_import_specifier_exclude_regexes(
        request: &TsServerRequest,
    ) -> Option<Vec<String>> {
        request
            .arguments
            .get("preferences")
            .and_then(|p| p.get("autoImportSpecifierExcludeRegexes"))
            .and_then(serde_json::Value::as_array)
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(std::string::ToString::to_string))
                    .collect()
            })
    }

    pub(crate) fn handle_configure(
        &mut self,
        seq: u64,
        request: &TsServerRequest,
    ) -> TsServerResponse {
        let preferences = request
            .arguments
            .get("preferences")
            .unwrap_or(&request.arguments);

        self.completion_import_module_specifier_ending = preferences
            .get("importModuleSpecifierEnding")
            .and_then(|v| v.as_str())
            .map(std::string::ToString::to_string);
        self.import_module_specifier_preference = preferences
            .get("importModuleSpecifierPreference")
            .and_then(|v| v.as_str())
            .map(std::string::ToString::to_string);
        self.organize_imports_type_order = preferences
            .get("organizeImportsTypeOrder")
            .and_then(|v| v.as_str())
            .map(std::string::ToString::to_string);
        self.organize_imports_ignore_case = preferences
            .get("organizeImportsIgnoreCase")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(true);
        self.auto_import_file_exclude_patterns =
            Self::extract_auto_import_file_exclude_patterns(request).unwrap_or_default();
        self.auto_import_specifier_exclude_regexes =
            Self::extract_auto_import_specifier_exclude_regexes(request).unwrap_or_default();
        self.include_completions_with_class_member_snippets = preferences
            .get("includeCompletionsWithClassMemberSnippets")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);

        // Accept configuration; selected completion preferences are wired.
        TsServerResponse {
            seq,
            msg_type: "response".to_string(),
            command: "configure".to_string(),
            request_seq: request.seq,
            success: true,
            message: None,
            body: None,
        }
    }

    pub(crate) fn handle_semantic_diagnostics_sync(
        &mut self,
        seq: u64,
        request: &TsServerRequest,
    ) -> TsServerResponse {
        let file = request.arguments.get("file").and_then(|v| v.as_str());
        let include_line_position = request
            .arguments
            .get("includeLinePosition")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let diagnostics: Vec<serde_json::Value> = if let Some(file_path) = file {
            if let Some(content) = self.open_files.get(file_path).cloned() {
                let line_map = LineMap::build(&content);
                let mut full_diags = self.get_semantic_diagnostics_full(file_path, &content);
                let has_module_none_diagnostic = full_diags.iter().any(|d| {
                    d.code
                        == tsz_checker::diagnostics::diagnostic_codes::CANNOT_USE_IMPORTS_EXPORTS_OR_MODULE_AUGMENTATIONS_WHEN_MODULE_IS_NONE
                });
                if !has_module_none_diagnostic
                    && full_diags.iter().all(|d| {
                        d.code != tsz_checker::diagnostics::diagnostic_codes::CANNOT_FIND_NAME
                    })
                    && let Some((_, binder, _, _)) = self.parse_and_bind_file(file_path)
                {
                    full_diags.extend(self.synthetic_missing_name_expression_diagnostics(
                        file_path, &content, &binder,
                    ));
                }
                if full_diags.iter().all(|d| d.code != 2420) {
                    full_diags.extend(
                        self.synthetic_implements_interface_diagnostics(file_path, &content),
                    );
                }
                full_diags
                    .iter()
                    .map(|diag| {
                        let (start_offset, length) =
                            Self::normalized_diagnostic_span(diag, &content);
                        Self::format_diagnostic(DiagnosticFormatInput {
                            start_offset,
                            length,
                            message: &diag.message_text,
                            code: diag.code,
                            category: diag.category,
                            line_map: &line_map,
                            content: &content,
                            include_line_position,
                        })
                    })
                    .collect()
            } else {
                vec![]
            }
        } else {
            vec![]
        };

        TsServerResponse {
            seq,
            msg_type: "response".to_string(),
            command: "semanticDiagnosticsSync".to_string(),
            request_seq: request.seq,
            success: true,
            message: None,
            body: Some(serde_json::json!(diagnostics)),
        }
    }

    pub(crate) fn handle_syntactic_diagnostics_sync(
        &mut self,
        seq: u64,
        request: &TsServerRequest,
    ) -> TsServerResponse {
        let file = request.arguments.get("file").and_then(|v| v.as_str());
        let include_line_position = request
            .arguments
            .get("includeLinePosition")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let diagnostics: Vec<serde_json::Value> = if let Some(file_path) = file {
            if let Some(content) = self.open_files.get(file_path).cloned() {
                let line_map = LineMap::build(&content);
                let mut parser = ParserState::new(file_path.to_string(), content.clone());
                let _root = parser.parse_source_file();
                parser
                    .get_diagnostics()
                    .iter()
                    .map(|d| {
                        Self::format_diagnostic(DiagnosticFormatInput {
                            start_offset: d.start,
                            length: d.length,
                            message: &d.message,
                            code: d.code,
                            category: DiagnosticCategory::Error,
                            line_map: &line_map,
                            content: &content,
                            include_line_position,
                        })
                    })
                    .collect()
            } else {
                vec![]
            }
        } else {
            vec![]
        };

        TsServerResponse {
            seq,
            msg_type: "response".to_string(),
            command: "syntacticDiagnosticsSync".to_string(),
            request_seq: request.seq,
            success: true,
            message: None,
            body: Some(serde_json::json!(diagnostics)),
        }
    }

    /// Format a diagnostic for the tsserver protocol.
    ///
    /// When `include_line_position` is true (the `SessionClient` always sets this),
    /// the response includes 0-based `start`/`length` fields plus `startLocation`/
    /// `endLocation` with 1-based line/offset. When false, uses `start`/`end` as
    /// 1-based line/offset objects (the traditional tsserver format).
    pub(crate) fn format_diagnostic(input: DiagnosticFormatInput<'_>) -> serde_json::Value {
        let start_pos = input
            .line_map
            .offset_to_position(input.start_offset, input.content);
        let end_pos = input
            .line_map
            .offset_to_position(input.start_offset + input.length, input.content);
        let cat_str = match input.category {
            DiagnosticCategory::Error => "error",
            DiagnosticCategory::Warning => "warning",
            _ => "suggestion",
        };

        if input.include_line_position {
            // When includeLinePosition is true, the harness expects:
            // - start: 0-based byte offset (number)
            // - length: byte length (number)
            // - startLocation: {line, offset} (1-based)
            // - endLocation: {line, offset} (1-based)
            // - message: the diagnostic text
            // - category: category string
            // - code: error code
            serde_json::json!({
                "start": input.start_offset,
                "length": input.length,
                "startLocation": {
                    "line": start_pos.line + 1,
                    "offset": start_pos.character + 1,
                },
                "endLocation": {
                    "line": end_pos.line + 1,
                    "offset": end_pos.character + 1,
                },
                "message": input.message,
                "code": input.code,
                "category": cat_str,
            })
        } else {
            // Traditional tsserver format: start/end as {line, offset}
            serde_json::json!({
                "start": {
                    "line": start_pos.line + 1,
                    "offset": start_pos.character + 1,
                },
                "end": {
                    "line": end_pos.line + 1,
                    "offset": end_pos.character + 1,
                },
                "text": input.message,
                "code": input.code,
                "category": cat_str,
            })
        }
    }

    fn normalized_diagnostic_span(
        diag: &tsz::checker::diagnostics::Diagnostic,
        content: &str,
    ) -> (u32, u32) {
        if diag.code
            != tsz_checker::diagnostics::diagnostic_codes::CANNOT_USE_IMPORTS_EXPORTS_OR_MODULE_AUGMENTATIONS_WHEN_MODULE_IS_NONE
        {
            return (diag.start, diag.length);
        }

        let Ok(start) = usize::try_from(diag.start) else {
            return (diag.start, diag.length);
        };
        let Some(mut end) = start.checked_add(diag.length as usize) else {
            return (diag.start, diag.length);
        };
        end = end.min(content.len());
        if start >= end || start >= content.len() {
            return (diag.start, diag.length);
        }

        let bytes = content.as_bytes();
        let slice = &bytes[start..end];
        if let Some(rel) = slice.iter().position(|b| *b == b';') {
            return (diag.start, (rel + 1) as u32);
        }
        if let Some(rel) = slice.iter().position(|b| *b == b'\n') {
            return (diag.start, rel as u32);
        }

        (diag.start, diag.length)
    }

    pub(crate) fn handle_suggestion_diagnostics_sync(
        &mut self,
        seq: u64,
        request: &TsServerRequest,
    ) -> TsServerResponse {
        let file = request.arguments.get("file").and_then(|v| v.as_str());
        let include_line_position = request
            .arguments
            .get("includeLinePosition")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let diagnostics: Vec<serde_json::Value> = if let Some(file_path) = file {
            if let Some(content) = self.open_files.get(file_path).cloned() {
                let line_map = LineMap::build(&content);
                let mut diags = self.get_suggestion_diagnostics(file_path, &content);
                if diags.iter().all(|d| d.code != 80004)
                    && let Some(diag) =
                        Self::synthetic_jsdoc_suggestion_diagnostic(file_path, &content)
                {
                    diags.push(diag);
                }
                if diags.iter().all(|d| d.code != 1308)
                    && let Some(diag) =
                        Self::synthetic_missing_async_suggestion_diagnostic(file_path, &content)
                {
                    diags.push(diag);
                }
                let has_jsdoc_type_tags = content.contains("@type {")
                    || content.contains("@param {")
                    || content.contains("@return {")
                    || content.contains("@returns {");
                if !has_jsdoc_type_tags
                    && diags.iter().all(|d| d.code != 7006)
                    && let Some(diag) = Self::synthetic_add_parameter_names_suggestion_diagnostic(
                        file_path, &content,
                    )
                {
                    diags.push(diag);
                }
                if diags.iter().all(|d| d.code != 2739)
                    && let Some(diag) = Self::synthetic_missing_attributes_suggestion_diagnostic(
                        file_path, &content,
                    )
                {
                    diags.push(diag);
                }
                if diags.iter().all(|d| d.code != 7043 && d.code != 7044) {
                    diags.extend(Self::synthetic_jsdoc_infer_from_usage_diagnostics(
                        file_path, &content,
                    ));
                }
                diags
                    .iter()
                    .map(|d| {
                        Self::format_diagnostic(DiagnosticFormatInput {
                            start_offset: d.start,
                            length: d.length,
                            message: &d.message_text,
                            code: d.code,
                            category: d.category,
                            line_map: &line_map,
                            content: &content,
                            include_line_position,
                        })
                    })
                    .collect()
            } else {
                vec![]
            }
        } else {
            vec![]
        };

        TsServerResponse {
            seq,
            msg_type: "response".to_string(),
            command: "suggestionDiagnosticsSync".to_string(),
            request_seq: request.seq,
            success: true,
            message: None,
            body: Some(serde_json::json!(diagnostics)),
        }
    }

    pub(crate) fn handle_geterr(
        &mut self,
        seq: u64,
        request: &TsServerRequest,
    ) -> TsServerResponse {
        // geterr is async in tsserver - it fires diagnostic events
        // For now, just acknowledge the request
        self.stub_response(seq, request, None)
    }

    pub(crate) fn handle_geterr_for_project(
        &mut self,
        seq: u64,
        request: &TsServerRequest,
    ) -> TsServerResponse {
        self.stub_response(seq, request, None)
    }
}