-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(unplugin-macro): prevent transforming unrelated components (#45)
* chore: add failing test * fix(unplugin-macro): prevent transforming unrelated components
- Loading branch information
Showing
6 changed files
with
165 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pandabox/unplugin-panda-macro": patch | ||
--- | ||
|
||
Fix an issue where some unrelated components from Panda would be transformed due to having the same name as some Panda components (JSX Patterns like Stack) |
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
53 changes: 53 additions & 0 deletions
53
packages/unplugin-panda-macro/src/plugin/get-import-declarations.ts
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,53 @@ | ||
// https://github.com/chakra-ui/panda/blob/0bf09f214ec25ff3ea74b8e432bd10c7c9453805/packages/parser/src/get-import-declarations.ts | ||
import { resolveTsPathPattern } from '@pandacss/config/ts-path' | ||
import type { ImportResult, ParserOptions } from '@pandacss/core' | ||
import type { SourceFile } from 'ts-morph' | ||
import { getModuleSpecifierValue } from './get-module-specifier-value' | ||
import { hasMacroAttribute } from './has-macro-attribute' | ||
|
||
export function getImportDeclarations(context: ParserOptions, sourceFile: SourceFile, onlyMacroImports = false) { | ||
const { imports, tsOptions } = context | ||
|
||
const importDeclarations: ImportResult[] = [] | ||
|
||
sourceFile.getImportDeclarations().forEach((node) => { | ||
const mod = getModuleSpecifierValue(node) | ||
if (!mod) return | ||
if (onlyMacroImports && !hasMacroAttribute(node)) return | ||
|
||
// import { flex, stack } from "styled-system/patterns" | ||
node.getNamedImports().forEach((specifier) => { | ||
const name = specifier.getNameNode().getText() | ||
const alias = specifier.getAliasNode()?.getText() || name | ||
|
||
const result: ImportResult = { name, alias, mod, kind: 'named' } | ||
|
||
const found = imports.match(result, (mod) => { | ||
if (!tsOptions?.pathMappings) return | ||
return resolveTsPathPattern(tsOptions.pathMappings, mod) | ||
}) | ||
|
||
if (!found) return | ||
|
||
importDeclarations.push(result) | ||
}) | ||
|
||
// import * as p from "styled-system/patterns | ||
const namespace = node.getNamespaceImport() | ||
if (namespace) { | ||
const name = namespace.getText() | ||
const result: ImportResult = { name, alias: name, mod, kind: 'namespace' } | ||
|
||
const found = imports.match(result, (mod) => { | ||
if (!tsOptions?.pathMappings) return | ||
return resolveTsPathPattern(tsOptions.pathMappings, mod) | ||
}) | ||
|
||
if (!found) return | ||
|
||
importDeclarations.push(result) | ||
} | ||
}) | ||
|
||
return importDeclarations | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/unplugin-panda-macro/src/plugin/get-module-specifier-value.ts
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,9 @@ | ||
import type { ImportDeclaration } from 'ts-morph' | ||
|
||
export const getModuleSpecifierValue = (node: ImportDeclaration) => { | ||
try { | ||
return node.getModuleSpecifierValue() | ||
} catch { | ||
return | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/unplugin-panda-macro/src/plugin/has-macro-attribute.ts
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,23 @@ | ||
import type { ImportDeclaration } from 'ts-morph' | ||
import { Node } from 'ts-morph' | ||
|
||
export const hasMacroAttribute = (node: ImportDeclaration) => { | ||
const attrs = node.getAttributes() | ||
if (!attrs) return | ||
|
||
const elements = attrs.getElements() | ||
if (!elements.length) return | ||
|
||
return elements.some((n) => { | ||
const name = n.getName() | ||
if (name === 'type') { | ||
const value = n.getValue() | ||
if (!Node.isStringLiteral(value)) return | ||
|
||
const type = value.getLiteralText() | ||
if (type === 'macro') { | ||
return true | ||
} | ||
} | ||
}) | ||
} |
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