FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
plugin-loop/scripts/script-source-transform.js at main · just-every/plugin-loop · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
just-every
/
plugin-loop
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
plugin-loop
/
scripts
/
script-source-transform.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
168 lines (145 loc) · 3.86 KB
Breadcrumbs
plugin-loop
/
scripts
/
script-source-transform.js
Copy path
File metadata and controls
168 lines (145 loc) · 3.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict"
;
const
EXPORTED_DECLARATIONS
=
new
Set
(
[
"const"
,
"let"
,
"var"
,
"async"
,
"function"
,
"class"
]
)
;
function
transformSource
(
source
)
{
const
body
=
rewriteModuleExports
(
String
(
source
==
null
?
""
:
source
)
)
;
return
`"use strict";\n
${
body
}
`
;
}
function
rewriteModuleExports
(
source
)
{
const
rewrites
=
findExportRewrites
(
source
)
;
if
(
rewrites
.
length
===
0
)
return
source
;
let
body
=
""
;
let
cursor
=
0
;
for
(
const
rewrite
of
rewrites
)
{
body
+=
source
.
slice
(
cursor
,
rewrite
.
start
)
;
body
+=
rewrite
.
text
;
cursor
=
rewrite
.
end
;
}
body
+=
source
.
slice
(
cursor
)
;
return
body
;
}
function
findExportRewrites
(
source
)
{
const
rewrites
=
[
]
;
let
state
=
"code"
;
let
escaped
=
false
;
let
braceDepth
=
0
;
for
(
let
i
=
0
;
i
<
source
.
length
;
i
+=
1
)
{
const
ch
=
source
[
i
]
;
const
next
=
source
[
i
+
1
]
;
if
(
state
===
"lineComment"
)
{
if
(
ch
===
"\n"
)
state
=
"code"
;
continue
;
}
if
(
state
===
"blockComment"
)
{
if
(
ch
===
"*"
&&
next
===
"/"
)
{
state
=
"code"
;
i
+=
1
;
}
continue
;
}
if
(
state
===
"singleQuote"
||
state
===
"doubleQuote"
||
state
===
"template"
)
{
if
(
escaped
)
{
escaped
=
false
;
continue
;
}
if
(
ch
===
"\\"
)
{
escaped
=
true
;
continue
;
}
if
(
(
state
===
"singleQuote"
&&
ch
===
"'"
)
||
(
state
===
"doubleQuote"
&&
ch
===
'"'
)
||
(
state
===
"template"
&&
ch
===
"`"
)
)
{
state
=
"code"
;
}
continue
;
}
if
(
ch
===
"/"
&&
next
===
"/"
)
{
state
=
"lineComment"
;
i
+=
1
;
continue
;
}
if
(
ch
===
"/"
&&
next
===
"*"
)
{
state
=
"blockComment"
;
i
+=
1
;
continue
;
}
if
(
ch
===
"'"
)
{
state
=
"singleQuote"
;
continue
;
}
if
(
ch
===
'"'
)
{
state
=
"doubleQuote"
;
continue
;
}
if
(
ch
===
"`"
)
{
state
=
"template"
;
continue
;
}
if
(
ch
===
"{"
)
{
braceDepth
+=
1
;
continue
;
}
if
(
ch
===
"}"
)
{
braceDepth
=
Math
.
max
(
0
,
braceDepth
-
1
)
;
continue
;
}
if
(
braceDepth
===
0
&&
isLinePrefixWhitespace
(
source
,
i
)
&&
startsKeyword
(
source
,
i
,
"export"
)
)
{
const
rewrite
=
exportRewriteAt
(
source
,
i
)
;
if
(
rewrite
)
{
rewrites
.
push
(
rewrite
)
;
i
=
rewrite
.
scanEnd
-
1
;
}
}
}
return
rewrites
;
}
function
exportRewriteAt
(
source
,
index
)
{
const
afterExport
=
index
+
"export"
.
length
;
const
tokenStart
=
skipWhitespace
(
source
,
afterExport
)
;
if
(
startsKeyword
(
source
,
tokenStart
,
"default"
)
)
{
const
afterDefault
=
skipWhitespace
(
source
,
tokenStart
+
"default"
.
length
)
;
return
{
start
:
index
,
end
:
afterDefault
,
text
:
"return "
,
scanEnd
:
afterDefault
}
;
}
const
declaration
=
readIdentifier
(
source
,
tokenStart
)
;
if
(
EXPORTED_DECLARATIONS
.
has
(
declaration
.
value
)
)
{
return
{
start
:
index
,
end
:
tokenStart
,
text
:
""
,
scanEnd
:
tokenStart
}
;
}
return
null
;
}
function
isLinePrefixWhitespace
(
source
,
index
)
{
for
(
let
i
=
index
-
1
;
i
>=
0
;
i
-=
1
)
{
const
ch
=
source
[
i
]
;
if
(
ch
===
"\n"
||
ch
===
"\r"
)
return
true
;
if
(
ch
!==
" "
&&
ch
!==
"\t"
)
return
false
;
}
return
true
;
}
function
skipWhitespace
(
source
,
index
)
{
let
i
=
index
;
while
(
i
<
source
.
length
&&
/
\s
/
.
test
(
source
[
i
]
)
)
i
+=
1
;
return
i
;
}
function
readIdentifier
(
source
,
index
)
{
let
i
=
index
;
while
(
i
<
source
.
length
&&
isIdentifierPart
(
source
[
i
]
)
)
i
+=
1
;
return
{
value
:
source
.
slice
(
index
,
i
)
,
end
:
i
}
;
}
function
startsKeyword
(
source
,
index
,
keyword
)
{
if
(
!
source
.
startsWith
(
keyword
,
index
)
)
return
false
;
return
!
isIdentifierPart
(
source
[
index
-
1
]
)
&&
!
isIdentifierPart
(
source
[
index
+
keyword
.
length
]
)
;
}
function
isIdentifierPart
(
ch
)
{
return
typeof
ch
===
"string"
&&
/
[
A
-
Z
a
-
z
0
-
9
_
$
]
/
.
test
(
ch
)
;
}
module
.
exports
=
{
transformSource
}
;
Back
|
FazBrowse Home
|
New Git URL