wrangler 0.0.0-e2a26a0 → 0.0.0-e3cab74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/package.json +16 -7
  2. package/pages/functions/filepath-routing.test.ts +112 -36
  3. package/pages/functions/filepath-routing.ts +66 -119
  4. package/pages/functions/routes.ts +8 -17
  5. package/pages/functions/template-worker.ts +3 -9
  6. package/src/__tests__/configuration.test.ts +2185 -0
  7. package/src/__tests__/dev.test.tsx +48 -6
  8. package/src/__tests__/guess-worker-format.test.ts +66 -0
  9. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  10. package/src/__tests__/helpers/mock-account-id.ts +6 -6
  11. package/src/__tests__/helpers/mock-bin.ts +4 -1
  12. package/src/__tests__/helpers/mock-cfetch.ts +2 -2
  13. package/src/__tests__/helpers/mock-console.ts +11 -5
  14. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  15. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  16. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  17. package/src/__tests__/index.test.ts +343 -20
  18. package/src/__tests__/jest.setup.ts +21 -1
  19. package/src/__tests__/kv.test.ts +82 -80
  20. package/src/__tests__/package-manager.test.ts +2 -2
  21. package/src/__tests__/parse.test.ts +322 -0
  22. package/src/__tests__/publish.test.ts +1428 -244
  23. package/src/__tests__/r2.test.ts +198 -0
  24. package/src/__tests__/secret.test.ts +176 -60
  25. package/src/__tests__/sentry.test.ts +146 -0
  26. package/src/__tests__/tail.test.ts +564 -0
  27. package/src/api/form_data.ts +106 -23
  28. package/src/api/preview.ts +32 -13
  29. package/src/api/worker.ts +49 -18
  30. package/src/bundle.ts +133 -0
  31. package/src/cfetch/index.ts +8 -6
  32. package/src/cfetch/internal.ts +49 -17
  33. package/src/cli.ts +10 -0
  34. package/src/config/README.md +107 -0
  35. package/src/config/config.ts +265 -0
  36. package/src/config/diagnostics.ts +80 -0
  37. package/src/config/environment.ts +305 -0
  38. package/src/config/index.ts +52 -0
  39. package/src/config/validation-helpers.ts +370 -0
  40. package/src/config/validation.ts +1017 -0
  41. package/src/dev.tsx +338 -211
  42. package/src/entry.ts +57 -0
  43. package/src/environment-variables.ts +33 -0
  44. package/src/guess-worker-format.ts +68 -0
  45. package/src/index.tsx +860 -634
  46. package/src/inspect.ts +16 -3
  47. package/src/kv.tsx +20 -29
  48. package/src/module-collection.ts +141 -34
  49. package/src/open-in-browser.ts +13 -0
  50. package/src/package-manager.ts +21 -15
  51. package/src/pages.tsx +9 -10
  52. package/src/parse.ts +183 -0
  53. package/src/proxy.ts +11 -5
  54. package/src/publish.ts +272 -314
  55. package/src/r2.ts +50 -0
  56. package/src/reporting.ts +115 -0
  57. package/src/sites.tsx +16 -15
  58. package/src/tail/filters.ts +279 -0
  59. package/src/tail/index.ts +299 -0
  60. package/src/tail/printing.ts +62 -0
  61. package/src/user.tsx +74 -44
  62. package/templates/new-worker.js +1 -1
  63. package/templates/new-worker.ts +1 -1
  64. package/templates/tsconfig.json +105 -0
  65. package/wrangler-dist/cli.js +51308 -36440
  66. package/wrangler-dist/cli.js.map +3 -3
  67. package/src/config.ts +0 -641
  68. package/src/tail.tsx +0 -73
@@ -10,7 +10,7 @@ import { mockConsoleMethods } from "./helpers/mock-console";
10
10
  import { mockKeyListRequest } from "./helpers/mock-kv";
11
11
  import { runInTempDir } from "./helpers/run-in-tmp";
12
12
  import { runWrangler } from "./helpers/run-wrangler";
13
- import type { KVNamespaceInfo } from "../kv";
13
+ import type { KVNamespaceInfo, NamespaceKeyInfo } from "../kv";
14
14
 
