Skip to content

Commit 7d3d1c7

Browse files
committed
wip
1 parent 89c80fe commit 7d3d1c7

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

internal/js_scanner/js_scanner.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ func isKeyword(value []byte) bool {
229229
return js.Keywords[string(value)] != 0
230230
}
231231

232+
// isPropsAliasing checks if we're in a Props aliasing context (import { Props as X })
233+
// rather than destructuring with 'as' property ({ as: Component })
234+
func isPropsAliasing(idents []string) bool {
235+
return len(idents) > 0 && idents[len(idents)-1] == "Props"
236+
}
237+
232238
func HoistImports(source []byte) HoistedScripts {
233239
imports := make([][]byte, 0)
234240
importLocs := make([]loc.Loc, 0)
@@ -340,7 +346,8 @@ outer:
340346
if js.IsIdentifier(token) {
341347
if isKeyword(value) {
342348
// fix(#814): fix Props detection when using `{ Props as SomethingElse }`
343-
if ident == "Props" && string(value) == "as" {
349+
// fix(#927): only reset Props when 'as' follows 'Props' in the same context
350+
if ident == "Props" && string(value) == "as" && isPropsAliasing(idents) {
344351
start = 0
345352
ident = defaultPropType
346353
idents = make([]string, 0)

internal/js_scanner/js_scanner_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,3 +775,96 @@ func TestGetObjectKeys(t *testing.T) {
775775
})
776776
}
777777
}
778+
779+
func TestGetPropsType(t *testing.T) {
780+
tests := []struct {
781+
name string
782+
source string
783+
want Props
784+
}{
785+
{
786+
name: "no props",
787+
source: `const foo = "bar"`,
788+
want: Props{
789+
Ident: "Record<string, any>",
790+
Statement: "",
791+
Generics: "",
792+
},
793+
},
794+
{
795+
name: "interface Props",
796+
source: `interface Props {
797+
foo: string;
798+
}`,
799+
want: Props{
800+
Ident: "Props",
801+
Statement: "",
802+
Generics: "",
803+
},
804+
},
805+
{
806+
name: "type Props",
807+
source: `type Props = {
808+
foo: string;
809+
}`,
810+
want: Props{
811+
Ident: "Props",
812+
Statement: "",
813+
Generics: "",
814+
},
815+
},
816+
{
817+
name: "Props with generics",
818+
source: `interface Props<T> {
819+
foo: T;
820+
}`,
821+
want: Props{
822+
Ident: "Props",
823+
Statement: "<T>",
824+
Generics: "<T>",
825+
},
826+
},
827+
{
828+
name: "destructuring with 'as' prop name without type assertion - issue #927",
829+
source: `interface Props {
830+
as?: string;
831+
href?: string;
832+
}
833+
const { as: Component, href } = Astro.props;`,
834+
want: Props{
835+
Ident: "Props",
836+
Statement: "",
837+
Generics: "",
838+
},
839+
},
840+
{
841+
name: "destructuring with 'as' prop name with type assertion",
842+
source: `interface Props {
843+
as?: string;
844+
href?: string;
845+
}
846+
const { as: Component, href } = Astro.props as Props;`,
847+
want: Props{
848+
Ident: "Props",
849+
Statement: "",
850+
Generics: "",
851+
},
852+
},
853+
}
854+
855+
for _, tt := range tests {
856+
t.Run(tt.name, func(t *testing.T) {
857+
got := GetPropsType([]byte(tt.source))
858+
if got.Ident != tt.want.Ident {
859+
t.Errorf("Ident: got %q, want %q", got.Ident, tt.want.Ident)
860+
t.Logf("Source:\n%s", tt.source)
861+
}
862+
if got.Statement != tt.want.Statement {
863+
t.Errorf("Statement: got %q, want %q", got.Statement, tt.want.Statement)
864+
}
865+
if got.Generics != tt.want.Generics {
866+
t.Errorf("Generics: got %q, want %q", got.Generics, tt.want.Generics)
867+
}
868+
})
869+
}
870+
}

0 commit comments

Comments
 (0)