-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate import aliases bug #87
Comments
I've rewritten the babel plugin before, because of the risk of discrepancies between the official swc and babel translations. public safeAddImportsBinding(name: string, node: Identifier) {
const st = this.importsBindingMap.get(name) ?? new Set<Identifier>()
// safeUpdate
this.importsBindingMap.set(name, st)
st.add(node)
}
public safeHasImportsBinding(name: string, node: Identifier): boolean {
const st = this.importsBindingMap.get(name) ?? new Set<Identifier>()
// safeUpdate
this.importsBindingMap.set(name, st)
return st.has(node)
}
public registerMacroImport(impPath: NodePath<ImportDeclaration>) {
impPath.node.specifiers.forEach((spec) => {
if (t.isImportSpecifier(spec)) {
const imported = spec.imported
const local = spec.local
if (t.isIdentifier(imported)) {
this.importsIdMap.set(imported.name, local.name)
this.importsIdMapInverted.set(local.name, imported.name)
const bindng = impPath.scope.getBinding(local.name)
bindng?.referencePaths.forEach((path) => {
this.safeAddImportsBinding(local.name, path.node as Identifier)
})
}
}
})
}
public isLinguiIdent(name: string, ident: Identifier): boolean {
return (
this.importsIdMap.get(name) === ident.name &&
this.safeHasImportsBinding(ident.name, ident)
)
} |
babel's version uses native babel's capabilities already and doesn't have this flaw. SWC version, indeed, should be improved for that case. PS What is the real usecase of this snippet? import { t as $t, t } from '@lingui/macro' |
It's a long story. At first our code only supported English, then I used jscodeshift to convert plain text to lingui syntax. const renderString = 'Hello';
const renderEle = <div>Hello</div>; auto translate by script import {t as $t, Trans} from '@lingui/macro';
const renderString = $t`Hello`;
const renderEle = <div><Trans>Hello</Trans></div>; With the previous strategy, Perhaps because of the large number of alias However, if there are developers at different levels in the project, there is a risk of an production incident if swc does not convert multiple aliases. It's hard to find problems in compile detection unless you add eslint rules manually (which I'm currently planning to do). However, as an underlying framework, it is necessary for swc to support basic js syntax. |
Only the last
t
variable will be registered inimports_id_map
Maybe need to add a secondary data structure
importsBindingMap
?If swc has babel's
scope.getBinding
, it can maintain the importsBindingMap during the registration phase.Please extend swc's capabilities~
The text was updated successfully, but these errors were encountered: