Skip to content

Commit

Permalink
try catch for removeCommentsFromTsx
Browse files Browse the repository at this point in the history
  • Loading branch information
wpdas committed Apr 1, 2024
1 parent 4f5164a commit ac53a62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
10 changes: 9 additions & 1 deletion lib/actions/loadFilesInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ const processFileSchema = (filePath) => {

// Remove comments from file
// INFO: Esta sendo usado para remover comentários de arquivos jsx também
fileContent = removeCommentsFromTSX(fileContent);
const removeCommentsResult = removeCommentsFromTSX(fileContent, filePath);
hasError = removeCommentsResult.error;

if (hasError) {
log.error(hasError);
return;
}

fileContent = removeCommentsResult.code;
const fileImportsPath = helpers.getImportsPath(fileContent);

const currentFileSchema = {
Expand Down
5 changes: 3 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,9 @@ const getFilePathBasedOnParentAndChildFilePath = (parentPath, childPath) => {
* Escapa os ` inclusos nos arquivos
* @param {string} content
*/
const scapeBacktick = (content) => content.replace(/[`$]/g, "\\$&");
// const scapeBacktick = (content) => content.replace(/(?<!\\)([`$])/g, "\\$1");
const scapeBacktick = (content) => content.replace(/(?<!\\)([`$])/g, "\\$1"); // V2, verifica se ja nao foi escapado antes de escapar
// const scapeBacktick = (content) => content.replace(/[`$]/g, "\\$&"); // V1 é melhor. V2 esta quebrando
// comentarios usando "//"

/**
* Converte object em array
Expand Down
55 changes: 31 additions & 24 deletions lib/parsers/removeCommentsFromTSX.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@ const pluginSyntaxJsx = require("./pluginSyntaxJsx");
const presetTypescriptPath = require("./presetTypescriptPath");
const traverse = require("@babel/traverse").default;

function removeCommentsFromTSX(code) {
function removeCommentsFromTSX(code, filePath) {
// Adicionando um nome de arquivo fictício para o contexto da transformação
const filename = "file.tsx";
const filename = filePath.replace(".js", ".tsx").replace(".jsx", ".tsx");
let error = null;

const ast = babel.parse(code, {
presets: [presetTypescriptPath],
plugins: [pluginSyntaxJsx],
filename, // Incluindo o nome do arquivo nas opções
});
try {
const ast = babel.parse(code, {
presets: [presetTypescriptPath],
plugins: [pluginSyntaxJsx],
filename, // Incluindo o nome do arquivo nas opções
});

traverse(ast, {
enter(path) {
path.node.leadingComments =
path.node.trailingComments =
path.node.innerComments =
null;
},
});
traverse(ast, {
enter(path) {
path.node.leadingComments =
path.node.trailingComments =
path.node.innerComments =
null;
},
});

// Incluindo o nome do arquivo também aqui
const output = babel.transformFromAstSync(ast, code, {
presets: [presetTypescriptPath],
plugins: [pluginSyntaxJsx],
filename, // Incluindo o nome do arquivo nas opções
code: true,
comments: false,
});
// Incluindo o nome do arquivo também aqui
const output = babel.transformFromAstSync(ast, code, {
presets: [presetTypescriptPath],
plugins: [pluginSyntaxJsx],
filename, // Incluindo o nome do arquivo nas opções
code: true,
comments: false,
});

return output.code;
return { code: output.code, error };
} catch (syntaxError) {
error = syntaxError.message;

return { code, error };
}
}

module.exports = removeCommentsFromTSX;

0 comments on commit ac53a62

Please sign in to comment.