-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstepsParser.ts
278 lines (255 loc) · 8.17 KB
/
stepsParser.ts
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
/**
* This file is based on the code done by @alexkrechik: https://github.com/alexkrechik/VSCucumberAutoComplete
*/
import * as fs from 'fs';
import * as glob from 'glob';
import * as path from 'path';
const url = require('remote-origin-url');
const git = require('parse-git-config');
class LineLocation {
public line: Number;
public character: Number;
constructor(line: Number, pos: Number) {
this.line = line;
this.character = pos;
}
}
class Location {
public line: LineLocation;
public displayPath: string;
public gitURL: string;
private _filePath: string;
set file(file: string) {
this._filePath = path.resolve(path.dirname(file), path.basename(file));
// Get git URL and relative path
var gitMasterUrl = url.sync().replace(/\/$/, '');
// git.resolve will return the configuration file --> '.git/config'
var prefix = path.resolve(git.resolve(this._filePath), '..', '..');
this.displayPath = this._filePath.replace(prefix, '').replace(/\\/g, '/').replace(/^\//, '');
var gitExtraPath = '/';
var suffix = '';
if (gitMasterUrl.match(/http[s]?:\/\/[^\/]*bitbucket\.(?:org|com)/)) {
gitExtraPath = '/src/master/';
suffix = "#" + path.basename(file) + "-" + this.line.line;
}
else if (gitMasterUrl.indexOf(/http[s]?:\/\/[^\/]*github\.com/) !== -1) {
// here we should parse the correct branch
gitExtraPath = '/blob/master/';
}
else {
console.log("Unknown git provider: " + gitMasterUrl);
}
this.gitURL = gitMasterUrl + gitExtraPath + this.displayPath + suffix;
}
get file() {
return this._filePath;
}
constructor(file: string, pos: LineLocation) {
this.line = pos;
this.file = file;
}
}
let workspaceRoot: string;
//Array will be populated with all the steps found
let steps: Array<Step> = [];
// Object will be populated with all the pages found
let pages = {};
//Gerkin Reg ex
let gerkinRegEx = /^\s*(Given|When|Then|And|But) /;
// Object, which contains current configuration
let settings = {
cucumberautocomplete: {
steps: [],
regExpStart: '',
regExpEnd: '',
pages: {},
},
};
export class CodeParser {
/**
* @param {Array<String>} stepsDir The directories from which to draw the steps
*/
constructor(stepsDir: Array<String>) {
settings.cucumberautocomplete.steps = settings.cucumberautocomplete.steps.concat(stepsDir);
populateStepsAndPageObjects();
}
/**
* Matches a Gherkin expression against all the recorded steps
* @param {string} line The Gherkin expression to parse
* @returns {StepLine} The line that matches the gherkin expression, if found
*/
public ParseLine(line: string): StepLine {
return handleLine(line);
}
}
//get unique id for the elements ids
let id = {
x: 0,
get() {
return this.x++;
},
};
interface Step {
id: string;
reg: RegExp;
text: string;
desc: string;
def: Location;
}
interface StepLine {
//Line without 'Given|When|Then|And' part
stepPart: string;
//Step, matched to the stepPart, or null if absent
stepMatch: Step;
//Start position of line
start: number;
//End position of line
end: number;
}
interface PageObject {
id: string;
text: string;
desc: string;
def: Location;
}
interface Page {
id: string;
text: string;
desc: string;
def: Location;
objects: PageObject[];
}
//Return start, end position and matched (if any) Gherkin step
function handleLine(line: String): StepLine {
let typeRegEx = /Given |When |Then |And |But /;
let typeMatch = line.match(typeRegEx);
let typePart = typeMatch[0];
let stepPart = line.replace(gerkinRegEx, '');
let stepMatch;
for (let i = 0; i < steps.length; i++) {
if (line.trim().match(steps[i].reg) || stepPart.search(steps[i].reg) !== -1) {
stepMatch = steps[i];
break;
}
}
let start = typeMatch.index;
let end = typeMatch.index + typePart.length + stepPart.length;
return {
stepPart: stepPart,
stepMatch: stepMatch,
start: start,
end: end,
};
}
//Get all the steps from provided file
function getFileSteps(filePath: string): Step[] {
let steps = [];
let regExpStart, regExpEnd;
if (settings) {
regExpStart = settings.cucumberautocomplete.regExpStart || '\/';
regExpEnd = settings.cucumberautocomplete.regExpEnd || '\/';
} else {
regExpStart = '\/';
regExpEnd = '\/';
}
fs.readFileSync(filePath, 'utf8').split(/\r?\n/g).forEach((line, lineIndex) => {
// We need to figure out how to ommit the comments, otherwise this will also parse commented lines
if (line.search(new RegExp('(Given|When|Then|And|But).*' + regExpStart + '.+' + regExpEnd, 'i')) !== -1) {
//Get the '//' match
let match = line.match(new RegExp(regExpStart + '(.+)' + regExpEnd));
//Get matched text, remove start and finish slashes
let matchText = match[1];
let pos = new LineLocation(lineIndex, match.index);
steps.push({
id: 'step' + id.get(),
reg: new RegExp(matchText),
//We should remove text between quotes, '^|$' regexp marks and backslashes
text: matchText.replace(/^\^|\$$/g, '').replace(/"\([^\)]*\)"/g, '""').replace(/\\/g, ''),
desc: line.replace(/\{.*/, '').replace(/^\s*/, '').replace(/\s*$/, ''),
def: new Location(filePath, pos),
});
}
});
return steps;
}
function getPageObjects(text: string, path: string): PageObject[] {
let res = [];
text.split(/\r?\n/g).forEach((line, i) => {
let poMatch = line.match(/[\s\.]([a-zA-z][^\s^\.]*)\s*[:=]/);
if (poMatch) {
let pos = new LineLocation(i, 0);
if (!res.filter(v => { return v.text === poMatch[1]; }).length) {
res.push({
id: 'pageObect' + id.get(),
text: poMatch[1],
desc: line,
def: new Location(path, pos),
});
}
}
});
return res;
}
//Get Page object
function getPage(name: string, path: string): Page {
let text = fs.readFileSync(path, 'utf8');
let zeroPos = new LineLocation(0, 0);
return {
id: 'page' + id.get(),
text: name,
desc: text.split(/\r?\n/g).slice(0, 10).join('\r\n'),
def: new Location(path, zeroPos),
objects: getPageObjects(text, path),
};
}
//Current position of our cursor
enum PositionType {
Step,
Page,
PageObject
}
interface PositionObject {
type: PositionType;
page?: string;
pageObject?: string;
}
function getPositionObject(line: string, position: number): PositionObject {
let slicedLine = line.slice(0, position);
let match = slicedLine.match(/"/g);
if (match && match.length % 2) {
//Double quote was opened but was not closed
let pageMatch = slicedLine.match(/"([^"]*)"\."([^"]*)$/);
let endLine = line.slice(position).replace(/".*/, '');
if (pageMatch) {
return {
type: PositionType.PageObject,
page: pageMatch[1],
pageObject: pageMatch[2] + endLine,
};
} else {
return {
type: PositionType.Page,
page: slicedLine.match(/([^"]*)$/)[1] + endLine,
};
}
} else {
return { type: PositionType.Step };
}
}
function populateStepsAndPageObjects() {
//Populate steps array
let stepsPathes = [].concat(settings.cucumberautocomplete.steps);
steps = [];
stepsPathes.forEach((path) => {
glob.sync(path, { ignore: '.gitignore' }).forEach(f => {
steps = steps.concat(getFileSteps(f));
});
});
//Populate pages array
let pagesObj = settings.cucumberautocomplete.pages;
pages = {};
Object.keys(pagesObj).forEach((key) => {
let path = pagesObj[key];
pages[key] = getPage(key, path);
});
}