Skip to content
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

fix: handle module IDs containing quotes #683

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*.go]
indent_style = tab
indent_size = 2
trim_trailing_whitespace = true
3 changes: 2 additions & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ func (p *printer) printFuncSuffix(opts transform.TransformOptions) {
componentName := getComponentName(opts.Pathname)
p.addNilSourceMapping()
if len(opts.ModuleId) > 0 {
p.println(fmt.Sprintf("}, '%s');", opts.ModuleId))
escapedModuleId := strings.ReplaceAll(opts.ModuleId, "'", "\\'")
p.println(fmt.Sprintf("}, '%s');", escapedModuleId))
} else {
p.println("});")
}
Expand Down
13 changes: 11 additions & 2 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ const someProps = {
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="icon" type="image/x-icon" href="/favicon.ico">

` + RENDER_HEAD_RESULT + `</head>
<body class="astro-HMNNHVCQ">
<main class="astro-HMNNHVCQ">
Expand Down Expand Up @@ -2332,6 +2332,14 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$maybeRenderHead($$result)}<div>test</div>`,
},
},
{
name: "passes escaped moduleId into createComponent if it contains single quotes",
source: `<div>test</div>`,
moduleId: "/projects/app/src/pages/page-with-'-quotes.astro",
want: want{
code: `${$$maybeRenderHead($$result)}<div>test</div>`,
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -2483,7 +2491,8 @@ const items = ["Dog", "Cat", "Platipus"];
}

if len(tt.moduleId) > 0 {
toMatch += suffixWithModuleId(tt.moduleId)
escapedModuleId := strings.ReplaceAll(tt.moduleId, "'", "\\'")
toMatch += suffixWithModuleId(escapedModuleId)
Comment on lines +2494 to +2495
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for complete correctness we might want to use:

func escapeForJSON(value string) string {

but since this is a file path, it would almost always not have double backslashes to mess up the replacements, so I think this is fine for now.

} else {
toMatch += SUFFIX
}
Expand Down