Skip to content

Commit aefafb4

Browse files
committed
move RegEx selector to their own file
1 parent 930c574 commit aefafb4

File tree

2 files changed

+70
-52
lines changed

2 files changed

+70
-52
lines changed

src/parser.ts

+31-52
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,8 @@ const path = require("path");
44
import { File, Input, Output } from "./shared/IFile";
55
import logger from "./shared/logger";
66
import pathResolver from "./utils/path-resolver";
7+
import { REGEX_SELECTORS } from "./utils/regexSelectors";
78

8-
// selectors
9-
const componentSelector = /export(\s+)class(\s+)[a-zA-Z0-9-_]+/g;
10-
const componentHTMLselector = /selector:(\s+)(\"|')[a-zA-Z-_]+(\"|')/g;
11-
12-
// inputs
13-
// @Input() variableName; and @Input() variableName
14-
const regularInputSelector = /@Input\(\)(\s+)[a-zA-Z0-9-_]+(;|)/g;
15-
// @Input() variableName: type; and @Input() variableName: number = 9;
16-
const regularInputWithTypeSelector = /@Input\(\)(\s+)[a-zA-Z0-9-_]+:(\s+)[a-zA-Z0-9-_]+((;|)|(\s+)[a-zA-Z0-9-_]+(\s+)=(\s+)[a-zA-Z0-9-_]+(;|))/g;
17-
// @Input('inputName') varName: type; and @Input("inputName") varName: type
18-
const customNameInputWithTypeSelector = /@Input\(('|")[a-zA-Z0-9-_]+('|")\)(\s+)[a-zA-Z0-9-_]+:(\s+)[a-zA-Z0-9-_]+\;/g;
19-
const regularInputLiteralTypeSelector = /@Input\(\)(\s+)[a-zA-Z0-9-_]+:((\s+)(('|")[a-zA-Z0-9-_]+('|")((\s+)\|)))+(\s+)('|")[a-zA-Z0-9-_]+('|")(;|:|)/g;
20-
//@Input() set foo(value) {}
21-
const setterInputSelector = /@Input\(\)(\s+)set(\s+)[a-zA-Z0-9-_]+\([a-zA-Z0-9-_]+\)(\s+)/g;
22-
//@Input() set foo(value: type) {}
23-
const setterInputWithTypeSelector = /@Input\(\)(\s+)set(\s+)[a-zA-Z0-9-_]+\([a-zA-Z0-9-_]+:(\s+)[a-zA-Z0-9-_]+\)(\s+)/g;
24-
// @Input('inputNameC') varName = 'adv';
25-
const setterInputCustomNameSelector = /@Input\(("|')[a-zA-Z0-9-_]+("|')\)(\s+)[a-zA-Z0-9-_]+(\s+)=(\s+)[A-Za-z0-9"']+(;|)/g;
26-
//@Input() set foo(value: 'type1' | 'type2') {}
27-
const setterInputLiteralTypeSelector = /@Input\(\)(\s+)set(\s+)[a-zA-Z0-9-_]+\([a-zA-Z0-9-_]+:((\s+)('|")[a-zA-Z0-9-_]+('|")(\s+)\|)+(\s)('|")[a-zA-Z0-9-_]+('|")\)/g;
28-
29-
// outputs
30-
// @Output() buttonClick: EventEmitter<any> = new EventEmitter()
31-
const regularOutputSelector = /@Output\(\)(\s+)[a-zA-Z0-9-_]+:(\s+)EventEmitter<[a-zA-Z0-9-_]+>(\s+)=(\s+)new(\s+)EventEmitter\(\);/g;
32-
33-
// other
34-
const extendedClassSelector = /export(\s+)class(\s+)[a-zA-Z0-9-_]+(\s+)extends(\s+)[a-zA-Z0-9-_]+/g;
35-
const extendedClassPathSelector = /import(\s+){(\s+)[a-zA-Z0-9-_]+(\s+)}(\s+)from(\s+)[\/\@A-Za-z0-9."'_-]+/g;
369
// TODO: class implementation with inputs/outputs defined
3710

3811
// TODO: Test in other OS (github actions)
@@ -62,7 +35,8 @@ export const parser = (filePaths: Array<string>): Array<File> => {
6235
continue;
6336
}
6437

65-
let fileNameData: Array<string> = file?.match(componentSelector) || [];
38+
let fileNameData: Array<string> =
39+
file?.match(REGEX_SELECTORS.componentSelector) || [];
6640
if (fileNameData.length === 0) {
6741
logger.warn("Component tag not defined by any class.");
6842
continue;
@@ -77,7 +51,7 @@ export const parser = (filePaths: Array<string>): Array<File> => {
7751
if (containsComponentDef) {
7852
// match returns a string not an array
7953
let componentSelectorData: Array<string> =
80-
file?.match(componentHTMLselector) || [];
54+
file?.match(REGEX_SELECTORS.componentHTMLselector) || [];
8155
if (componentSelectorData.length === 0) {
8256
logger.warn(
8357
"Component doesn't define any selector but contains @Component anotation."
@@ -93,7 +67,7 @@ export const parser = (filePaths: Array<string>): Array<File> => {
9367
// Input() foo: 'type1' | 'type2'
9468
let inputs: Array<Input> = [];
9569
let inputsData: Array<string> =
96-
file?.match(regularInputLiteralTypeSelector) || [];
70+
file?.match(REGEX_SELECTORS.regularInputLiteralTypeSelector) || [];
9771
for (let input of inputsData) {
9872
logger.log("inputs parsed:", inputsData);
9973
let tmp: Array<string> = input.replace(/(\s)+/g, " ").split(" ");
@@ -108,23 +82,25 @@ export const parser = (filePaths: Array<string>): Array<File> => {
10882
type,
10983
});
11084
}
111-
file = file.replace(regularInputLiteralTypeSelector, "");
85+
file = file.replace(REGEX_SELECTORS.regularInputLiteralTypeSelector, "");
11286

11387
// @Input() variableName: type; and @Input() variableName: number = 9;
11488
inputsData = [];
115-
inputsData = file?.match(regularInputWithTypeSelector) || [];
89+
inputsData =
90+
file?.match(REGEX_SELECTORS.regularInputWithTypeSelector) || [];
11691
for (let input of inputsData) {
11792
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
11893
inputs.push({
11994
inputName: tmp[1].replace(":", ""),
12095
type: tmp[2].replace(";", ""),
12196
});
12297
}
123-
file = file.replace(regularInputWithTypeSelector, "");
98+
file = file.replace(REGEX_SELECTORS.regularInputWithTypeSelector, "");
12499

125100
inputsData = [];
126101
// @Input('inputName') varName: type; and @Input("inputName") varName: type
127-
inputsData = file?.match(customNameInputWithTypeSelector) || [];
102+
inputsData =
103+
file?.match(REGEX_SELECTORS.customNameInputWithTypeSelector) || [];
128104
for (let input of inputsData) {
129105
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
130106
const inputName = (tmp[0].match(/('|")[a-zA-Z0-9-_]+('|")/g) ||
@@ -134,12 +110,13 @@ export const parser = (filePaths: Array<string>): Array<File> => {
134110
type: tmp[2].replace(";", ""),
135111
});
136112
}
137-
file = file.replace(customNameInputWithTypeSelector, "");
113+
file = file.replace(REGEX_SELECTORS.customNameInputWithTypeSelector, "");
138114

139115
// @Input('inputNameC') varName = 'adv';
140116
// @Input("inputNameD") varName = 2354;
141117
inputsData = [];
142-
inputsData = file?.match(setterInputCustomNameSelector) || [];
118+
inputsData =
119+
file?.match(REGEX_SELECTORS.setterInputCustomNameSelector) || [];
143120
for (let input of inputsData) {
144121
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
145122
const inputName = (tmp[0].match(/('|")[a-zA-Z0-9-_]+('|")/g) || [
@@ -150,11 +127,11 @@ export const parser = (filePaths: Array<string>): Array<File> => {
150127
type: undefined,
151128
});
152129
}
153-
file = file.replace(setterInputCustomNameSelector, "");
130+
file = file.replace(REGEX_SELECTORS.setterInputCustomNameSelector, "");
154131

155132
//@Input() set foo(value) {}
156133
inputsData = [];
157-
inputsData = file?.match(setterInputSelector) || [];
134+
inputsData = file?.match(REGEX_SELECTORS.setterInputSelector) || [];
158135
for (let input of inputsData) {
159136
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
160137
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
@@ -163,11 +140,11 @@ export const parser = (filePaths: Array<string>): Array<File> => {
163140
type: undefined,
164141
});
165142
}
166-
file = file.replace(setterInputSelector, "");
143+
file = file.replace(REGEX_SELECTORS.setterInputSelector, "");
167144

168145
//@Input() set foo(value: type) {}
169146
inputsData = [];
170-
inputsData = file?.match(setterInputWithTypeSelector) || [];
147+
inputsData = file?.match(REGEX_SELECTORS.setterInputWithTypeSelector) || [];
171148
for (let input of inputsData) {
172149
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
173150
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
@@ -177,11 +154,12 @@ export const parser = (filePaths: Array<string>): Array<File> => {
177154
type,
178155
});
179156
}
180-
file = file.replace(setterInputWithTypeSelector, "");
157+
file = file.replace(REGEX_SELECTORS.setterInputWithTypeSelector, "");
181158

182159
//@Input() set foo(value: 'type1' | 'type2') {}
183160
inputsData = [];
184-
inputsData = file?.match(setterInputLiteralTypeSelector) || [];
161+
inputsData =
162+
file?.match(REGEX_SELECTORS.setterInputLiteralTypeSelector) || [];
185163
for (let input of inputsData) {
186164
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
187165
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
@@ -195,12 +173,12 @@ export const parser = (filePaths: Array<string>): Array<File> => {
195173
type,
196174
});
197175
}
198-
file = file.replace(setterInputLiteralTypeSelector, "");
176+
file = file.replace(REGEX_SELECTORS.setterInputLiteralTypeSelector, "");
199177

200178
// @Input() variableName; and @Input() variableName. Also for now we will parse
201179
// in this part of the code @Input() variableName = value and @Input() variableName = value;
202180
inputsData = [];
203-
inputsData = file?.match(regularInputSelector) || [];
181+
inputsData = file?.match(REGEX_SELECTORS.regularInputSelector) || [];
204182
for (let input of inputsData) {
205183
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
206184
const inputName = tmp[1].replace(";", "");
@@ -212,12 +190,13 @@ export const parser = (filePaths: Array<string>): Array<File> => {
212190
type: undefined,
213191
});
214192
}
215-
file = file.replace(regularInputSelector, "");
193+
file = file.replace(REGEX_SELECTORS.regularInputSelector, "");
216194
logger.log("Inputs detected:", inputs);
217195

218196
let outputs: Array<Output> = [];
219197
// only @Output() buttonClick: EventEmitter<any> = new EventEmitter(); for now
220-
let outputsData: Array<string> = file?.match(regularOutputSelector) || [];
198+
let outputsData: Array<string> =
199+
file?.match(REGEX_SELECTORS.regularOutputSelector) || [];
221200
for (let output of outputsData) {
222201
let tmp: Array<string> = output.replace(/(\s+)/g, " ").split(" ");
223202
outputs.push({
@@ -228,27 +207,27 @@ export const parser = (filePaths: Array<string>): Array<File> => {
228207
.replace("<", ""),
229208
});
230209
}
231-
file = file.replace(regularOutputSelector, "");
210+
file = file.replace(REGEX_SELECTORS.regularOutputSelector, "");
232211
logger.log("Outputs detected:", outputs);
233212

234213
let extendedClassPath;
235-
if (file?.match(extendedClassSelector)) {
214+
if (file?.match(REGEX_SELECTORS.extendedClassSelector)) {
236215
// we should see if the extended class is in tmp and if not extract the inputs defined inside
237216
let matchExtendedClass: Array<string> =
238-
file?.match(extendedClassSelector) || [];
217+
file?.match(REGEX_SELECTORS.extendedClassSelector) || [];
239218
// resolve the path of the class
240219
let extendedClass: string = matchExtendedClass[0]
241220
.replace(/(\s+)/g, " ")
242221
.split(" ")[4];
243222
logger.log("extendedClassName:", extendedClass);
244223
let matchExtendedClassPath: Array<string> =
245-
file?.match(extendedClassPathSelector) || [];
224+
file?.match(REGEX_SELECTORS.extendedClassPathSelector) || [];
246225

247226
extendedClassPath = pathResolver.resolve(
248227
filePath,
249228
matchExtendedClassPath[0]
250229
);
251-
230+
252231
logger.log("path:", extendedClassPath);
253232
}
254233

src/utils/regexSelectors.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// selectors
2+
export const REGEX_SELECTORS = {
3+
componentSelector: /export(\s+)class(\s+)[a-zA-Z0-9-_]+/g,
4+
componentHTMLselector: /selector:(\s+)(\"|')[a-zA-Z-_]+(\"|')/g,
5+
// inputs
6+
// @Input() variableName; and @Input() variableName
7+
regularInputSelector: /@Input\(\)(\s+)[a-zA-Z0-9-_]+(;|)/g,
8+
// @Input() variableName: type; and @Input() variableName: number = 9;
9+
regularInputWithTypeSelector:
10+
/@Input\(\)(\s+)[a-zA-Z0-9-_]+:(\s+)[a-zA-Z0-9-_]+((;|)|(\s+)[a-zA-Z0-9-_]+(\s+)=(\s+)[a-zA-Z0-9-_]+(;|))/g,
11+
// @Input('inputName') varName: type; and @Input("inputName") varName: type
12+
customNameInputWithTypeSelector:
13+
/@Input\(('|")[a-zA-Z0-9-_]+('|")\)(\s+)[a-zA-Z0-9-_]+:(\s+)[a-zA-Z0-9-_]+\;/g,
14+
regularInputLiteralTypeSelector:
15+
/@Input\(\)(\s+)[a-zA-Z0-9-_]+:((\s+)(('|")[a-zA-Z0-9-_]+('|")((\s+)\|)))+(\s+)('|")[a-zA-Z0-9-_]+('|")(;|:|)/g,
16+
//@Input() set foo(value) {}
17+
setterInputSelector:
18+
/@Input\(\)(\s+)set(\s+)[a-zA-Z0-9-_]+\([a-zA-Z0-9-_]+\)(\s+)/g,
19+
//@Input() set foo(value: type) {}
20+
setterInputWithTypeSelector:
21+
/@Input\(\)(\s+)set(\s+)[a-zA-Z0-9-_]+\([a-zA-Z0-9-_]+:(\s+)[a-zA-Z0-9-_]+\)(\s+)/g,
22+
// @Input('inputNameC') varName = 'adv';
23+
setterInputCustomNameSelector:
24+
/@Input\(("|')[a-zA-Z0-9-_]+("|')\)(\s+)[a-zA-Z0-9-_]+(\s+)=(\s+)[A-Za-z0-9"']+(;|)/g,
25+
//@Input() set foo(value: 'type1' | 'type2') {}
26+
setterInputLiteralTypeSelector:
27+
/@Input\(\)(\s+)set(\s+)[a-zA-Z0-9-_]+\([a-zA-Z0-9-_]+:((\s+)('|")[a-zA-Z0-9-_]+('|")(\s+)\|)+(\s)('|")[a-zA-Z0-9-_]+('|")\)/g,
28+
29+
// outputs
30+
// @Output() buttonClick: EventEmitter<any> = new EventEmitter()
31+
regularOutputSelector:
32+
/@Output\(\)(\s+)[a-zA-Z0-9-_]+:(\s+)EventEmitter<[a-zA-Z0-9-_]+>(\s+)=(\s+)new(\s+)EventEmitter\(\);/g,
33+
34+
// other
35+
extendedClassSelector:
36+
/export(\s+)class(\s+)[a-zA-Z0-9-_]+(\s+)extends(\s+)[a-zA-Z0-9-_]+/g,
37+
extendedClassPathSelector:
38+
/import(\s+){(\s+)[a-zA-Z0-9-_]+(\s+)}(\s+)from(\s+)[\/\@A-Za-z0-9."'_-]+/g,
39+
};

0 commit comments

Comments
 (0)