Skip to content

Commit

Permalink
refactor(macro): remove alreadyVisitedCache in favour of dedupe refer…
Browse files Browse the repository at this point in the history
…ences from babel-macro-plugin
  • Loading branch information
timofei-iatsenko committed Feb 15, 2023
1 parent 24d3258 commit b24690d
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions packages/macro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ function macro({ references, state, babel, config }: MacroParams) {
const opts: LinguiMacroOpts = config as LinguiMacroOpts

const jsxNodes = new Set<NodePath>()
const jsNodes: NodePath[] = []
const jsNodes = new Set<NodePath>()
let needsI18nImport = false

Object.keys(references).forEach((tagName) => {
const nodes = references[tagName]

if (jsMacroTags.has(tagName)) {
nodes.forEach((node) => {
jsNodes.push(node.parentPath)
jsNodes.add(node.parentPath)
})
} else if (jsxMacroTags.has(tagName)) {
// babel-plugin-macros return JSXIdentifier nodes.
// Which is for every JSX element would be presented twice (opening / close)
// Here we're taking JSXElement and dedupe it.

nodes.forEach((node) => {
// identifier.openingElement.jsxElement
jsxNodes.add(node.parentPath.parentPath)
Expand All @@ -80,16 +79,16 @@ function macro({ references, state, babel, config }: MacroParams) {
const stripNonEssentialProps =
process.env.NODE_ENV == "production" && !opts.extract

jsNodes.filter(isRootPath(jsNodes)).forEach((path) => {
if (alreadyVisited(path)) return
const jsNodesArray = Array.from(jsNodes)

jsNodesArray.filter(isRootPath(jsNodesArray)).forEach((path) => {
const macro = new MacroJS(babel, { i18nImportName, stripNonEssentialProps })
if (macro.replacePath(path)) needsI18nImport = true
})

const jsxNodesArray = Array.from(jsxNodes)

jsxNodesArray.filter(isRootPath(jsxNodesArray)).forEach((path) => {
if (alreadyVisited(path)) return
const macro = new MacroJSX(babel, { stripNonEssentialProps })
macro.replacePath(path)
})
Expand Down Expand Up @@ -159,16 +158,6 @@ function isRootPath(allPath: NodePath[]) {
})(node)
}

const alreadyVisitedCache = new WeakSet()
const alreadyVisited = (path: NodePath) => {
if (alreadyVisitedCache.has(path)) {
return true
} else {
alreadyVisitedCache.add(path)
return false
}
}

;[...jsMacroTags, ...jsxMacroTags].forEach((name) => {
Object.defineProperty(module.exports, name, {
get() {
Expand Down

0 comments on commit b24690d

Please sign in to comment.