Skip to content

Commit

Permalink
compiler now changes the useState values during the building process
Browse files Browse the repository at this point in the history
  • Loading branch information
wpdas committed Mar 12, 2024
1 parent 7c1d15d commit 71bd4ef
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 22 deletions.
23 changes: 6 additions & 17 deletions lib/actions/handleNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ const {
BETWEEN_EXPORT_VAR_AND_EQUAL,
BETWEEN_EXPORT_FUNCTION_AND_OPEN_PARENTHESE,
} = require("../regexp");
const {
capitalize_first_letter,
get_randon_hexadecimal,
get_random_character,
} = require("../utils");
const { create_new_name } = require("../utils");
const handleUseStateValues = require("./handleUseStateValues");

/**
* Get "export const" items
Expand Down Expand Up @@ -161,17 +158,6 @@ const getExportsOrganizer = (fileSchemas) => {
return exportsOrganizer;
};

/**
* Generate new name
* @param {string} currentName
*/
const createNewName = () => {
let randomHexName = get_randon_hexadecimal(6);
return capitalize_first_letter(
`${get_random_character()}${get_random_character()}_${randomHexName}`,
);
};

/**
* Change the fileSchema.content in other files when a dependent item gets its name changed
* @param {string} contentFilePath
Expand Down Expand Up @@ -437,7 +423,7 @@ const handleNames = (fileSchemas) => {
/**
* = {"caminho/arquivo.tsx": [index of export key][key name]}
*/
const newName = createNewName(exportKeyName);
const newName = create_new_name(exportKeyName);
exportsOrganizer[itemKey][objIndex][exportKeyName] = newName;
// exportsOrganizer[itemKey][importKeyName] = 'NewName'

Expand All @@ -459,6 +445,9 @@ const handleNames = (fileSchemas) => {
tempBundle += fileContent;
});

// Update useState values name
fileSchemas = handleUseStateValues(fileSchemas);

return fileSchemas;
};

Expand Down
59 changes: 59 additions & 0 deletions lib/actions/handleUseStateValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { USESTATE_VALUES } = require("../regexp");
const { create_new_name } = require("../utils");

/**
* Handle the useState values creating a new name for them to avoid
* conflicts over the app as it uses only one instance of state for the widget
*
* @param {{filePath: string, toImport: string[], content: string}[]} fileSchemas schemas to change the files content when a item gets its name changed
*/
const handleUseStateValues = (fileSchemas) => {
// console.log(fileSchemas);
fileSchemas.forEach((fileSchema, fileSchemaIndex) => {
let currentFileContent = fileSchema.content;
const useStateList = currentFileContent.match(USESTATE_VALUES);
// Se achar valores de useState, atualiza no conteúdo do arquivo
if (useStateList) {
// loop through found values
useStateList.forEach((useStateCases) => {
// get values
const [getter, setter] = useStateCases.split(",");

// create new name for them and update the schema file content
const newGetterName = create_new_name(true);
const newSetterName = create_new_name(true);

// change names in file content
if (getter) {
const replaceGetterRegexp = new RegExp(
"\\b" + getter.replaceAll(" ", "") + "\\b",
"gm",
);

currentFileContent = currentFileContent.replaceAll(
replaceGetterRegexp,
newGetterName,
);
}

if (setter) {
const replaceSetterRegexp = new RegExp(
"\\b" + setter.replaceAll(" ", "") + "\\b",
"gm",
);

currentFileContent = currentFileContent.replaceAll(
replaceSetterRegexp,
newSetterName,
);
}

fileSchemas[fileSchemaIndex].content = currentFileContent;
});
}
});

return fileSchemas;
};

module.exports = handleUseStateValues;
9 changes: 5 additions & 4 deletions lib/actions/loadFilesInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ const processFileSchema = (filePath) => {
if (importedFileContentPath) {
currentFileSchema.nextFilesToLoad.push(importedFileContentPath);
orderedFilesToImport.push(importedFileContentPath);
} else {
console.log(
`${filePath} -> Arquivo dependente nao encontrado: ${importPath}`,
);
}
// else {
// console.log(
// `${filePath} -> Arquivo dependente nao encontrado: ${importPath}`,
// );
// }

processedFiles.push(importedFileContentPath);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const AFTER_EXPORT_DEFAULT = /(?<=export default).*/gm;

const EXPORT_AND_EXPORT_DEFAULT = /export\s+default|export(?!\s+default)/g;

const USESTATE_VALUES = /(?<=\[)(.*?)(?=\]\s*=\s*useState)/g;

module.exports = {
SPECIAL_CHARACTERS,
SPECIAL_CHARACTERS_AND_SPACES,
Expand All @@ -52,6 +54,7 @@ module.exports = {
BETWEEN_EXPORT_DEFAULT_FUNCTION_AND_OPEN_PARENTHESE,
AFTER_EXPORT_DEFAULT,
EXPORT_AND_EXPORT_DEFAULT,
USESTATE_VALUES,
};

// ENTRE export const e =
Expand Down
22 changes: 22 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ const capitalize_first_letter = (word) => {
return word.charAt(0).toUpperCase() + word.slice(1);
};

let nameChangeCounter = 0;
/**
* Generate new name
* @param {string} currentName
*/
const create_new_name = (toLowerCase = false) => {
// let randomHexName = get_randon_hexadecimal(6);
// return capitalize_first_letter(
// `${get_random_character()}${get_random_character()}_${randomHexName}`,
// );
nameChangeCounter++;

if (toLowerCase) {
return `${get_random_character()}_${nameChangeCounter}`;
}

return capitalize_first_letter(
`${get_random_character()}_${nameChangeCounter}`,
);
};

module.exports = {
create_dist,
for_rfile,
Expand All @@ -176,4 +197,5 @@ module.exports = {
get_randon_hexadecimal,
get_random_character,
capitalize_first_letter,
create_new_name,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alem",
"description": "Create web3 applications for NEAR BOS with a focus on performance and friendly development.",
"version": "0.0.1-alpha.22",
"version": "0.0.1-alpha.23",
"main": "main.js",
"types": "index.d.ts",
"author": "Wenderson Pires - wendersonpires.near",
Expand Down

0 comments on commit 71bd4ef

Please sign in to comment.