|
1 | | -const { DiagnosticSeverity: Severity } = require('vscode-languageserver/node'); |
2 | | -const { replaceCommentsTags, replaceDocString, replaceStory, getLineKeyword } = require('../utils'); |
3 | | -const Keywords = require('../keywords'); |
4 | | -const { addToDiagnostics } = require('./helper'); |
5 | | -const Messages = require('./messages'); |
6 | | -const { globalMatch, firstMatch } = require('../regex'); |
| 1 | +const rules = require('./rules'); |
7 | 2 |
|
8 | | -module.exports.validateDocument = function (document, docConfig) { |
| 3 | +module.exports.lint = function (document, docConfig) { |
9 | 4 | const diagnostics = []; |
10 | | - validateFeatureOccurance(document, diagnostics); |
11 | | - validateStartingStep(document, diagnostics); |
12 | | - |
13 | | - const simpleText = getSimpleText(document); |
14 | | - validateByLine(document, simpleText, diagnostics); |
15 | | - return diagnostics; |
16 | | -}; |
17 | | - |
18 | | -function validateFeatureOccurance(document, diagnostics) { |
19 | 5 | const text = document.getText(); |
20 | | - const regex = globalMatch.feature; |
21 | | - let matchCount = 0; |
22 | | - while ((match = regex.exec(text))) { |
23 | | - matchCount++; |
24 | | - // only show error from second match onward |
25 | | - if (matchCount > 1) { |
26 | | - addToDiagnostics( |
27 | | - document, |
28 | | - diagnostics, |
29 | | - match.index, |
30 | | - match.index + match[0].length, |
31 | | - Messages.mustHaveOnlyOneFeature, |
32 | | - Severity.Error |
33 | | - ); |
34 | | - } |
35 | | - } |
36 | | - if (!matchCount) { |
37 | | - addToDiagnostics(document, diagnostics, 1, 1, Messages.mustHaveFeatureName, Severity.Error); |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -function validateStartingStep(document, diagnostics) { |
42 | | - const text = replaceCommentsTags(document.getText()); |
43 | | - const regex = globalMatch.beginningStep; |
44 | | - |
45 | | - while ((match = regex.exec(text))) { |
46 | | - matchStep = match[0].trim(); |
47 | | - if (![Keywords.Given, Keywords.When].includes(matchStep)) { |
48 | | - addToDiagnostics( |
49 | | - document, |
50 | | - diagnostics, |
51 | | - match.index, |
52 | | - match.index + match[0].length, |
53 | | - Messages.firstStepShouldBeGivenOrWhen |
54 | | - ); |
55 | | - } |
56 | | - } |
57 | | -} |
58 | | - |
59 | | -function validateByLine(document, text, diagnostics) { |
60 | | - const lines = text.split('\n'); |
61 | | - const regex = firstMatch.step; |
62 | | - |
63 | | - let prevStep = ''; |
64 | | - let index = 0; |
65 | | - let keywordHit = false; |
66 | | - lines.forEach((line) => { |
67 | | - let lineLength = line.length; |
68 | | - if (lineLength === 0) { |
69 | | - lineLength = 1; |
70 | | - } else { |
71 | | - // also count line break |
72 | | - lineLength += 1; |
73 | | - } |
74 | | - |
75 | | - // do not process empty line |
76 | | - if (!line.trim().length) { |
77 | | - index += lineLength; |
78 | | - return; |
79 | | - } |
80 | | - |
81 | | - const keyword = getLineKeyword(line); |
82 | | - if (!keyword) { |
83 | | - addToDiagnostics(document, diagnostics, index, index + line.length, Messages.invalidLine, Severity.Error); |
84 | | - index += lineLength; |
85 | | - return; |
86 | | - } else if (!keywordHit && keyword !== Keywords.Feature) { |
87 | | - addToDiagnostics( |
88 | | - document, |
89 | | - diagnostics, |
90 | | - index, |
91 | | - index + line.length, |
92 | | - Messages.mustStartWithFeatureName, |
93 | | - Severity.Error |
94 | | - ); |
95 | | - } |
96 | | - keywordHit = true; |
97 | | - |
98 | | - const match = regex.exec(line); |
99 | | - if (Boolean(match)) { |
100 | | - const matchStep = match[0]; |
101 | | - if (prevStep === matchStep) { |
102 | | - const rangeEnd = index + match.index + matchStep.length; |
103 | | - let message = Messages.repeatedStep; |
104 | | - if (matchStep === Keywords.Then) { |
105 | | - message = Messages.repeatedStepForThen; |
106 | | - } |
107 | | - addToDiagnostics(document, diagnostics, index + match.index, rangeEnd, message); |
108 | | - } |
109 | | - if (matchStep !== Keywords.And) { |
110 | | - prevStep = matchStep; |
111 | | - } |
112 | | - } else { |
113 | | - prevStep = ''; |
114 | | - } |
115 | | - index += lineLength; |
| 6 | + Object.keys(rules).forEach(function (ruleId) { |
| 7 | + rules[ruleId].run(document, text, diagnostics); |
116 | 8 | }); |
117 | | -} |
118 | | - |
119 | | -// returns text where tags, comments, user story and docstring are replaced with spaces |
120 | | -function getSimpleText(document) { |
121 | | - let text = replaceCommentsTags(document.getText()); |
122 | | - text = replaceDocString(text); |
123 | | - text = replaceStory(text); |
124 | | - return text; |
125 | | -} |
| 9 | + return diagnostics; |
| 10 | +}; |
0 commit comments