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

check that options.varName is a valid JS identifier #262

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import compileToString from './compile-string'
import { getConfig } from './config'
import { asyncFunc } from './utils'
import { asyncFunc, isValidJSIdentifier } from './utils'
import SqrlErr from './err'

/* TYPES */
Expand All @@ -26,6 +26,9 @@ export default function compile (str: string, env?: PartialConfig): TemplateFunc
throw SqrlErr("This environment doesn't support async/await")
}
}
if (options.varName && isValidJSIdentifier(options.varName) === false) {
throw SqrlErr("options.varName must be a valid JS identifier")
}

/* END ASYNC HANDLING */
try {
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ function trimWS (
return str
}

export { trimWS }
// credit to pugjs/pug
function isValidJSIdentifier (str: string) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}

export { trimWS, isValidJSIdentifier }
Loading