-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added 'replaceIdentifierInCode', new feature to replace content names…
… using Babel. This is util for the resource where the duplicated names are renamed.
- Loading branch information
Showing
6 changed files
with
115 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { parse } = require("@babel/parser"); | ||
const traverse = require("@babel/traverse").default; | ||
const generate = require("@babel/generator").default; | ||
|
||
/** | ||
* Troca o nome do recurso dentro do código, tanto para o arquivo principal quanto para os arquivos que dependem | ||
* do arquivo original. | ||
* | ||
* Se um arquivo com nome Foo tiver seu nome trocado, isso será aplicado corretamente sem alterar | ||
* outros dados ou estruturas. | ||
* | ||
* Já um arquivo que depende do principal (quando esse principal é alterado) ira sofrer alterações somente | ||
* no nome do recurso específico. Ou sejá, se Foo for mudado para Bar, somente este item será mudado em todo o arquivo | ||
* incluindo JSX. Exemplo: | ||
* | ||
* import Foo from "./caminho/Foo" | ||
* console.log(Foo); | ||
* <Test>Foo<Test/> | ||
* <Foo /> | ||
* | ||
* se tornará | ||
* | ||
* import Bar from "./caminho/Bar" | ||
* console.log(Bar); | ||
* <Test>Foo<Test/> | ||
* <Bar /> | ||
* | ||
* | ||
* @param {*} code | ||
* @param {*} identifier | ||
* @param {*} newIdentifier | ||
* @returns | ||
*/ | ||
function replaceIdentifierInCode(code, identifier, newIdentifier) { | ||
const ast = parse(code, { | ||
sourceType: "module", | ||
plugins: ["jsx", "typescript"], // Support for TypeScript and JSX | ||
}); | ||
|
||
traverse(ast, { | ||
enter(path) { | ||
if ( | ||
path.isIdentifier({ name: identifier }) || | ||
path.isJSXIdentifier({ name: identifier }) | ||
) { | ||
path.node.name = newIdentifier; | ||
} else if ( | ||
path.isImportSpecifier() && | ||
path.node.imported.name === identifier | ||
) { | ||
path.node.imported.name = newIdentifier; | ||
} else if ( | ||
path.isImportDefaultSpecifier() && | ||
path.node.local.name === identifier | ||
) { | ||
path.node.local.name = newIdentifier; | ||
} | ||
}, | ||
}); | ||
|
||
return generate(ast, { retainLines: true }).code; | ||
} | ||
|
||
module.exports = replaceIdentifierInCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters