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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
package api_test
import (
"testing"
"github.com/evanw/esbuild/internal/test"
"github.com/evanw/esbuild/pkg/api"
)
func TestFormatMessages(t *testing.T) {
check := func(name string, opts api.FormatMessagesOptions, msg api.Message, expected string) {
t.Helper()
t.Run(name, func(t *testing.T) {
test.AssertEqualWithDiff(t, api.FormatMessages([]api.Message{msg}, opts)[0], expected)
})
}
check("Error", api.FormatMessagesOptions{Kind: api.ErrorMessage}, api.Message{Text: "This is a test"}, "✘ [ERROR] This is a test\n\n")
check("Warning", api.FormatMessagesOptions{Kind: api.WarningMessage}, api.Message{Text: "This is a test"}, "▲ [WARNING] This is a test\n\n")
check("Basic location",
api.FormatMessagesOptions{},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 5, // 0-based
Length: 3,
LineText: "this.foo();",
Suggestion: "bar",
}},
`✘ [ERROR] This is a test
some file.js:100:5:
100 │ this.foo();
│ ~~~
╵ bar
`,
)
check("Unicode location",
api.FormatMessagesOptions{
Kind: api.WarningMessage,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 17, // In UTF-8 bytes
Length: 10, // In UTF-8 bytes
LineText: "𝓉𝒽𝒾𝓈.𝒻ℴℴ();",
Suggestion: "𝒷𝒶𝓇",
}},
`▲ [WARNING] This is a test
some file.js:100:17:
100 │ 𝓉𝒽𝒾𝓈.𝒻ℴℴ();
│ ~~~
╵ 𝒷𝒶𝓇
`,
)
check("Tab stop rendering",
api.FormatMessagesOptions{
Kind: api.WarningMessage,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 6,
Length: 4,
LineText: "0\t1\t23\t45\t678",
}},
`▲ [WARNING] This is a test
some file.js:100:6:
100 │ 0 1 23 45 678
╵ ~~~~~~
`,
)
check("Truncated location tail, zero length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 3,
Length: 0,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:3:
100 │ 012345678 abcdefg...
╵ ^
`,
)
check("Truncated location tail, nonzero length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 3,
Length: 6,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:3:
100 │ 012345678 abcdefg...
╵ ~~~~~~
`,
)
check("Truncated location tail, truncated length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 3,
Length: 100,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:3:
100 │ 012345678 abcdefg...
╵ ~~~~~~~~~~~~~~
`,
)
check("Truncated location head, zero length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 200,
Length: 0,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:59:
100 │ ...defghi ABCDEFGHI
╵ ^
`,
)
check("Truncated location head, nonzero length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 50,
Length: 200,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:50:
100 │ ...cdefghi ABCDEFGHI
╵ ~~~~~~~~~
`,
)
check("Truncated location head and tail, truncated length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 30,
Length: 30,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:30:
100 │ ... 012345678 abc...
╵ ~~~~~~~~~~~~~
`,
)
check("Truncated location head and tail, non-truncated length",
api.FormatMessagesOptions{
TerminalWidth: 32,
},
api.Message{Text: "This is a test", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 30,
Length: 9,
LineText: "012345678 abcdefghi ABCDEFGHI 012345678 abcdefghi ABCDEFGHI",
}},
`✘ [ERROR] This is a test
some file.js:100:30:
100 │ ...HI 012345678 a...
╵ ~~~~~~~~~
`,
)
check("Multi-line line text",
api.FormatMessagesOptions{},
api.Message{Text: "ReferenceError: Cannot access 'foo' before initialization", Location: &api.Location{
File: "some file.js",
Line: 100,
Column: 2,
LineText: ` foo();
at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)`,
}},
`✘ [ERROR] ReferenceError: Cannot access 'foo' before initialization
some file.js:100:2:
100 │ foo();
╵ ^
at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
`,
)
check("Note formatting",
api.FormatMessagesOptions{
TerminalWidth: 40,
},
api.Message{
Text: "Why would you do this?",
Location: &api.Location{
File: "some file.js",
Line: 1,
Column: 10,
Length: 16,
LineText: "let ten = +([+!+[]]+[+[]]);",
},
Notes: []api.Note{{
Text: "This is 1:",
Location: &api.Location{
File: "some file.js",
Line: 1,
Column: 12,
Length: 7,
LineText: "let ten = +([+!+[]]+[+[]]);",
Suggestion: "'1'",
},
}, {
Text: "This is 0:",
Location: &api.Location{
File: "some file.js",
Line: 1,
Column: 20,
Length: 5,
LineText: "let ten = +([+!+[]]+[+[]]);",
Suggestion: "'0'",
},
}, {
Text: "The number 0 is created by +[], where [] is the empty array and + is the unary plus, " +
"used to convert the right side to a numeric value. The number 1 is formed as +!+[], where " +
"the boolean value true is converted into the numeric value 1 by the prepended plus sign.",
}},
},
`✘ [ERROR] Why would you do this?
some file.js:1:10:
1 │ let ten = +([+!+[]]+[+[]]);
╵ ~~~~~~~~~~~~~~~~
This is 1:
some file.js:1:12:
1 │ let ten = +([+!+[]]+[+[]]);
│ ~~~~~~~
╵ '1'
This is 0:
some file.js:1:20:
1 │ let ten = +([+!+[]]+[+[]]);
│ ~~~~~
╵ '0'
The number 0 is created by +[], where
[] is the empty array and + is the
unary plus, used to convert the right
side to a numeric value. The number 1
is formed as +!+[], where the boolean
value true is converted into the
numeric value 1 by the prepended plus
sign.
`,
)
}
|