Skip to content

Commit

Permalink
Merge pull request #22 from wpdas/feat/improve-change-detect
Browse files Browse the repository at this point in the history
Feat: improve change detect
  • Loading branch information
wpdas committed May 8, 2024
2 parents 65385f8 + 7b2e61b commit 5a8bd47
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 234 deletions.
138 changes: 138 additions & 0 deletions lib/actions/getFileExports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* Handle const/var/let names to avoid duplicates
*/
const {
BETWEEN_EXPORT_CONST_AND_EQUAL,
SPACES,
BETWEEN_EXPORT_DEFAULT_FUNCTION_AND_OPEN_PARENTHESE,
AFTER_EXPORT_DEFAULT,
SPECIAL_CHARACTERS_AND_SPACES,
BETWEEN_EXPORT_LET_AND_EQUAL,
BETWEEN_EXPORT_VAR_AND_EQUAL,
BETWEEN_EXPORT_FUNCTION_AND_OPEN_PARENTHESE,
} = require("../regexp");

/**
* Get "export const" items
* @param {string} content
*/
const getExportConsts = (content) => {
let foundItems = content.match(BETWEEN_EXPORT_CONST_AND_EQUAL);

// Remove spaces
if (foundItems) {
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
}

return foundItems || [];
};

/**
* Get "export let" items
* @param {string} content
*/
const getExportLets = (content) => {
let foundItems = content.match(BETWEEN_EXPORT_LET_AND_EQUAL);

// Remove spaces
if (foundItems) {
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
}

return foundItems || [];
};

/**
* Get "export var" items
* @param {string} content
*/
const getExportVars = (content) => {
let foundItems = content.match(BETWEEN_EXPORT_VAR_AND_EQUAL);

// Remove spaces
if (foundItems) {
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
}

return foundItems || [];
};

/**
* Get "export function" items
* @param {string} content
*/
const getExportFunction = (content) => {
let foundItems = content.match(BETWEEN_EXPORT_FUNCTION_AND_OPEN_PARENTHESE);

// Remove spaces
if (foundItems) {
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
}

return foundItems || [];
};

/**
* Get "export default function" items
* @param {string} content
*/
const getExportDefaultFunction = (content) => {
let foundItems = content.match(
BETWEEN_EXPORT_DEFAULT_FUNCTION_AND_OPEN_PARENTHESE,
);

// Remove spaces
if (foundItems) {
foundItems = foundItems.map((item) => item.replaceAll(SPACES, ""));
}

return foundItems || [];
};

/**
* Get "export default" items
* @param {string} content
*/
const getExportDefault = (content) => {
// Remove all "export default function"
const _content = content.replaceAll("export default function", "");

let foundItems = _content.match(AFTER_EXPORT_DEFAULT);

// Remove spaces
if (foundItems) {
foundItems = foundItems.map((item) =>
item.replaceAll(SPACES, "").replaceAll(SPECIAL_CHARACTERS_AND_SPACES, ""),
);
}

return foundItems || [];
};

/**
* Generates the exports schema
*
*
* exports organizer - original state
*
* returns: [{MyVar: "MyVar"}, {MyVar2: "MyVar2"}]
*
*
* @param {{filePath: string, toImport: string[], content: string}[]} fileSchemas
* @returns {Record<string, {Record<string, string>}[]>}
*/
const getFileExports = (fileContent) => {
// TODO: melhorar isso usando Babel ? (pode ser mais penoso para processar se usar Babel)
const exports = [
...getExportConsts(fileContent),
...getExportLets(fileContent),
...getExportVars(fileContent),
...getExportFunction(fileContent),
...getExportDefaultFunction(fileContent),
...getExportDefault(fileContent),
];

return exports;
};

module.exports = getFileExports;
Loading

0 comments on commit 5a8bd47

Please sign in to comment.