forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.js
More file actions
45 lines (36 loc) · 1.06 KB
/
check.js
File metadata and controls
45 lines (36 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import isESModule from '../../utils/is-es-module';
const JSXSyntax = /\n(.*?)<[A-z](.|\n)*?\/?>/;
const regeneratorSyntax = /\n(.*?)(\s|^)regeneratorRuntime\./;
function checkComment(match) {
const startOfLine = match[1];
// If it's in a comment or string, we're extremely aggressive here because
// transpiling is absolutely our last resort.
if (
startOfLine.indexOf('//') > -1 ||
startOfLine.indexOf('*') > -1 ||
startOfLine.indexOf("'") > -1 ||
startOfLine.indexOf('"') > -1 ||
startOfLine.indexOf('`') > -1
) {
return false;
}
return true;
}
export function shouldTranspile(code: string, path: string) {
if (isESModule(code)) {
return true;
}
if (path.endsWith('.min.js')) {
// This needs no transpiling and often fools our JSX check with <a etc...
return false;
}
const jsxMatch = code.match(JSXSyntax);
if (jsxMatch) {
return checkComment(jsxMatch);
}
const regeneratorMatch = code.match(regeneratorSyntax);
if (regeneratorMatch) {
return checkComment(regeneratorMatch);
}
return false;
}