-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_javascript_test.go
More file actions
138 lines (131 loc) · 3.56 KB
/
Copy pathscan_javascript_test.go
File metadata and controls
138 lines (131 loc) · 3.56 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
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
package highlight
import (
"strings"
"testing"
"github.com/croaky/is"
)
func TestScanJS(t *testing.T) {
for _, tt := range []struct {
name string
in state
line string
want string
out state
}{
{
name: "declaration and numbers",
line: "const s = 1000, m = 60 * s;",
want: "k:const|: s = |m:1000|:, m = |m:60|: * s;",
},
{
name: "declaration and call",
line: "function reltime(t) {",
want: "k:function|: |n:reltime|:(t) {",
},
{
name: "keyword and string",
line: " if (d < 5 * s) return \"<5s ago\";",
want: ": |k:if|: (d < |m:5|: * s) |k:return|: |s:\"<5s ago\"|:;",
},
{
name: "line comment",
line: "el.innerHTML = html; // why",
want: ":el.innerHTML = html; |c:// why",
},
{
name: "block comment left open",
line: "const a = 1; /* why",
want: "k:const|: a = |m:1|:; |c:/* why",
out: stateBlockComment,
},
{
name: "block comment closing",
in: stateBlockComment,
line: "still why */ const b = 2;",
want: "c:still why */|: |k:const|: b = |m:2|:;",
},
{
// A backtick would have closed the Go string this file used
// to live in, so a template literal is newly expressible,
// and it is the one string that spans lines.
name: "template literal left open",
line: "const q = `hello",
want: "k:const|: q = |s:`hello",
out: stateRawString,
},
{
name: "template literal closing",
in: stateRawString,
line: "world`;",
want: "s:world`|:;",
},
{
// The slash a value can follow is a regex; the slash an
// operand can follow is division.
name: "regex literal",
line: "if (/^(input|select)$/i.test(t.tagName)) {",
want: "k:if|: (|s:/^(input|select)$/i|:.|n:test|:(t.tagName)) {",
},
{
name: "division",
line: "return Math.round(d / s) + \"s ago\";",
want: "k:return|: |n:Math|:.|n:round|:(d / s) + |s:\"s ago\"|:;",
},
{
name: "an unclosed slash is not a regex",
line: "const half = total / 2;",
want: "k:const|: half = total / |m:2|:;",
},
{
// A literal ends an operand, so a slash after one divides.
// This compared the byte the previous token began with,
// which for a string is its opening quote, so no literal
// ever counted and `/ b /` came back as a regex.
name: "a slash after a string divides",
line: `const x = "a" / b / c;`,
want: `k:const|: x = |s:"a"|: / b / c;`,
},
{
// The case this scanner already claimed to handle, by
// setting prev to a backtick -- which the rule answered no
// to, so the comment and the code disagreed.
name: "a slash after a template literal divides",
in: stateRawString,
line: "a` / b / c;",
want: "s:a`|: / b / c;",
},
{
name: "escaped quote",
line: "if (e.key !== \"\\\\\") {",
want: "k:if|: (e.key !== |s:\"\\\\\"|:) {",
},
{
name: "spread and arrow",
line: "const open = [...files].some((d) => !d.open);",
want: "k:const|: open = [...files].|n:some|:((d) => !d.open);",
},
{
name: "await and a global",
line: "const r = await fetch(el.dataset.live);",
want: "k:const|: r = |k:await|: |n:fetch|:(el.dataset.live);",
},
} {
t.Run(tt.name, func(t *testing.T) {
is := is.NewRelaxed(t)
ts, out := scanJS(tt.in, tt.line)
is.Eq(formatTokens(ts), tt.want)
is.Eq(out, tt.out)
})
}
}
// TestScanJSByExtension checks the wiring.
func TestScanJSByExtension(t *testing.T) {
is := is.NewRelaxed(t)
got := string(Diff("ui/script.js", "@@ -1 +1 @@\n+const a = 1;\n"))
for _, want := range []string{
`<span class=k>const</span>`,
`<span class=m>1</span>`,
} {
is.True(strings.Contains(got, want))
}
}