Skip to content

Commit

Permalink
fix(#814): improve Props import detection (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Jul 5, 2023
1 parent a35468a commit 4aba173
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-crews-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix props detection when importing `Props` from another file (see [#814](https://github.com/withastro/compiler/issues/814))
6 changes: 6 additions & 0 deletions internal/js_scanner/js_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ outer:

if js.IsIdentifier(token) {
if isKeyword(value) {
// fix(#814): fix Props detection when using `{ Props as SomethingElse }`
if ident == "Props" && string(value) == "as" {
start = 0
ident = defaultPropType
idents = make([]string, 0)
}
i += len(value)
continue
}
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler/test/tsx/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,23 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}\n
assert.snapshot(code, output, `expected code to match snapshot`);
});

test('unrelated sibling prop', async () => {
const input = `---
import type { Props as ComponentBProps } from './ComponentB.astro'
---
<div />
`;
const output = `
import type { Props as ComponentBProps } from './ComponentB.astro'
"";<Fragment>
<div />
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});

test.run();

0 comments on commit 4aba173

Please sign in to comment.