Skip to content

Commit e56b277

Browse files
authored
Merge pull request #17 from roguib/refactor-parser
Refactor parser
2 parents 930c574 + 8224276 commit e56b277

21 files changed

+586
-440
lines changed

.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"printWidth": 160
5+
}

DESIGN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Once every file has been parsed, generate a JSON with the information gathered a
1111
## High-level implementation
1212

1313
```ts
14-
public walker(root: string, filesExploredPath: Array<string>): Array<string>
14+
public reader(root: string): Array<string>
1515
```
1616

17-
The function `walker` will start at the root directory and save those files candidates of having components defined inside of them. Only will consider files with `.ts` extension. Should traverse all the file tree from the root up to the bottom and return the full path to every candidate file.
17+
The function `reader` will start at the root directory and save those files candidates of having components defined inside of them. Only will consider files with `.ts` extension. Should traverse all the file tree from the root up to the bottom and return the full path to every candidate file, the content of the file and the type of definition that contains that file (component, pipe or directive).
1818

1919
```ts
2020
public parser(filePaths: Array<string>): Array<File>

src/generator.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ import logger from "./shared/logger";
33
const fs = require("fs");
44
const path = require("path");
55

6+
/**
7+
*
8+
* @param {Array<File>} files An array of tokens generated after parsing the content of every file
9+
* @param {string} outputPath The location in which the function has to write the file with the deffinitions
10+
* of the snippets
11+
*/
612
export const generator = (files: Array<File>, outputPath: string): void => {
713
// scope will be html only for now
814
let json = Object();
915
for (const file of files) {
10-
let component = Object(), inputs = "", index = 1;
16+
let component = Object(),
17+
inputs = "",
18+
index = 1;
1119
component.scope = "html";
1220
component.prefix = file.prefix;
1321
for (let input of file.inputs) {
14-
if (input.type?.indexOf('|') != -1 && input.type) {
15-
inputs +=
16-
` [${input.inputName}]=\"$\{${index}\|${input.type.replace(/(\s)\|(\s)/g, ',')
17-
.replace(/'/g, '')}\|\}\"`;
18-
}
19-
else {
20-
inputs += input.type === "string" ?
21-
` ${input.inputName}=` :
22-
` [${input.inputName}]=`;
22+
if (input.type?.indexOf("|") != -1 && input.type) {
23+
inputs += ` [${input.inputName}]=\"$\{${index}\|${input.type.replace(/(\s)\|(\s)/g, ",").replace(/'/g, "")}\|\}\"`;
24+
} else {
25+
inputs += input.type === "string" ? ` ${input.inputName}=` : ` [${input.inputName}]=`;
2326
inputs += `\"$${index}\"`;
2427
}
2528
++index;
@@ -30,11 +33,9 @@ export const generator = (files: Array<File>, outputPath: string): void => {
3033
outputs += ` (${output.outputName})=\"$${index}\"`;
3134
++index;
3235
}
33-
component.body = [
34-
`<${file.prefix}` + inputs + outputs + `></${file.prefix}>`,
35-
];
36+
component.body = [`<${file.prefix}` + inputs + outputs + `></${file.prefix}>`];
3637
json[file.componentName] = {
37-
...component
38+
...component,
3839
};
3940
}
4041
const dir = path.join(outputPath, "/out.code-snippets");

src/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const path = require("path");
22
const argv = process.argv;
3-
import * as walker from "./walker";
3+
import * as reader from "./reader";
44
import * as parser from "./parser";
55
import * as generator from "./generator";
66
import { File } from "./shared/IFile";
77
import logger from "./shared/logger";
88
import { ICLIConfig } from "./shared/ICLIConfig";
9+
import { FileType } from "./shared/constants";
10+
import { IFileData } from "./shared/IFileData";
911

1012
let config: ICLIConfig = {
1113
workingDir: null,
@@ -65,10 +67,7 @@ export const run = async (args: string[]) => {
6567

6668
process.env.ROOT_PROJECT_PATH = config.workingDir || path.posix.resolve();
6769

68-
let candidateFilePaths: Array<string> = walker.walker(
69-
process.env.ROOT_PROJECT_PATH as string,
70-
[]
71-
);
70+
let candidateFilePaths: Array<IFileData> = reader.reader(process.env.ROOT_PROJECT_PATH as string);
7271
let fileData: Array<File> = parser.parser(candidateFilePaths);
7372
generator.generator(fileData, config.outputDir as string);
7473
};

0 commit comments

Comments
 (0)