Skip to content

Commit 9f5e572

Browse files
committed
move input parsing into its own function
1 parent e33e13a commit 9f5e572

File tree

1 file changed

+121
-116
lines changed

1 file changed

+121
-116
lines changed

src/parser.ts

Lines changed: 121 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -55,122 +55,7 @@ export const parser = (filePaths: Array<string>): Array<File> => {
5555
logger.log("Selector:", selector);
5656
}
5757

58-
// notice we ignore the default value of the input in the regex
59-
// Input() foo: 'type1' | 'type2'
60-
let inputs: Array<Input> = [];
61-
let inputsData: Array<string> = file?.match(REGEX_SELECTORS.regularInputLiteralTypeSelector) || [];
62-
for (let input of inputsData) {
63-
logger.log("inputs parsed:", inputsData);
64-
let tmp: Array<string> = input.replace(/(\s)+/g, " ").split(" ");
65-
let type = tmp.slice(2, tmp.length).join().replace(/\"/g, "'").replace(";", "").replace(/,/g, "");
66-
inputs.push({
67-
inputName: tmp[1].replace(":", ""),
68-
type,
69-
});
70-
}
71-
file = file.replace(REGEX_SELECTORS.regularInputLiteralTypeSelector, "");
72-
73-
// @Input() variableName: type; and @Input() variableName: number = 9;
74-
inputsData = [];
75-
inputsData = file?.match(REGEX_SELECTORS.regularInputWithTypeSelector) || [];
76-
for (let input of inputsData) {
77-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
78-
inputs.push({
79-
inputName: tmp[1].replace(":", ""),
80-
type: tmp[2].replace(";", ""),
81-
});
82-
}
83-
file = file.replace(REGEX_SELECTORS.regularInputWithTypeSelector, "");
84-
85-
inputsData = [];
86-
// @Input('inputName') varName: type; and @Input("inputName") varName: type
87-
inputsData = file?.match(REGEX_SELECTORS.customNameInputWithTypeSelector) || [];
88-
for (let input of inputsData) {
89-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
90-
const inputName = (tmp[0].match(/('|")[a-zA-Z0-9-_]+('|")/g) || [])[0].replace(/'|"/g, "");
91-
inputs.push({
92-
inputName,
93-
type: tmp[2].replace(";", ""),
94-
});
95-
}
96-
file = file.replace(REGEX_SELECTORS.customNameInputWithTypeSelector, "");
97-
98-
// @Input('inputNameC') varName = 'adv';
99-
// @Input("inputNameD") varName = 2354;
100-
inputsData = [];
101-
inputsData = file?.match(REGEX_SELECTORS.setterInputCustomNameSelector) || [];
102-
for (let input of inputsData) {
103-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
104-
const inputName = (tmp[0].match(/('|")[a-zA-Z0-9-_]+('|")/g) || [""])[0].replace(/'|"/g, "");
105-
inputs.push({
106-
inputName,
107-
type: undefined,
108-
});
109-
}
110-
file = file.replace(REGEX_SELECTORS.setterInputCustomNameSelector, "");
111-
112-
//@Input() set foo(value) {}
113-
inputsData = [];
114-
inputsData = file?.match(REGEX_SELECTORS.setterInputSelector) || [];
115-
for (let input of inputsData) {
116-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
117-
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
118-
inputs.push({
119-
inputName,
120-
type: undefined,
121-
});
122-
}
123-
file = file.replace(REGEX_SELECTORS.setterInputSelector, "");
124-
125-
//@Input() set foo(value: type) {}
126-
inputsData = [];
127-
inputsData = file?.match(REGEX_SELECTORS.setterInputWithTypeSelector) || [];
128-
for (let input of inputsData) {
129-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
130-
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
131-
const type = tmp[3].replace(/(\s+)/g, "").split(")")[0];
132-
inputs.push({
133-
inputName,
134-
type,
135-
});
136-
}
137-
file = file.replace(REGEX_SELECTORS.setterInputWithTypeSelector, "");
138-
139-
//@Input() set foo(value: 'type1' | 'type2') {}
140-
inputsData = [];
141-
inputsData = file?.match(REGEX_SELECTORS.setterInputLiteralTypeSelector) || [];
142-
for (let input of inputsData) {
143-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
144-
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
145-
const type = tmp
146-
.slice(3, tmp.length)
147-
.join()
148-
.replace(/'|"|\)/g, "")
149-
.replace(/,/g, " ");
150-
inputs.push({
151-
inputName,
152-
type,
153-
});
154-
}
155-
file = file.replace(REGEX_SELECTORS.setterInputLiteralTypeSelector, "");
156-
157-
// @Input() variableName; and @Input() variableName. Also for now we will parse
158-
// in this part of the code @Input() variableName = value and @Input() variableName = value;
159-
inputsData = [];
160-
inputsData = file?.match(REGEX_SELECTORS.regularInputSelector) || [];
161-
for (let input of inputsData) {
162-
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
163-
const inputName = tmp[1].replace(";", "");
164-
if (inputs.filter((elem) => elem.inputName == inputName).length > 0) {
165-
continue;
166-
}
167-
inputs.push({
168-
inputName,
169-
type: undefined,
170-
});
171-
}
172-
file = file.replace(REGEX_SELECTORS.regularInputSelector, "");
173-
logger.log("Inputs detected:", inputs);
58+
let inputs: Array<Input> = parseInputs(file);
17459

17560
let outputs: Array<Output> = [];
17661
// only @Output() buttonClick: EventEmitter<any> = new EventEmitter(); for now
@@ -241,3 +126,123 @@ export const parser = (filePaths: Array<string>): Array<File> => {
241126
}
242127
return result;
243128
};
129+
130+
const parseInputs = (file: string): Array<Input> => {
131+
// notice we ignore the default value of the input in the regex
132+
// Input() foo: 'type1' | 'type2'
133+
let inputsData: Array<string> = file?.match(REGEX_SELECTORS.regularInputLiteralTypeSelector) || [],
134+
inputs: Array<Input> = [];
135+
for (let input of inputsData) {
136+
logger.log("inputs parsed:", inputsData);
137+
let tmp: Array<string> = input.replace(/(\s)+/g, " ").split(" ");
138+
let type = tmp.slice(2, tmp.length).join().replace(/\"/g, "'").replace(";", "").replace(/,/g, "");
139+
inputs.push({
140+
inputName: tmp[1].replace(":", ""),
141+
type,
142+
});
143+
}
144+
file = file.replace(REGEX_SELECTORS.regularInputLiteralTypeSelector, "");
145+
146+
// @Input() variableName: type; and @Input() variableName: number = 9;
147+
inputsData = [];
148+
inputsData = file?.match(REGEX_SELECTORS.regularInputWithTypeSelector) || [];
149+
for (let input of inputsData) {
150+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
151+
inputs.push({
152+
inputName: tmp[1].replace(":", ""),
153+
type: tmp[2].replace(";", ""),
154+
});
155+
}
156+
file = file.replace(REGEX_SELECTORS.regularInputWithTypeSelector, "");
157+
158+
inputsData = [];
159+
// @Input('inputName') varName: type; and @Input("inputName") varName: type
160+
inputsData = file?.match(REGEX_SELECTORS.customNameInputWithTypeSelector) || [];
161+
for (let input of inputsData) {
162+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
163+
const inputName = (tmp[0].match(/('|")[a-zA-Z0-9-_]+('|")/g) || [])[0].replace(/'|"/g, "");
164+
inputs.push({
165+
inputName,
166+
type: tmp[2].replace(";", ""),
167+
});
168+
}
169+
file = file.replace(REGEX_SELECTORS.customNameInputWithTypeSelector, "");
170+
171+
// @Input('inputNameC') varName = 'adv';
172+
// @Input("inputNameD") varName = 2354;
173+
inputsData = [];
174+
inputsData = file?.match(REGEX_SELECTORS.setterInputCustomNameSelector) || [];
175+
for (let input of inputsData) {
176+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
177+
const inputName = (tmp[0].match(/('|")[a-zA-Z0-9-_]+('|")/g) || [""])[0].replace(/'|"/g, "");
178+
inputs.push({
179+
inputName,
180+
type: undefined,
181+
});
182+
}
183+
file = file.replace(REGEX_SELECTORS.setterInputCustomNameSelector, "");
184+
185+
//@Input() set foo(value) {}
186+
inputsData = [];
187+
inputsData = file?.match(REGEX_SELECTORS.setterInputSelector) || [];
188+
for (let input of inputsData) {
189+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
190+
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
191+
inputs.push({
192+
inputName,
193+
type: undefined,
194+
});
195+
}
196+
file = file.replace(REGEX_SELECTORS.setterInputSelector, "");
197+
198+
//@Input() set foo(value: type) {}
199+
inputsData = [];
200+
inputsData = file?.match(REGEX_SELECTORS.setterInputWithTypeSelector) || [];
201+
for (let input of inputsData) {
202+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
203+
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
204+
const type = tmp[3].replace(/(\s+)/g, "").split(")")[0];
205+
inputs.push({
206+
inputName,
207+
type,
208+
});
209+
}
210+
file = file.replace(REGEX_SELECTORS.setterInputWithTypeSelector, "");
211+
212+
//@Input() set foo(value: 'type1' | 'type2') {}
213+
inputsData = [];
214+
inputsData = file?.match(REGEX_SELECTORS.setterInputLiteralTypeSelector) || [];
215+
for (let input of inputsData) {
216+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
217+
const inputName = tmp[2].replace(/(\s+)/g, "").split("(")[0];
218+
const type = tmp
219+
.slice(3, tmp.length)
220+
.join()
221+
.replace(/'|"|\)/g, "")
222+
.replace(/,/g, " ");
223+
inputs.push({
224+
inputName,
225+
type,
226+
});
227+
}
228+
file = file.replace(REGEX_SELECTORS.setterInputLiteralTypeSelector, "");
229+
230+
// @Input() variableName; and @Input() variableName. Also for now we will parse
231+
// in this part of the code @Input() variableName = value and @Input() variableName = value;
232+
inputsData = [];
233+
inputsData = file?.match(REGEX_SELECTORS.regularInputSelector) || [];
234+
for (let input of inputsData) {
235+
let tmp: Array<string> = input.replace(/(\s+)/g, " ").split(" ");
236+
const inputName = tmp[1].replace(";", "");
237+
if (inputs.filter((elem) => elem.inputName == inputName).length > 0) {
238+
continue;
239+
}
240+
inputs.push({
241+
inputName,
242+
type: undefined,
243+
});
244+
}
245+
file = file.replace(REGEX_SELECTORS.regularInputSelector, "");
246+
logger.log("Inputs detected:", inputs);
247+
return inputs;
248+
};

0 commit comments

Comments
 (0)