Skip to content

Commit 426f930

Browse files
authored
Merge pull request #262 from samuzora/master
2 parents 10cd382 + 8b1aed9 commit 426f930

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/compile.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import compileToString from './compile-string'
22
import { getConfig } from './config'
3-
import { asyncFunc } from './utils'
3+
import { asyncFunc, isValidJSIdentifier } from './utils'
44
import SqrlErr from './err'
55

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

3033
/* END ASYNC HANDLING */
3134
try {

src/utils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,9 @@ function trimWS (
113113
return str
114114
}
115115

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

0 commit comments

Comments
 (0)