15
15
  describe("wrangler", () => {
16
16
  mockAccountId();
@@ -53,12 +53,12 @@ describe("wrangler", () => {
53
53
  namespace The name of the new namespace [string] [required]
54
54
 
55
55
  Flags:
56
- -c, --config Path to .toml configuration file [string]
57
- -h, --help Show help [boolean]
58
- -v, --version Show version number [boolean]
56
+ -c, --config Path to .toml configuration file [string]
57
+ -h, --help Show help [boolean]
58
+ -v, --version Show version number [boolean]
59
+ --legacy-env Use legacy environments [boolean]
59
60
 
60
61
  Options:
61
- -l, --local Run on my machine [boolean] [default: false]
62
62
  --env Perform on a specific environment [string]
63
63
  --preview Interact with a preview namespace [boolean]
64
64
 
@@ -82,12 +82,12 @@ describe("wrangler", () => {
82
82
  namespace The name of the new namespace [string] [required]
83
83
 
84
84
  Flags:
85
- -c, --config Path to .toml configuration file [string]
86
- -h, --help Show help [boolean]
87
- -v, --version Show version number [boolean]
85
+ -c, --config Path to .toml configuration file [string]
86
+ -h, --help Show help [boolean]
87
+ -v, --version Show version number [boolean]
88
+ --legacy-env Use legacy environments [boolean]
88
89
 
89
90
  Options:
90
- -l, --local Run on my machine [boolean] [default: false]
91
91
  --env Perform on a specific environment [string]
92
92
  --preview Interact with a preview namespace [boolean]
93
93
 
@@ -112,12 +112,12 @@ describe("wrangler", () => {
112
112
  namespace The name of the new namespace [string] [required]
113
113
 
114
114
  Flags:
115
- -c, --config Path to .toml configuration file [string]
116
- -h, --help Show help [boolean]
117
- -v, --version Show version number [boolean]
115
+ -c, --config Path to .toml configuration file [string]
116
+ -h, --help Show help [boolean]
117
+ -v, --version Show version number [boolean]
118
+ --legacy-env Use legacy environments [boolean]
118
119
 
119
120
  Options:
120
- -l, --local Run on my machine [boolean] [default: false]
121
121
  --env Perform on a specific environment [string]
122
122
  --preview Interact with a preview namespace [boolean]
123
123
 
@@ -276,12 +276,12 @@ describe("wrangler", () => {
276
276
  Deletes a given namespace.
277
277
 
278
278
  Flags:
279
- -c, --config Path to .toml configuration file [string]
280
- -h, --help Show help [boolean]
281
- -v, --version Show version number [boolean]
279
+ -c, --config Path to .toml configuration file [string]
280
+ -h, --help Show help [boolean]
281
+ -v, --version Show version number [boolean]
282
+ --legacy-env Use legacy environments [boolean]
282
283
 
283
284
  Options:
284
- -l, --local Run on my machine [boolean] [default: false]
285
285
  --binding The name of the namespace to delete [string]
286
286
  --namespace-id The id of the namespace to delete [string]
287
287
  --env Perform on a specific environment [string]
@@ -467,23 +467,23 @@ describe("wrangler", () => {
467
467
  Writes a single key/value pair to the given namespace.
468
468
 
469
469
  Positionals:
470
- key The key to write to. [string] [required]
471
- value The value to write. [string]
470
+ key The key to write to [string] [required]
471
+ value The value to write [string]
472
472
 
473
473
  Flags:
474
- -c, --config Path to .toml configuration file [string]
475
- -h, --help Show help [boolean]
476
- -v, --version Show version number [boolean]
474
+ -c, --config Path to .toml configuration file [string]
475
+ -h, --help Show help [boolean]
476
+ -v, --version Show version number [boolean]
477
+ --legacy-env Use legacy environments [boolean]
477
478
 
478
479
  Options:
479
- -l, --local Run on my machine [boolean] [default: false]
480
- --binding The binding of the namespace to write to. [string]
481
- --namespace-id The id of the namespace to write to. [string]
480
+ --binding The binding of the namespace to write to [string]
481
+ --namespace-id The id of the namespace to write to [string]
482
482
  --env Perform on a specific environment [string]
483
483
  --preview Interact with a preview namespace [boolean]
484
- --ttl Time for which the entries should be visible. [number]
484
+ --ttl Time for which the entries should be visible [number]
485
485
  --expiration Time since the UNIX epoch after which the entry expires [number]
486
- --path Read value from the file at a given path. [string]
486
+ --path Read value from the file at a given path [string]
487
487
 
488
488
  Not enough non-option arguments: got 0, need at least 1"
489
489
  `);
@@ -503,23 +503,23 @@ describe("wrangler", () => {
503
503
  Writes a single key/value pair to the given namespace.
504
504
 
505
505
  Positionals:
506
- key The key to write to. [string] [required]
507
- value The value to write. [string]
506
+ key The key to write to [string] [required]
507
+ value The value to write [string]
508
508
 
509
509
  Flags:
510
- -c, --config Path to .toml configuration file [string]
511
- -h, --help Show help [boolean]
512
- -v, --version Show version number [boolean]
510
+ -c, --config Path to .toml configuration file [string]
511
+ -h, --help Show help [boolean]
512
+ -v, --version Show version number [boolean]
513
+ --legacy-env Use legacy environments [boolean]
513
514
 
514
515
  Options:
515
- -l, --local Run on my machine [boolean] [default: false]
516
- --binding The binding of the namespace to write to. [string]
517
- --namespace-id The id of the namespace to write to. [string]
516
+ --binding The binding of the namespace to write to [string]
517
+ --namespace-id The id of the namespace to write to [string]
518
518
  --env Perform on a specific environment [string]
519
519
  --preview Interact with a preview namespace [boolean]
520
- --ttl Time for which the entries should be visible. [number]
520
+ --ttl Time for which the entries should be visible [number]
521
521
  --expiration Time since the UNIX epoch after which the entry expires [number]
522
- --path Read value from the file at a given path. [string]
522
+ --path Read value from the file at a given path [string]
523
523
 
524
524
  Exactly one of the arguments binding and namespace-id is required"
525
525
  `);
@@ -539,23 +539,23 @@ describe("wrangler", () => {
539
539
  Writes a single key/value pair to the given namespace.
540
540
 
541
541
  Positionals:
542
- key The key to write to. [string] [required]
543
- value The value to write. [string]
542
+ key The key to write to [string] [required]
543
+ value The value to write [string]
544
544
 
545
545
  Flags:
546
- -c, --config Path to .toml configuration file [string]
547
- -h, --help Show help [boolean]
548
- -v, --version Show version number [boolean]
546
+ -c, --config Path to .toml configuration file [string]
547
+ -h, --help Show help [boolean]
548
+ -v, --version Show version number [boolean]
549
+ --legacy-env Use legacy environments [boolean]
549
550
 
550
551
  Options:
551
- -l, --local Run on my machine [boolean] [default: false]
552
- --binding The binding of the namespace to write to. [string]
553
- --namespace-id The id of the namespace to write to. [string]
552
+ --binding The binding of the namespace to write to [string]
553
+ --namespace-id The id of the namespace to write to [string]
554
554
  --env Perform on a specific environment [string]
555
555
  --preview Interact with a preview namespace [boolean]
556
- --ttl Time for which the entries should be visible. [number]
556
+ --ttl Time for which the entries should be visible [number]
557
557
  --expiration Time since the UNIX epoch after which the entry expires [number]
558
- --path Read value from the file at a given path. [string]
558
+ --path Read value from the file at a given path [string]
559
559
 
560
560
  Arguments binding and namespace-id are mutually exclusive"
561
561
  `);
@@ -575,23 +575,23 @@ describe("wrangler", () => {
575
575
  Writes a single key/value pair to the given namespace.
576
576
 
577
577
  Positionals:
578
- key The key to write to. [string] [required]
579
- value The value to write. [string]
578
+ key The key to write to [string] [required]
579
+ value The value to write [string]
580
580
 
581
581
  Flags:
582
- -c, --config Path to .toml configuration file [string]
583
- -h, --help Show help [boolean]
584
- -v, --version Show version number [boolean]
582
+ -c, --config Path to .toml configuration file [string]
583
+ -h, --help Show help [boolean]
584
+ -v, --version Show version number [boolean]
585
+ --legacy-env Use legacy environments [boolean]
585
586
 
586
587
  Options:
587
- -l, --local Run on my machine [boolean] [default: false]
588
- --binding The binding of the namespace to write to. [string]
589
- --namespace-id The id of the namespace to write to. [string]
588
+ --binding The binding of the namespace to write to [string]
589
+ --namespace-id The id of the namespace to write to [string]
590
590
  --env Perform on a specific environment [string]
591
591
  --preview Interact with a preview namespace [boolean]
592
- --ttl Time for which the entries should be visible. [number]
592
+ --ttl Time for which the entries should be visible [number]
593
593
  --expiration Time since the UNIX epoch after which the entry expires [number]
594
- --path Read value from the file at a given path. [string]
594
+ --path Read value from the file at a given path [string]
595
595
 
596
596
  Exactly one of the arguments value and path is required"
597
597
  `);
@@ -611,23 +611,23 @@ describe("wrangler", () => {
611
611
  Writes a single key/value pair to the given namespace.
612
612
 
613
613
  Positionals:
614
- key The key to write to. [string] [required]
615
- value The value to write. [string]
614
+ key The key to write to [string] [required]
615
+ value The value to write [string]
616
616
 
617
617
  Flags:
618
- -c, --config Path to .toml configuration file [string]
619
- -h, --help Show help [boolean]
620
- -v, --version Show version number [boolean]
618
+ -c, --config Path to .toml configuration file [string]
619
+ -h, --help Show help [boolean]
620
+ -v, --version Show version number [boolean]
621
+ --legacy-env Use legacy environments [boolean]
621
622
 
622
623
  Options:
623
- -l, --local Run on my machine [boolean] [default: false]
624
- --binding The binding of the namespace to write to. [string]
625
- --namespace-id The id of the namespace to write to. [string]
624
+ --binding The binding of the namespace to write to [string]
625
+ --namespace-id The id of the namespace to write to [string]
626
626
  --env Perform on a specific environment [string]
627
627
  --preview Interact with a preview namespace [boolean]
628
- --ttl Time for which the entries should be visible. [number]
628
+ --ttl Time for which the entries should be visible [number]
629
629
  --expiration Time since the UNIX epoch after which the entry expires [number]
630
- --path Read value from the file at a given path. [string]
630
+ --path Read value from the file at a given path [string]
631
631
 
632
632
  Arguments value and path are mutually exclusive"
633
633
  `);
@@ -839,7 +839,9 @@ describe("wrangler", () => {
839
839
  "kv:key list --namespace-id some-namespace-id --limit 100"
840
840
  );
841
841
  expect(std.err).toMatchInlineSnapshot(`""`);
842
- expect(JSON.parse(std.out).map((k) => k.name)).toEqual(keys);
842
+ expect(
843
+ JSON.parse(std.out).map((k: NamespaceKeyInfo) => k.name)
844
+ ).toEqual(keys);
843
845
  expect(requests.count).toEqual(6);
844
846
  });
845
847
  });
@@ -938,12 +940,12 @@ describe("wrangler", () => {
938
940
  key The key value to get. [string] [required]
939
941
 
940
942
  Flags:
941
- -c, --config Path to .toml configuration file [string]
942
- -h, --help Show help [boolean]
943
- -v, --version Show version number [boolean]
943
+ -c, --config Path to .toml configuration file [string]
944
+ -h, --help Show help [boolean]
945
+ -v, --version Show version number [boolean]
946
+ --legacy-env Use legacy environments [boolean]
944
947
 
945
948
  Options:
946
- -l, --local Run on my machine [boolean] [default: false]
947
949
  --binding The name of the namespace to get from [string]
948
950
  --namespace-id The id of the namespace to get from [string]
949
951
  --env Perform on a specific environment [string]
@@ -969,12 +971,12 @@ describe("wrangler", () => {
969
971
  key The key value to get. [string] [required]
970
972
 
971
973
  Flags:
972
- -c, --config Path to .toml configuration file [string]
973
- -h, --help Show help [boolean]
974
- -v, --version Show version number [boolean]
974
+ -c, --config Path to .toml configuration file [string]
975
+ -h, --help Show help [boolean]
976
+ -v, --version Show version number [boolean]
977
+ --legacy-env Use legacy environments [boolean]
975
978
 
976
979
  Options:
977
- -l, --local Run on my machine [boolean] [default: false]
978
980
  --binding The name of the namespace to get from [string]
979
981
  --namespace-id The id of the namespace to get from [string]
980
982
  --env Perform on a specific environment [string]
@@ -1001,12 +1003,12 @@ describe("wrangler", () => {
1001
1003
  key The key value to get. [string] [required]
1002
1004
 
1003
1005
  Flags:
1004
- -c, --config Path to .toml configuration file [string]
1005
- -h, --help Show help [boolean]
1006
- -v, --version Show version number [boolean]
1006
+ -c, --config Path to .toml configuration file [string]
1007
+ -h, --help Show help [boolean]
1008
+ -v, --version Show version number [boolean]
1009
+ --legacy-env Use legacy environments [boolean]
1007
1010
 
1008
1011
  Options:
1009
- -l, --local Run on my machine [boolean] [default: false]
1010
1012
  --binding The name of the namespace to get from [string]
1011
1013
  --namespace-id The id of the namespace to get from [string]
1012
1014
  --env Perform on a specific environment [string]
@@ -151,7 +151,7 @@ describe("getPackageManager()", () => {
151
151
  * Create a fake yarn binary
152
152
  */
153
153
  function mockYarn(succeed: boolean): void {
154
- let unMock;
154
+ let unMock: () => void;
155
155
  beforeEach(async () => {
156
156
  unMock = await mockBinary("yarn", `process.exit(${succeed ? 0 : 1})`);
157
157
  });
@@ -162,7 +162,7 @@ function mockYarn(succeed: boolean): void {
162
162
  * Create a fake npm binary
163
163
  */
164
164
  function mockNpm(succeed: boolean): void {
165
- let unMock;
165
+ let unMock: () => void;
166
166
  beforeEach(async () => {
167
167
  unMock = await mockBinary("npm", `process.exit(${succeed ? 0 : 1})`);
168
168
  });
@@ -0,0 +1,322 @@
1
+ import {
2
+ formatMessage,
3
+ searchLocation,
4
+ indexLocation,
5
+ parseJSON,
6
+ parseTOML,
7
+ } from "../parse";
8
+ import type { Message } from "../parse";
9
+
10
+ describe("formatMessage", () => {
11
+ const format = (input: Message) => {
12
+ // No color and skip emojis at the start.
13
+ return formatMessage(input, false).substring(2);
14
+ };
15
+
16
+ it("should format message without location", () => {
17
+ expect(
18
+ format({
19
+ text: "Invalid argument",
20
+ kind: "warning",
21
+ })
22
+ ).toMatchInlineSnapshot(`
23
+ "[WARNING] Invalid argument
24
+
25
+ "
26
+ `);
27
+ });
28
+
29
+ it("should format message with location", () => {
30
+ expect(
31
+ format({
32
+ text: "Missing property: main",
33
+ location: {
34
+ line: 1,
35
+ column: 0,
36
+ lineText: "{}",
37
+ file: "package.json",
38
+ fileText: "{}",
39
+ },
40
+ })
41
+ ).toMatchInlineSnapshot(`
42
+ "[ERROR] Missing property: main
43
+
44
+ package.json:1:0:
45
+ 1 │ {}
46
+ ╵ ^
47
+
48
+ "
49
+ `);
50
+ });
51
+
52
+ it("should format message with location and notes", () => {
53
+ expect(
54
+ format({
55
+ text: "Invalid property: type",
56
+ location: {
57
+ line: 3,
58
+ column: 8,
59
+ length: 7,
60
+ lineText: "type = 'modular'",
61
+ suggestion: "Did you mean 'module'?",
62
+ file: "package.toml",
63
+ fileText: "[package]\ntype = 'modular'\n",
64
+ },
65
+ notes: [
66
+ {
67
+ text: "There are two acceptable types: 'module' and 'commonjs'",
68
+ },
69
+ ],
70
+ })
71
+ ).toMatchInlineSnapshot(`
72
+ "[ERROR] Invalid property: type
73
+
74
+ package.toml:3:8:
75
+ 3 │ type = 'modular'
76
+ │ ~~~~~~~
77
+ ╵ Did you mean 'module'?
78
+
79
+ There are two acceptable types: 'module' and 'commonjs'
80
+
81
+ "
82
+ `);
83
+ });
84
+ });
85
+
86
+ describe("parseTOML", () => {
87
+ it("should parse toml that is empty", () => {
88
+ expect(parseTOML("")).toStrictEqual({});
89
+ });
90
+
91
+ it("should parse toml with basic values", () => {
92
+ expect(
93
+ parseTOML(`
94
+ name = "basic"
95
+ version = 1
96
+ `)
97
+ ).toStrictEqual({
98
+ name: "basic",
99
+ version: 1,
100
+ });
101
+ });
102
+
103
+ it("should parse toml with complex values", () => {
104
+ expect(
105
+ parseTOML(`
106
+ name = 'complex'
107
+ version = 1
108
+ [owner]
109
+ name = ["tim"]
110
+ alive = true
111
+ [owner.dog]
112
+ exists = true
113
+ `)
114
+ ).toStrictEqual({
115
+ name: "complex",
116
+ owner: {
117
+ alive: true,
118
+ dog: {
119
+ exists: true,
120
+ },
121
+ name: ["tim"],
122
+ },
123
+ version: 1,
124
+ });
125
+ });
126
+
127
+ it("should fail to parse toml with invalid string", () => {
128
+ try {
129
+ parseTOML(`name = 'fail"`);
130
+ fail("parseTOML did not throw");
131
+ } catch (err) {
132
+ expect({ ...(err as Error) }).toStrictEqual({
133
+ name: "ParseError",
134
+ text: "Unterminated string",
135
+ kind: "error",
136
+ location: {
137
+ line: 1,
138
+ column: 14,
139
+ fileText: "name = 'fail\"",
140
+ file: undefined,
141
+ lineText: "name = 'fail\"",
142
+ },
143
+ notes: [],
144
+ });
145
+ }
146
+ });
147
+
148
+ it("should fail to parse toml with invalid header", () => {
149
+ try {
150
+ parseTOML(`\n[name`, "config.toml");
151
+ fail("parseTOML did not throw");
152
+ } catch (err) {
153
+ expect({ ...(err as Error) }).toStrictEqual({
154
+ name: "ParseError",
155
+ text: "Key ended without value",
156
+ kind: "error",
157
+ location: {
158
+ line: 2,
159
+ column: 5,
160
+ lineText: "[name",
161
+ file: "config.toml",
162
+ fileText: "\n[name",
163
+ },
164
+ notes: [],
165
+ });
166
+ }
167
+ });
168
+ });
169
+
170
+ describe("parseJSON", () => {
171
+ it("should parse json that is empty", () => {
172
+ expect(parseJSON("{}")).toStrictEqual({});
173
+ });
174
+
175
+ it("should parse json with basic values", () => {
176
+ expect(
177
+ parseJSON(`
178
+ {
179
+ "name" : "basic",
180
+ "version": 1
181
+ }`)
182
+ ).toStrictEqual({
183
+ name: "basic",
184
+ version: 1,
185
+ });
186
+ });
187
+
188
+ it("should parse json with complex values", () => {
189
+ expect(
190
+ parseJSON(
191
+ `{
192
+ "name":"complex",
193
+ "spec":{
194
+ "uptime":[1,2.5,3],
195
+ "ok":true
196
+ }
197
+ }`
198
+ )
199
+ ).toStrictEqual({
200
+ name: "complex",
201
+ spec: {
202
+ uptime: [1, 2.5, 3],
203
+ ok: true,
204
+ },
205
+ });
206
+ });
207
+
208
+ it("should fail to parse json with invalid string", () => {
209
+ try {
210
+ parseJSON(`\n{\n"version" "1\n}\n`);
211
+ fail("parseJSON did not throw");
212
+ } catch (err) {
213
+ expect({ ...(err as Error) }).toStrictEqual({
214
+ name: "ParseError",
215
+ text: "Unexpected string",
216
+ kind: "error",
217
+ location: {
218
+ line: 3,
219
+ column: 9,
220
+ lineText: '"version" "1',
221
+ file: undefined,
222
+ fileText: `\n{\n"version" "1\n}\n`,
223
+ },
224
+ notes: [],
225
+ });
226
+ }
227
+ });
228
+
229
+ it("should fail to parse json with invalid number", () => {
230
+ const file = "config.json",
231
+ fileText = `{\n\t"a":{\n\t\t"b":{\n\t\t\t"c":[012345]\n}\n}\n}`;
232
+ try {
233
+ parseJSON(fileText, file);
234
+ fail("parseJSON did not throw");
235
+ } catch (err) {
236
+ expect({ ...(err as Error) }).toStrictEqual({
237
+ name: "ParseError",
238
+ text: "Unexpected number",
239
+ kind: "error",
240
+ location: {
241
+ file,
242
+ fileText,
243
+ line: 4,
244
+ column: 8,
245
+ lineText: `\t\t\t"c":[012345]`,
246
+ },
247
+ notes: [],
248
+ });
249
+ }
250
+ });
251
+ });
252
+
253
+ describe("indexLocation", () => {
254
+ it("should calculate location from one-line input", () => {
255
+ const fileText = "";
256
+ expect(indexLocation({ fileText }, 1)).toStrictEqual({
257
+ fileText,
258
+ line: 1,
259
+ column: 0,
260
+ lineText: "",
261
+ });
262
+ });
263
+
264
+ it("should calculate location from multi-line input", () => {
265
+ const file = "package.json",
266
+ fileText = `\n{\n\t"hello":"world"\n}\n`;
267
+ expect(indexLocation({ file, fileText }, 11)).toStrictEqual({
268
+ file,
269
+ fileText,
270
+ line: 3,
271
+ column: 7,
272
+ lineText: `\t"hello":"world"`,
273
+ });
274
+ });
275
+
276
+ it("should calculate location when index is out of bounds", () => {
277
+ const fileText = `\n\n\n\n`;
278
+ expect(indexLocation({ fileText }, 10)).toStrictEqual({
279
+ fileText,
280
+ line: 5,
281
+ column: 0,
282
+ lineText: undefined,
283
+ });
284
+ });
285
+ });
286
+
287
+ describe("searchLocation", () => {
288
+ it("should calculate location from one-line match", () => {
289
+ const file = "config.toml",
290
+ fileText = `name = 'coolthing'`;
291
+ expect(searchLocation({ file, fileText }, "coolthing")).toStrictEqual({
292
+ file,
293
+ fileText,
294
+ line: 1,
295
+ column: 8,
296
+ length: 9,
297
+ lineText: `name = 'coolthing'`,
298
+ });
299
+ });
300
+
301
+ it("should calculate location from multi-line match", () => {
302
+ const fileText = `\n{"versions":[\n\t"1.2.3",\n\t"1.2.4",\n\t"1.2.5"\n]}\n`;
303
+ expect(searchLocation({ fileText }, "1.2.4")).toStrictEqual({
304
+ fileText,
305
+ line: 4,
306
+ column: 2,
307
+ length: 5,
308
+ lineText: `\t"1.2.4",`,
309
+ });
310
+ });
311
+
312
+ it("should calculate location from no match", () => {
313
+ const fileText = `\n{}\n`;
314
+ expect(searchLocation({ fileText }, "apple")).toStrictEqual({
315
+ fileText,
316
+ line: 3,
317
+ column: 0,
318
+ length: undefined,
319
+ lineText: undefined,
320
+ });
321
+ });
322
+ });