@herb-tools/language-server 0.9.0 → 0.9.1
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/diagnostics.js +0 -221
- package/dist/diagnostics.js.map +1 -1
- package/dist/herb-language-server.js +13186 -10335
- package/dist/herb-language-server.js.map +1 -1
- package/dist/index.cjs +65 -263
- package/dist/index.cjs.map +1 -1
- package/dist/linter_service.js +7 -2
- package/dist/linter_service.js.map +1 -1
- package/dist/server.js +3 -0
- package/dist/server.js.map +1 -1
- package/dist/types/diagnostics.d.ts +1 -34
- package/dist/types/utils.d.ts +4 -2
- package/dist/utils.js +12 -1
- package/dist/utils.js.map +1 -1
- package/package.json +7 -7
- package/src/diagnostics.ts +1 -291
- package/src/linter_service.ts +10 -2
- package/src/server.ts +3 -0
- package/src/utils.ts +15 -2
package/dist/diagnostics.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { DiagnosticSeverity, DiagnosticTag } from "vscode-languageserver/node";
|
|
2
|
-
import { Visitor } from "@herb-tools/core";
|
|
3
|
-
import { isHTMLTextNode, isERBIfNode, isERBElseNode } from "@herb-tools/core";
|
|
4
|
-
import { lspRangeFromLocation } from "./range_utils";
|
|
5
1
|
export class Diagnostics {
|
|
6
2
|
constructor(connection, documentService, parserService, linterService, configService) {
|
|
7
3
|
this.diagnostics = new Map();
|
|
@@ -19,21 +15,14 @@ export class Diagnostics {
|
|
|
19
15
|
else {
|
|
20
16
|
const parseResult = this.parserService.parseDocument(textDocument);
|
|
21
17
|
const lintResult = await this.linterService.lintDocument(textDocument);
|
|
22
|
-
const unreachableCodeDiagnostics = this.getUnreachableCodeDiagnostics(parseResult.document);
|
|
23
18
|
allDiagnostics = [
|
|
24
19
|
...parseResult.diagnostics,
|
|
25
20
|
...lintResult.diagnostics,
|
|
26
|
-
...unreachableCodeDiagnostics,
|
|
27
21
|
];
|
|
28
22
|
}
|
|
29
23
|
this.diagnostics.set(textDocument, allDiagnostics);
|
|
30
24
|
this.sendDiagnosticsFor(textDocument);
|
|
31
25
|
}
|
|
32
|
-
getUnreachableCodeDiagnostics(document) {
|
|
33
|
-
const collector = new UnreachableCodeCollector();
|
|
34
|
-
collector.visit(document);
|
|
35
|
-
return collector.diagnostics;
|
|
36
|
-
}
|
|
37
26
|
async refreshDocument(document) {
|
|
38
27
|
await this.validate(document);
|
|
39
28
|
}
|
|
@@ -50,214 +39,4 @@ export class Diagnostics {
|
|
|
50
39
|
this.diagnostics.delete(textDocument);
|
|
51
40
|
}
|
|
52
41
|
}
|
|
53
|
-
export class UnreachableCodeCollector extends Visitor {
|
|
54
|
-
constructor() {
|
|
55
|
-
super(...arguments);
|
|
56
|
-
this.diagnostics = [];
|
|
57
|
-
this.processedIfNodes = new Set();
|
|
58
|
-
this.processedElseNodes = new Set();
|
|
59
|
-
}
|
|
60
|
-
visitERBCaseNode(node) {
|
|
61
|
-
this.checkUnreachableChildren(node.children);
|
|
62
|
-
this.checkAndMarkElseClause(node.else_clause);
|
|
63
|
-
this.visitChildNodes(node);
|
|
64
|
-
}
|
|
65
|
-
visitERBCaseMatchNode(node) {
|
|
66
|
-
this.checkUnreachableChildren(node.children);
|
|
67
|
-
this.checkAndMarkElseClause(node.else_clause);
|
|
68
|
-
this.visitChildNodes(node);
|
|
69
|
-
}
|
|
70
|
-
visitERBIfNode(node) {
|
|
71
|
-
if (this.processedIfNodes.has(node)) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
this.markIfChainAsProcessed(node);
|
|
75
|
-
this.markElseNodesInIfChain(node);
|
|
76
|
-
const entireChainEmpty = this.isEntireIfChainEmpty(node);
|
|
77
|
-
if (entireChainEmpty) {
|
|
78
|
-
this.checkEmptyStatements(node, node.statements, "if");
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
this.checkIfChainParts(node);
|
|
82
|
-
}
|
|
83
|
-
this.visitChildNodes(node);
|
|
84
|
-
}
|
|
85
|
-
visitERBElseNode(node) {
|
|
86
|
-
if (this.processedElseNodes.has(node)) {
|
|
87
|
-
this.visitChildNodes(node);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
this.checkEmptyStatements(node, node.statements, "else");
|
|
91
|
-
this.visitChildNodes(node);
|
|
92
|
-
}
|
|
93
|
-
visitERBUnlessNode(node) {
|
|
94
|
-
const unlessHasContent = this.statementsHaveContent(node.statements);
|
|
95
|
-
const elseHasContent = node.else_clause && this.statementsHaveContent(node.else_clause.statements);
|
|
96
|
-
if (node.else_clause) {
|
|
97
|
-
this.processedElseNodes.add(node.else_clause);
|
|
98
|
-
}
|
|
99
|
-
const entireBlockEmpty = !unlessHasContent && !elseHasContent;
|
|
100
|
-
if (entireBlockEmpty) {
|
|
101
|
-
this.checkEmptyStatements(node, node.statements, "unless");
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
if (!unlessHasContent) {
|
|
105
|
-
this.checkEmptyStatementsWithEndLocation(node, node.statements, "unless", node.else_clause);
|
|
106
|
-
}
|
|
107
|
-
if (node.else_clause && !elseHasContent) {
|
|
108
|
-
this.checkEmptyStatements(node.else_clause, node.else_clause.statements, "else");
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
this.visitChildNodes(node);
|
|
112
|
-
}
|
|
113
|
-
visitERBForNode(node) {
|
|
114
|
-
this.checkEmptyStatements(node, node.statements, "for");
|
|
115
|
-
this.visitChildNodes(node);
|
|
116
|
-
}
|
|
117
|
-
visitERBWhileNode(node) {
|
|
118
|
-
this.checkEmptyStatements(node, node.statements, "while");
|
|
119
|
-
this.visitChildNodes(node);
|
|
120
|
-
}
|
|
121
|
-
visitERBUntilNode(node) {
|
|
122
|
-
this.checkEmptyStatements(node, node.statements, "until");
|
|
123
|
-
this.visitChildNodes(node);
|
|
124
|
-
}
|
|
125
|
-
visitERBWhenNode(node) {
|
|
126
|
-
if (!node.then_keyword) {
|
|
127
|
-
this.checkEmptyStatements(node, node.statements, "when");
|
|
128
|
-
}
|
|
129
|
-
this.visitChildNodes(node);
|
|
130
|
-
}
|
|
131
|
-
visitERBBeginNode(node) {
|
|
132
|
-
this.checkEmptyStatements(node, node.statements, "begin");
|
|
133
|
-
this.visitChildNodes(node);
|
|
134
|
-
}
|
|
135
|
-
visitERBRescueNode(node) {
|
|
136
|
-
this.checkEmptyStatements(node, node.statements, "rescue");
|
|
137
|
-
this.visitChildNodes(node);
|
|
138
|
-
}
|
|
139
|
-
visitERBEnsureNode(node) {
|
|
140
|
-
this.checkEmptyStatements(node, node.statements, "ensure");
|
|
141
|
-
this.visitChildNodes(node);
|
|
142
|
-
}
|
|
143
|
-
visitERBBlockNode(node) {
|
|
144
|
-
this.checkEmptyStatements(node, node.body, "block");
|
|
145
|
-
this.visitChildNodes(node);
|
|
146
|
-
}
|
|
147
|
-
visitERBInNode(node) {
|
|
148
|
-
if (!node.then_keyword) {
|
|
149
|
-
this.checkEmptyStatements(node, node.statements, "in");
|
|
150
|
-
}
|
|
151
|
-
this.visitChildNodes(node);
|
|
152
|
-
}
|
|
153
|
-
checkUnreachableChildren(children) {
|
|
154
|
-
for (const child of children) {
|
|
155
|
-
if (isHTMLTextNode(child) && child.content.trim() === "") {
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
this.addDiagnostic(child.location, "Unreachable code: content between case and when/in is never executed");
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
checkEmptyStatements(node, statements, blockType) {
|
|
162
|
-
this.checkEmptyStatementsWithEndLocation(node, statements, blockType, null);
|
|
163
|
-
}
|
|
164
|
-
checkEmptyStatementsWithEndLocation(node, statements, blockType, subsequentNode) {
|
|
165
|
-
if (this.statementsHaveContent(statements)) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
const startLocation = node.location.start;
|
|
169
|
-
const endLocation = subsequentNode
|
|
170
|
-
? subsequentNode.location.start
|
|
171
|
-
: node.location.end;
|
|
172
|
-
this.addDiagnostic({ start: startLocation, end: endLocation }, `Empty ${blockType} block: this control flow statement has no content`);
|
|
173
|
-
}
|
|
174
|
-
addDiagnostic(location, message) {
|
|
175
|
-
const diagnostic = {
|
|
176
|
-
range: lspRangeFromLocation(location),
|
|
177
|
-
message,
|
|
178
|
-
severity: DiagnosticSeverity.Hint,
|
|
179
|
-
tags: [DiagnosticTag.Unnecessary],
|
|
180
|
-
source: "Herb Language Server"
|
|
181
|
-
};
|
|
182
|
-
this.diagnostics.push(diagnostic);
|
|
183
|
-
}
|
|
184
|
-
statementsHaveContent(statements) {
|
|
185
|
-
return statements.some(statement => {
|
|
186
|
-
if (isHTMLTextNode(statement)) {
|
|
187
|
-
return statement.content.trim() !== "";
|
|
188
|
-
}
|
|
189
|
-
return true;
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
checkAndMarkElseClause(elseClause) {
|
|
193
|
-
if (!elseClause) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
this.processedElseNodes.add(elseClause);
|
|
197
|
-
if (!this.statementsHaveContent(elseClause.statements)) {
|
|
198
|
-
this.checkEmptyStatements(elseClause, elseClause.statements, "else");
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
markIfChainAsProcessed(node) {
|
|
202
|
-
this.processedIfNodes.add(node);
|
|
203
|
-
this.traverseSubsequentNodes(node.subsequent, (current) => {
|
|
204
|
-
if (isERBIfNode(current)) {
|
|
205
|
-
this.processedIfNodes.add(current);
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
markElseNodesInIfChain(node) {
|
|
210
|
-
this.traverseSubsequentNodes(node.subsequent, (current) => {
|
|
211
|
-
if (isERBElseNode(current)) {
|
|
212
|
-
this.processedElseNodes.add(current);
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
traverseSubsequentNodes(startNode, callback) {
|
|
217
|
-
let current = startNode;
|
|
218
|
-
while (current) {
|
|
219
|
-
if (isERBIfNode(current)) {
|
|
220
|
-
callback(current);
|
|
221
|
-
current = current.subsequent;
|
|
222
|
-
}
|
|
223
|
-
else if (isERBElseNode(current)) {
|
|
224
|
-
callback(current);
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
break;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
checkIfChainParts(node) {
|
|
233
|
-
if (!this.statementsHaveContent(node.statements)) {
|
|
234
|
-
this.checkEmptyStatementsWithEndLocation(node, node.statements, "if", node.subsequent);
|
|
235
|
-
}
|
|
236
|
-
this.traverseSubsequentNodes(node.subsequent, (current) => {
|
|
237
|
-
if (this.statementsHaveContent(current.statements)) {
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
const blockType = isERBIfNode(current) ? 'elsif' : 'else';
|
|
241
|
-
const nextSubsequent = isERBIfNode(current) ? current.subsequent : null;
|
|
242
|
-
if (nextSubsequent) {
|
|
243
|
-
this.checkEmptyStatementsWithEndLocation(current, current.statements, blockType, nextSubsequent);
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
this.checkEmptyStatements(current, current.statements, blockType);
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
isEntireIfChainEmpty(node) {
|
|
251
|
-
if (this.statementsHaveContent(node.statements)) {
|
|
252
|
-
return false;
|
|
253
|
-
}
|
|
254
|
-
let hasContent = false;
|
|
255
|
-
this.traverseSubsequentNodes(node.subsequent, (current) => {
|
|
256
|
-
if (this.statementsHaveContent(current.statements)) {
|
|
257
|
-
hasContent = true;
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
return !hasContent;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
42
|
//# sourceMappingURL=diagnostics.js.map
|
package/dist/diagnostics.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"diagn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