Skip to content

Commit 5a8bd47

Browse files
authored
Merge pull request #22 from wpdas/feat/improve-change-detect
Feat: improve change detect
2 parents 65385f8 + 7b2e61b commit 5a8bd47

File tree

11 files changed

+515
-234
lines changed

11 files changed

+515
-234
lines changed

lib/actions/getFileExports.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Handle const/var/let names to avoid duplicates
3+
*/
4+
const {
5+
BETWEEN_EXPORT_CONST_AND_EQUAL,
6+
SPACES,
7+
BETWEEN_EXPORT_DEFAULT_FUNCTION_AND_OPEN_PARENTHESE,
8+
AFTER_EXPORT_DEFAULT,
9+
SPECIAL_CHARACTERS_AND_SPACES,
10+
BETWEEN_EXPORT_LET_AND_EQUAL,
11+
BETWEEN_EXPORT_VAR_AND_EQUAL,
12+
BETWEEN_EXPORT_FUNCTION_AND_OPEN_PARENTHESE,
13+
} = require("../regexp");
14+
15+
/**
16+
* Get "export const" items
17+
* @param {string} content
18+
*/
19+
const getExportConsts = (content) => {
20+
let foundItems = content.match(BETWEEN_EXPORT_CONST_AND_EQUAL);
21+
22+
// Remove spaces
23+
if (foundItems) {
24+
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
25+
}
26+
27+
return foundItems || [];
28+
};
29+
30+
/**
31+
* Get "export let" items
32+
* @param {string} content
33+
*/
34+
const getExportLets = (content) => {
35+
let foundItems = content.match(BETWEEN_EXPORT_LET_AND_EQUAL);
36+
37+
// Remove spaces
38+
if (foundItems) {
39+
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
40+
}
41+
42+
return foundItems || [];
43+
};
44+
45+
/**
46+
* Get "export var" items
47+
* @param {string} content
48+
*/
49+
const getExportVars = (content) => {
50+
let foundItems = content.match(BETWEEN_EXPORT_VAR_AND_EQUAL);
51+
52+
// Remove spaces
53+
if (foundItems) {
54+
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
55+
}
56+
57+
return foundItems || [];
58+
};
59+
60+
/**
61+
* Get "export function" items
62+
* @param {string} content
63+
*/
64+
const getExportFunction = (content) => {
65+
let foundItems = content.match(BETWEEN_EXPORT_FUNCTION_AND_OPEN_PARENTHESE);
66+
67+
// Remove spaces
68+
if (foundItems) {
69+
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
70+
}
71+
72+
return foundItems || [];
73+
};
74+
75+
/**
76+
* Get "export default function" items
77+
* @param {string} content
78+
*/
79+
const getExportDefaultFunction = (content) => {
80+
let foundItems = content.match(
81+
BETWEEN_EXPORT_DEFAULT_FUNCTION_AND_OPEN_PARENTHESE,
82+
);
83+
84+
// Remove spaces
85+
if (foundItems) {
86+
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
87+
}
88+
89+
return foundItems || [];
90+
};
91+
92+
/**
93+
* Get "export default" items
94+
* @param {string} content
95+
*/
96+
const getExportDefault = (content) => {
97+
// Remove all "export default function"
98+
const _content = content.replaceAll("export default function", "");
99+
100+
let foundItems = _content.match(AFTER_EXPORT_DEFAULT);
101+
102+
// Remove spaces
103+
if (foundItems) {
104+
foundItems = foundItems.map((item) =>
105+
item.replaceAll(SPACES, "").replaceAll(SPECIAL_CHARACTERS_AND_SPACES, ""),
106+
);
107+
}
108+
109+
return foundItems || [];
110+
};
111+
112+
/**
113+
* Generates the exports schema
114+
*
115+
*
116+
* exports organizer - original state
117+
*
118+
* returns: [{MyVar: "MyVar"}, {MyVar2: "MyVar2"}]
119+
*
120+
*
121+
* @param {{filePath: string, toImport: string[], content: string}[]} fileSchemas
122+
* @returns {Record<string, {Record<string, string>}[]>}
123+
*/
124+
const getFileExports = (fileContent) => {
125+
// TODO: melhorar isso usando Babel ? (pode ser mais penoso para processar se usar Babel)
126+
const exports = [
127+
...getExportConsts(fileContent),
128+
...getExportLets(fileContent),
129+
...getExportVars(fileContent),
130+
...getExportFunction(fileContent),
131+
...getExportDefaultFunction(fileContent),
132+
...getExportDefault(fileContent),
133+
];
134+
135+
return exports;
136+
};
137+
138+
module.exports = getFileExports;

0 commit comments

Comments
 (0)