@teambit/ts-server 0.0.59 → 0.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/format-diagnostics.js +38 -32
- package/dist/format-diagnostics.js.map +1 -1
- package/dist/index.js +18 -4
- package/dist/index.js.map +1 -1
- package/dist/modules-resolver.js +43 -46
- package/dist/modules-resolver.js.map +1 -1
- package/dist/process-based-tsserver.js +238 -227
- package/dist/process-based-tsserver.js.map +1 -1
- package/dist/ts-server-client.d.ts +1 -1
- package/dist/ts-server-client.js +348 -328
- package/dist/ts-server-client.js.map +1 -1
- package/dist/tsp-command-types.js +170 -166
- package/dist/tsp-command-types.js.map +1 -1
- package/dist/utils.js +15 -11
- package/dist/utils.js.map +1 -1
- package/package.json +22 -8
- package/types/asset.d.ts +15 -3
- package/dist/preview-1734020193676.js +0 -7
package/dist/ts-server-client.js
CHANGED
|
@@ -1,349 +1,369 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
15
6
|
exports.TsserverClient = void 0;
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
7
|
+
function _fsExtra() {
|
|
8
|
+
const data = _interopRequireDefault(require("fs-extra"));
|
|
9
|
+
_fsExtra = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _path() {
|
|
15
|
+
const data = _interopRequireDefault(require("path"));
|
|
16
|
+
_path = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _watcher() {
|
|
22
|
+
const data = require("@teambit/watcher");
|
|
23
|
+
_watcher = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _commandExists() {
|
|
29
|
+
const data = _interopRequireDefault(require("command-exists"));
|
|
30
|
+
_commandExists = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _modulesResolver() {
|
|
36
|
+
const data = require("./modules-resolver");
|
|
37
|
+
_modulesResolver = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _processBasedTsserver() {
|
|
43
|
+
const data = require("./process-based-tsserver");
|
|
44
|
+
_processBasedTsserver = function () {
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
function _tspCommandTypes() {
|
|
50
|
+
const data = require("./tsp-command-types");
|
|
51
|
+
_tspCommandTypes = function () {
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
function _utils() {
|
|
57
|
+
const data = require("./utils");
|
|
58
|
+
_utils = function () {
|
|
59
|
+
return data;
|
|
60
|
+
};
|
|
61
|
+
return data;
|
|
62
|
+
}
|
|
63
|
+
function _formatDiagnostics() {
|
|
64
|
+
const data = require("./format-diagnostics");
|
|
65
|
+
_formatDiagnostics = function () {
|
|
66
|
+
return data;
|
|
67
|
+
};
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
25
71
|
class TsserverClient {
|
|
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
|
-
const failedFiles = openPromises.filter((promise) => promise instanceof Error);
|
|
69
|
-
if (failedFiles.length > 0) {
|
|
70
|
-
this.logger.error('TsserverClient.init failed to open files:', failedFiles);
|
|
71
|
-
}
|
|
72
|
-
if (failedFiles.length > 0) {
|
|
73
|
-
this.logger.error('TsserverClient.init failed to open files:', failedFiles);
|
|
74
|
-
}
|
|
75
|
-
this.checkTypesIfNeeded();
|
|
76
|
-
}
|
|
77
|
-
this.logger.debug('TsserverClient.init completed');
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
this.logger.error('TsserverClient.init failed', err);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
checkTypesIfNeeded(files = this.files) {
|
|
85
|
-
if (!this.shouldCheckTypes()) {
|
|
86
|
-
return;
|
|
72
|
+
lastDiagnostics = [];
|
|
73
|
+
serverRunning = false;
|
|
74
|
+
constructor(
|
|
75
|
+
/**
|
|
76
|
+
* absolute root path of the project.
|
|
77
|
+
*/
|
|
78
|
+
projectPath, logger, options = {},
|
|
79
|
+
/**
|
|
80
|
+
* provide files if you want to check types on init. (options.checkTypes should be enabled).
|
|
81
|
+
* paths should be absolute.
|
|
82
|
+
*/
|
|
83
|
+
files = []) {
|
|
84
|
+
this.projectPath = projectPath;
|
|
85
|
+
this.logger = logger;
|
|
86
|
+
this.options = options;
|
|
87
|
+
this.files = files;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* start the ts-server and keep its process alive.
|
|
92
|
+
* this methods returns pretty fast. if checkTypes is enabled, it runs the process in the background and
|
|
93
|
+
* doesn't wait for it.
|
|
94
|
+
*/
|
|
95
|
+
async init() {
|
|
96
|
+
try {
|
|
97
|
+
this.tsServer = new (_processBasedTsserver().ProcessBasedTsServer)({
|
|
98
|
+
logger: this.logger,
|
|
99
|
+
tsserverPath: this.findTsserverPath(),
|
|
100
|
+
logToConsole: this.options.verbose,
|
|
101
|
+
onEvent: this.onTsserverEvent.bind(this)
|
|
102
|
+
});
|
|
103
|
+
this.tsServer.start().then(() => {
|
|
104
|
+
this.serverRunning = true;
|
|
105
|
+
}).catch(err => {
|
|
106
|
+
this.logger.error('TsserverClient.init failed', err);
|
|
107
|
+
});
|
|
108
|
+
if (this.files.length) {
|
|
109
|
+
const openPromises = this.files.map(file => this.open(file));
|
|
110
|
+
await Promise.all(openPromises.map(promise => promise.catch(error => error)));
|
|
111
|
+
const failedFiles = openPromises.filter(promise => promise instanceof Error);
|
|
112
|
+
if (failedFiles.length > 0) {
|
|
113
|
+
this.logger.error('TsserverClient.init failed to open files:', failedFiles);
|
|
87
114
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
.then(() => {
|
|
91
|
-
const end = Date.now() - start;
|
|
92
|
-
const msg = `completed type checking (${end / 1000} sec)`;
|
|
93
|
-
if (this.lastDiagnostics.length) {
|
|
94
|
-
this.logger.consoleFailure(`${msg}. found errors in ${this.lastDiagnostics.length} files.`);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
this.logger.consoleSuccess(`${msg}. no errors were found.`);
|
|
98
|
-
}
|
|
99
|
-
})
|
|
100
|
-
.catch((err) => {
|
|
101
|
-
const msg = `failed getting the type errors from ts-server`;
|
|
102
|
-
this.logger.console(msg);
|
|
103
|
-
this.logger.error(msg, err);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
shouldCheckTypes() {
|
|
107
|
-
// this also covers this.options.checkTypes !== CheckTypes.None.
|
|
108
|
-
return Boolean(this.options.checkTypes);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* if `bit watch` or `bit start` are running in the background, this method is triggered.
|
|
112
|
-
*/
|
|
113
|
-
onFileChange(file) {
|
|
114
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
-
yield this.changed(file);
|
|
116
|
-
const files = this.options.checkTypes === watcher_1.CheckTypes.ChangedFile ? [file] : undefined;
|
|
117
|
-
this.checkTypesIfNeeded(files);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
killTsServer() {
|
|
121
|
-
if (this.tsServer && this.serverRunning) {
|
|
122
|
-
this.tsServer.kill();
|
|
123
|
-
this.tsServer = null;
|
|
124
|
-
this.serverRunning = false;
|
|
115
|
+
if (failedFiles.length > 0) {
|
|
116
|
+
this.logger.error('TsserverClient.init failed to open files:', failedFiles);
|
|
125
117
|
}
|
|
118
|
+
this.checkTypesIfNeeded();
|
|
119
|
+
}
|
|
120
|
+
this.logger.debug('TsserverClient.init completed');
|
|
121
|
+
} catch (err) {
|
|
122
|
+
this.logger.error('TsserverClient.init failed', err);
|
|
126
123
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
* get diagnostic of all files opened in the project.
|
|
132
|
-
* there is little to no value of getting diagnostic for a specific file, as
|
|
133
|
-
* changing a type in one file may cause errors in different files.
|
|
134
|
-
*
|
|
135
|
-
* the errors/diagnostic info are sent as events, see this.onTsserverEvent() for more info.
|
|
136
|
-
*
|
|
137
|
-
* the return value here just shows whether the request was succeeded, it doesn't have any info about whether errors
|
|
138
|
-
* were found or not.
|
|
139
|
-
*/
|
|
140
|
-
getDiagnostic(files = this.files) {
|
|
141
|
-
var _a;
|
|
142
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
this.lastDiagnostics = [];
|
|
144
|
-
return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.Geterr, { delay: 0, files });
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* avoid using this method, it takes longer than `getDiagnostic()` and shows errors from paths outside the project
|
|
149
|
-
*/
|
|
150
|
-
getDiagnosticAllProject(requestedByFile) {
|
|
151
|
-
var _a;
|
|
152
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.GeterrForProject, { file: requestedByFile, delay: 0 });
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* @param file can be absolute or relative to this.projectRoot.
|
|
158
|
-
*/
|
|
159
|
-
getQuickInfo(file, position) {
|
|
160
|
-
var _a;
|
|
161
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
-
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
163
|
-
yield this.openIfNeeded(absFile);
|
|
164
|
-
return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.Quickinfo, {
|
|
165
|
-
file: absFile,
|
|
166
|
-
line: position.line,
|
|
167
|
-
offset: position.character,
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* @param file can be absolute or relative to this.projectRoot.
|
|
173
|
-
*/
|
|
174
|
-
getTypeDefinition(file, position) {
|
|
175
|
-
var _a;
|
|
176
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
178
|
-
yield this.openIfNeeded(absFile);
|
|
179
|
-
return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.TypeDefinition, {
|
|
180
|
-
file: absFile,
|
|
181
|
-
line: position.line,
|
|
182
|
-
offset: position.character,
|
|
183
|
-
});
|
|
184
|
-
});
|
|
124
|
+
}
|
|
125
|
+
checkTypesIfNeeded(files = this.files) {
|
|
126
|
+
if (!this.shouldCheckTypes()) {
|
|
127
|
+
return;
|
|
185
128
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
129
|
+
const start = Date.now();
|
|
130
|
+
this.getDiagnostic(files).then(() => {
|
|
131
|
+
const end = Date.now() - start;
|
|
132
|
+
const msg = `completed type checking (${end / 1000} sec)`;
|
|
133
|
+
if (this.lastDiagnostics.length) {
|
|
134
|
+
this.logger.consoleFailure(`${msg}. found errors in ${this.lastDiagnostics.length} files.`);
|
|
135
|
+
} else {
|
|
136
|
+
this.logger.consoleSuccess(`${msg}. no errors were found.`);
|
|
137
|
+
}
|
|
138
|
+
}).catch(err => {
|
|
139
|
+
const msg = `failed getting the type errors from ts-server`;
|
|
140
|
+
this.logger.console(msg);
|
|
141
|
+
this.logger.error(msg, err);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
shouldCheckTypes() {
|
|
145
|
+
// this also covers this.options.checkTypes !== CheckTypes.None.
|
|
146
|
+
return Boolean(this.options.checkTypes);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* if `bit watch` or `bit start` are running in the background, this method is triggered.
|
|
151
|
+
*/
|
|
152
|
+
async onFileChange(file) {
|
|
153
|
+
await this.changed(file);
|
|
154
|
+
const files = this.options.checkTypes === _watcher().CheckTypes.ChangedFile ? [file] : undefined;
|
|
155
|
+
this.checkTypesIfNeeded(files);
|
|
156
|
+
}
|
|
157
|
+
killTsServer() {
|
|
158
|
+
if (this.tsServer && this.serverRunning) {
|
|
159
|
+
this.tsServer.kill();
|
|
160
|
+
this.tsServer = null;
|
|
161
|
+
this.serverRunning = false;
|
|
203
162
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
163
|
+
}
|
|
164
|
+
isServerRunning() {
|
|
165
|
+
return this.serverRunning;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* get diagnostic of all files opened in the project.
|
|
170
|
+
* there is little to no value of getting diagnostic for a specific file, as
|
|
171
|
+
* changing a type in one file may cause errors in different files.
|
|
172
|
+
*
|
|
173
|
+
* the errors/diagnostic info are sent as events, see this.onTsserverEvent() for more info.
|
|
174
|
+
*
|
|
175
|
+
* the return value here just shows whether the request was succeeded, it doesn't have any info about whether errors
|
|
176
|
+
* were found or not.
|
|
177
|
+
*/
|
|
178
|
+
async getDiagnostic(files = this.files) {
|
|
179
|
+
this.lastDiagnostics = [];
|
|
180
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.Geterr, {
|
|
181
|
+
delay: 0,
|
|
182
|
+
files
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* avoid using this method, it takes longer than `getDiagnostic()` and shows errors from paths outside the project
|
|
188
|
+
*/
|
|
189
|
+
async getDiagnosticAllProject(requestedByFile) {
|
|
190
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.GeterrForProject, {
|
|
191
|
+
file: requestedByFile,
|
|
192
|
+
delay: 0
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param file can be absolute or relative to this.projectRoot.
|
|
198
|
+
*/
|
|
199
|
+
async getQuickInfo(file, position) {
|
|
200
|
+
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
201
|
+
await this.openIfNeeded(absFile);
|
|
202
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.Quickinfo, {
|
|
203
|
+
file: absFile,
|
|
204
|
+
line: position.line,
|
|
205
|
+
offset: position.character
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* @param file can be absolute or relative to this.projectRoot.
|
|
211
|
+
*/
|
|
212
|
+
async getTypeDefinition(file, position) {
|
|
213
|
+
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
214
|
+
await this.openIfNeeded(absFile);
|
|
215
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.TypeDefinition, {
|
|
216
|
+
file: absFile,
|
|
217
|
+
line: position.line,
|
|
218
|
+
offset: position.character
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
async getDefinition(file, position) {
|
|
222
|
+
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
223
|
+
await this.openIfNeeded(absFile);
|
|
224
|
+
const response = await this.tsServer?.request(_tspCommandTypes().CommandTypes.Definition, {
|
|
225
|
+
file: absFile,
|
|
226
|
+
line: position.line,
|
|
227
|
+
offset: position.character
|
|
228
|
+
});
|
|
229
|
+
if (!response?.success) {
|
|
230
|
+
// TODO: we need a function to handle responses properly here for all.
|
|
231
|
+
this.logger.warn(`For file ${absFile} tsserver failed to request definition info`);
|
|
232
|
+
return response;
|
|
218
233
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
234
|
+
return response;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @param file can be absolute or relative to this.projectRoot.
|
|
239
|
+
*/
|
|
240
|
+
async getReferences(file, position) {
|
|
241
|
+
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
242
|
+
await this.openIfNeeded(absFile);
|
|
243
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.References, {
|
|
244
|
+
file: absFile,
|
|
245
|
+
line: position.line,
|
|
246
|
+
offset: position.character
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* @param file can be absolute or relative to this.projectRoot.
|
|
252
|
+
*/
|
|
253
|
+
async getSignatureHelp(file, position) {
|
|
254
|
+
const absFile = this.convertFileToAbsoluteIfNeeded(file);
|
|
255
|
+
await this.openIfNeeded(absFile);
|
|
256
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.SignatureHelp, {
|
|
257
|
+
file: absFile,
|
|
258
|
+
line: position.line,
|
|
259
|
+
offset: position.character
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
async configure(configureArgs = {}) {
|
|
263
|
+
return this.tsServer?.request(_tspCommandTypes().CommandTypes.Configure, configureArgs);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* ask tsserver to open a file if it was not opened before.
|
|
268
|
+
* @param file absolute path of the file
|
|
269
|
+
*/
|
|
270
|
+
async openIfNeeded(file) {
|
|
271
|
+
if (this.files.includes(file)) {
|
|
272
|
+
return;
|
|
233
273
|
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
274
|
+
await this.open(file);
|
|
275
|
+
this.files.push(file);
|
|
276
|
+
}
|
|
277
|
+
async open(file) {
|
|
278
|
+
return this.tsServer?.notify(_tspCommandTypes().CommandTypes.Open, {
|
|
279
|
+
file,
|
|
280
|
+
projectRootPath: this.projectPath
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
async close(file) {
|
|
284
|
+
await this.tsServer?.notify(_tspCommandTypes().CommandTypes.Close, {
|
|
285
|
+
file
|
|
286
|
+
});
|
|
287
|
+
this.files = this.files.filter(openFile => openFile !== file);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* since Bit is not an IDE, it doesn't have the information such as the exact line/offset of the changes.
|
|
292
|
+
* as a workaround, to tell tsserver what was changed, we pretend that the entire file was cleared and new text was
|
|
293
|
+
* added. this is the only way I could find to tell tsserver about the change. otherwise, tsserver keep assuming that
|
|
294
|
+
* the file content remained the same. (closing/re-opening the file doesn't help).
|
|
295
|
+
*/
|
|
296
|
+
async changed(file) {
|
|
297
|
+
// tell tsserver that all content was removed
|
|
298
|
+
await this.tsServer?.notify(_tspCommandTypes().CommandTypes.Change, {
|
|
299
|
+
file,
|
|
300
|
+
line: 1,
|
|
301
|
+
offset: 1,
|
|
302
|
+
endLine: 99999,
|
|
303
|
+
endOffset: 1,
|
|
304
|
+
insertString: ''
|
|
305
|
+
});
|
|
306
|
+
const content = await _fsExtra().default.readFile(file, 'utf-8');
|
|
307
|
+
|
|
308
|
+
// tell tsserver that all file content was added
|
|
309
|
+
await this.tsServer?.notify(_tspCommandTypes().CommandTypes.Change, {
|
|
310
|
+
file,
|
|
311
|
+
line: 1,
|
|
312
|
+
offset: 1,
|
|
313
|
+
endLine: 1,
|
|
314
|
+
endOffset: 1,
|
|
315
|
+
insertString: content
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
onTsserverEvent(event) {
|
|
319
|
+
switch (event.event) {
|
|
320
|
+
case _tspCommandTypes().EventName.semanticDiag:
|
|
321
|
+
case _tspCommandTypes().EventName.syntaxDiag:
|
|
322
|
+
this.publishDiagnostic(event);
|
|
323
|
+
break;
|
|
324
|
+
default:
|
|
325
|
+
this.logger.debug(`ignored TsServer event: ${event.event}`);
|
|
239
326
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
openIfNeeded(file) {
|
|
245
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
-
if (this.files.includes(file)) {
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
yield this.open(file);
|
|
250
|
-
this.files.push(file);
|
|
251
|
-
});
|
|
327
|
+
}
|
|
328
|
+
convertFileToAbsoluteIfNeeded(filepath) {
|
|
329
|
+
if (_path().default.isAbsolute(filepath)) {
|
|
330
|
+
return filepath;
|
|
252
331
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
projectRootPath: this.projectPath,
|
|
259
|
-
});
|
|
260
|
-
});
|
|
332
|
+
return _path().default.join(this.projectPath, filepath);
|
|
333
|
+
}
|
|
334
|
+
publishDiagnostic(message) {
|
|
335
|
+
if (!message.body?.diagnostics.length || !this.options.printTypeErrors) {
|
|
336
|
+
return;
|
|
261
337
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
338
|
+
this.lastDiagnostics.push(message.body);
|
|
339
|
+
const file = _path().default.relative(this.projectPath, message.body.file);
|
|
340
|
+
message.body.diagnostics.forEach(diag => this.logger.console((0, _formatDiagnostics().formatDiagnostic)(diag, file)));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* copied over from https://github.com/typescript-language-server/typescript-language-server/blob/master/src/lsp-server.ts
|
|
345
|
+
*/
|
|
346
|
+
findTsserverPath() {
|
|
347
|
+
if (this.options.tsServerPath) {
|
|
348
|
+
return this.options.tsServerPath;
|
|
270
349
|
}
|
|
350
|
+
const tsServerPath = _path().default.join('typescript', 'lib', 'tsserver.js');
|
|
351
|
+
|
|
271
352
|
/**
|
|
272
|
-
*
|
|
273
|
-
* as a workaround, to tell tsserver what was changed, we pretend that the entire file was cleared and new text was
|
|
274
|
-
* added. this is the only way I could find to tell tsserver about the change. otherwise, tsserver keep assuming that
|
|
275
|
-
* the file content remained the same. (closing/re-opening the file doesn't help).
|
|
353
|
+
* (1) find it in the bit directory
|
|
276
354
|
*/
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
// tell tsserver that all content was removed
|
|
281
|
-
yield ((_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.notify(tsp_command_types_1.CommandTypes.Change, {
|
|
282
|
-
file,
|
|
283
|
-
line: 1,
|
|
284
|
-
offset: 1,
|
|
285
|
-
endLine: 99999,
|
|
286
|
-
endOffset: 1,
|
|
287
|
-
insertString: '',
|
|
288
|
-
}));
|
|
289
|
-
const content = yield fs_extra_1.default.readFile(file, 'utf-8');
|
|
290
|
-
// tell tsserver that all file content was added
|
|
291
|
-
yield ((_b = this.tsServer) === null || _b === void 0 ? void 0 : _b.notify(tsp_command_types_1.CommandTypes.Change, {
|
|
292
|
-
file,
|
|
293
|
-
line: 1,
|
|
294
|
-
offset: 1,
|
|
295
|
-
endLine: 1,
|
|
296
|
-
endOffset: 1,
|
|
297
|
-
insertString: content,
|
|
298
|
-
}));
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
onTsserverEvent(event) {
|
|
302
|
-
switch (event.event) {
|
|
303
|
-
case tsp_command_types_1.EventName.semanticDiag:
|
|
304
|
-
case tsp_command_types_1.EventName.syntaxDiag:
|
|
305
|
-
this.publishDiagnostic(event);
|
|
306
|
-
break;
|
|
307
|
-
default:
|
|
308
|
-
this.logger.debug(`ignored TsServer event: ${event.event}`);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
convertFileToAbsoluteIfNeeded(filepath) {
|
|
312
|
-
if (path_1.default.isAbsolute(filepath)) {
|
|
313
|
-
return filepath;
|
|
314
|
-
}
|
|
315
|
-
return path_1.default.join(this.projectPath, filepath);
|
|
316
|
-
}
|
|
317
|
-
publishDiagnostic(message) {
|
|
318
|
-
var _a;
|
|
319
|
-
if (!((_a = message.body) === null || _a === void 0 ? void 0 : _a.diagnostics.length) || !this.options.printTypeErrors) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
this.lastDiagnostics.push(message.body);
|
|
323
|
-
const file = path_1.default.relative(this.projectPath, message.body.file);
|
|
324
|
-
message.body.diagnostics.forEach((diag) => this.logger.console((0, format_diagnostics_1.formatDiagnostic)(diag, file)));
|
|
355
|
+
const bundled = (0, _modulesResolver().findPathToModule)(__dirname, tsServerPath);
|
|
356
|
+
if (bundled) {
|
|
357
|
+
return bundled;
|
|
325
358
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
if (this.options.tsServerPath) {
|
|
331
|
-
return this.options.tsServerPath;
|
|
332
|
-
}
|
|
333
|
-
const tsServerPath = path_1.default.join('typescript', 'lib', 'tsserver.js');
|
|
334
|
-
/**
|
|
335
|
-
* (1) find it in the bit directory
|
|
336
|
-
*/
|
|
337
|
-
const bundled = (0, modules_resolver_1.findPathToModule)(__dirname, tsServerPath);
|
|
338
|
-
if (bundled) {
|
|
339
|
-
return bundled;
|
|
340
|
-
}
|
|
341
|
-
// (2) use globally installed tsserver
|
|
342
|
-
if (command_exists_1.default.sync((0, utils_1.getTsserverExecutable)())) {
|
|
343
|
-
return (0, utils_1.getTsserverExecutable)();
|
|
344
|
-
}
|
|
345
|
-
throw new Error(`Couldn't find '${(0, utils_1.getTsserverExecutable)()}' executable or 'tsserver.js' module`);
|
|
359
|
+
|
|
360
|
+
// (2) use globally installed tsserver
|
|
361
|
+
if (_commandExists().default.sync((0, _utils().getTsserverExecutable)())) {
|
|
362
|
+
return (0, _utils().getTsserverExecutable)();
|
|
346
363
|
}
|
|
364
|
+
throw new Error(`Couldn't find '${(0, _utils().getTsserverExecutable)()}' executable or 'tsserver.js' module`);
|
|
365
|
+
}
|
|
347
366
|
}
|
|
348
367
|
exports.TsserverClient = TsserverClient;
|
|
368
|
+
|
|
349
369
|
//# sourceMappingURL=ts-server-client.js.map
|