@sentry/wizard 3.11.0 → 3.13.0
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.
- package/CHANGELOG.md +20 -0
- package/dist/lib/Steps/ChooseIntegration.js +1 -0
- package/dist/lib/Steps/ChooseIntegration.js.map +1 -1
- package/dist/package.json +1 -1
- package/dist/src/android/android-wizard.js +14 -4
- package/dist/src/android/android-wizard.js.map +1 -1
- package/dist/src/android/code-tools.d.ts +8 -0
- package/dist/src/android/code-tools.js +20 -8
- package/dist/src/android/code-tools.js.map +1 -1
- package/dist/src/android/gradle.js +6 -1
- package/dist/src/android/gradle.js.map +1 -1
- package/dist/src/nextjs/nextjs-wizard.js +5 -2
- package/dist/src/nextjs/nextjs-wizard.js.map +1 -1
- package/dist/src/nextjs/templates.d.ts +1 -1
- package/dist/src/nextjs/templates.js +2 -2
- package/dist/src/nextjs/templates.js.map +1 -1
- package/dist/src/remix/remix-wizard.js +8 -4
- package/dist/src/remix/remix-wizard.js.map +1 -1
- package/dist/src/remix/sdk-setup.d.ts +5 -1
- package/dist/src/remix/sdk-setup.js +3 -2
- package/dist/src/remix/sdk-setup.js.map +1 -1
- package/dist/src/sourcemaps/tools/sentry-cli.d.ts +9 -0
- package/dist/src/sourcemaps/tools/sentry-cli.js +26 -22
- package/dist/src/sourcemaps/tools/sentry-cli.js.map +1 -1
- package/dist/src/sourcemaps/tools/tsc.d.ts +6 -0
- package/dist/src/sourcemaps/tools/tsc.js +98 -17
- package/dist/src/sourcemaps/tools/tsc.js.map +1 -1
- package/dist/src/sourcemaps/tools/vite.js +39 -124
- package/dist/src/sourcemaps/tools/vite.js.map +1 -1
- package/dist/src/sourcemaps/tools/webpack.d.ts +6 -1
- package/dist/src/sourcemaps/tools/webpack.js +280 -25
- package/dist/src/sourcemaps/tools/webpack.js.map +1 -1
- package/dist/src/sveltekit/sdk-setup.js +123 -49
- package/dist/src/sveltekit/sdk-setup.js.map +1 -1
- package/dist/src/sveltekit/sveltekit-wizard.d.ts +1 -0
- package/dist/src/sveltekit/sveltekit-wizard.js +119 -44
- package/dist/src/sveltekit/sveltekit-wizard.js.map +1 -1
- package/dist/src/sveltekit/utils.d.ts +2 -0
- package/dist/src/sveltekit/utils.js +48 -0
- package/dist/src/sveltekit/utils.js.map +1 -0
- package/dist/src/utils/ast-utils.d.ts +77 -3
- package/dist/src/utils/ast-utils.js +172 -6
- package/dist/src/utils/ast-utils.js.map +1 -1
- package/dist/src/utils/clack-utils.d.ts +85 -1
- package/dist/src/utils/clack-utils.js +214 -51
- package/dist/src/utils/clack-utils.js.map +1 -1
- package/dist/src/utils/package-manager.d.ts +5 -0
- package/dist/src/utils/package-manager.js +11 -7
- package/dist/src/utils/package-manager.js.map +1 -1
- package/dist/test/android/code-tools.test.d.ts +1 -0
- package/dist/test/android/code-tools.test.js +34 -0
- package/dist/test/android/code-tools.test.js.map +1 -0
- package/dist/test/sourcemaps/tools/sentry-cli.test.d.ts +1 -0
- package/dist/test/sourcemaps/tools/sentry-cli.test.js +112 -0
- package/dist/test/sourcemaps/tools/sentry-cli.test.js.map +1 -0
- package/dist/test/sourcemaps/tools/tsc.test.d.ts +1 -0
- package/dist/test/sourcemaps/tools/tsc.test.js +121 -0
- package/dist/test/sourcemaps/tools/tsc.test.js.map +1 -0
- package/dist/test/sourcemaps/tools/webpack.test.d.ts +1 -0
- package/dist/test/sourcemaps/tools/webpack.test.js +179 -0
- package/dist/test/sourcemaps/tools/webpack.test.js.map +1 -0
- package/dist/test/utils/ast-utils.test.js +181 -15
- package/dist/test/utils/ast-utils.test.js.map +1 -1
- package/dist/test/utils/clack-utils.test.d.ts +1 -0
- package/dist/test/utils/clack-utils.test.js +200 -0
- package/dist/test/utils/clack-utils.test.js.map +1 -0
- package/lib/Steps/ChooseIntegration.ts +1 -0
- package/package.json +1 -1
- package/src/android/android-wizard.ts +16 -5
- package/src/android/code-tools.ts +21 -7
- package/src/android/gradle.ts +6 -1
- package/src/nextjs/nextjs-wizard.ts +15 -3
- package/src/nextjs/templates.ts +3 -2
- package/src/remix/remix-wizard.ts +8 -11
- package/src/remix/sdk-setup.ts +8 -2
- package/src/sourcemaps/tools/sentry-cli.ts +16 -9
- package/src/sourcemaps/tools/tsc.ts +133 -28
- package/src/sourcemaps/tools/vite.ts +37 -127
- package/src/sourcemaps/tools/webpack.ts +343 -27
- package/src/sveltekit/sdk-setup.ts +115 -39
- package/src/sveltekit/sveltekit-wizard.ts +93 -25
- package/src/sveltekit/utils.ts +50 -0
- package/src/utils/ast-utils.ts +203 -7
- package/src/utils/clack-utils.ts +211 -44
- package/src/utils/package-manager.ts +12 -6
- package/test/android/code-tools.test.ts +49 -0
- package/test/sourcemaps/tools/sentry-cli.test.ts +51 -0
- package/test/sourcemaps/tools/tsc.test.ts +181 -0
- package/test/sourcemaps/tools/webpack.test.ts +303 -0
- package/test/utils/ast-utils.test.ts +240 -20
- package/test/utils/clack-utils.test.ts +142 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
|
|
3
|
+
import { modifyWebpackConfig } from '../../../src/sourcemaps/tools/webpack';
|
|
4
|
+
|
|
5
|
+
function updateFileContent(content: string): void {
|
|
6
|
+
fileContent = content;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let fileContent = '';
|
|
10
|
+
|
|
11
|
+
jest.mock('@clack/prompts', () => {
|
|
12
|
+
return {
|
|
13
|
+
log: {
|
|
14
|
+
info: jest.fn(),
|
|
15
|
+
success: jest.fn(),
|
|
16
|
+
},
|
|
17
|
+
select: jest.fn().mockImplementation(() => Promise.resolve(true)),
|
|
18
|
+
isCancel: jest.fn().mockReturnValue(false),
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
jest
|
|
23
|
+
.spyOn(fs.promises, 'readFile')
|
|
24
|
+
.mockImplementation(() => Promise.resolve(fileContent));
|
|
25
|
+
|
|
26
|
+
const writeFileSpy = jest
|
|
27
|
+
.spyOn(fs.promises, 'writeFile')
|
|
28
|
+
.mockImplementation(() => Promise.resolve(void 0));
|
|
29
|
+
|
|
30
|
+
const noSourcemapNoPluginsPojo = `module.exports = {
|
|
31
|
+
entry: "./src/index.js",
|
|
32
|
+
output: {
|
|
33
|
+
filename: "main.js",
|
|
34
|
+
path: path.resolve(__dirname, "build"),
|
|
35
|
+
},
|
|
36
|
+
};`;
|
|
37
|
+
|
|
38
|
+
const noSourcemapNoPluginsPojoResult = `const {
|
|
39
|
+
sentryWebpackPlugin
|
|
40
|
+
} = require("@sentry/webpack-plugin");
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
entry: "./src/index.js",
|
|
44
|
+
|
|
45
|
+
output: {
|
|
46
|
+
filename: "main.js",
|
|
47
|
+
path: path.resolve(__dirname, "build"),
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
devtool: "source-map",
|
|
51
|
+
|
|
52
|
+
plugins: [sentryWebpackPlugin({
|
|
53
|
+
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
54
|
+
org: "my-org",
|
|
55
|
+
project: "my-project"
|
|
56
|
+
})]
|
|
57
|
+
};`;
|
|
58
|
+
|
|
59
|
+
const noSourcemapsNoPluginsId = `const config = {
|
|
60
|
+
entry: "./src/index.js",
|
|
61
|
+
|
|
62
|
+
output: {
|
|
63
|
+
filename: "main.js",
|
|
64
|
+
path: path.resolve(__dirname, "build"),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
module.exports = config;`;
|
|
69
|
+
|
|
70
|
+
const noSourcemapsNoPluginsIdResult = `const {
|
|
71
|
+
sentryWebpackPlugin
|
|
72
|
+
} = require("@sentry/webpack-plugin");
|
|
73
|
+
|
|
74
|
+
const config = {
|
|
75
|
+
entry: "./src/index.js",
|
|
76
|
+
|
|
77
|
+
output: {
|
|
78
|
+
filename: "main.js",
|
|
79
|
+
path: path.resolve(__dirname, "build"),
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
devtool: "source-map",
|
|
83
|
+
|
|
84
|
+
plugins: [sentryWebpackPlugin({
|
|
85
|
+
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
86
|
+
org: "my-org",
|
|
87
|
+
project: "my-project"
|
|
88
|
+
})]
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
module.exports = config;`;
|
|
92
|
+
|
|
93
|
+
const hiddenSourcemapNoPluginsId = `const config = {
|
|
94
|
+
entry: "./src/index.js",
|
|
95
|
+
|
|
96
|
+
output: {
|
|
97
|
+
filename: "main.js",
|
|
98
|
+
path: path.resolve(__dirname, "build"),
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
devtool: "hidden-cheap-source-map",
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
module.exports = config;
|
|
105
|
+
`;
|
|
106
|
+
const hiddenSourcemapNoPluginsIdResult = `const {
|
|
107
|
+
sentryWebpackPlugin
|
|
108
|
+
} = require("@sentry/webpack-plugin");
|
|
109
|
+
|
|
110
|
+
const config = {
|
|
111
|
+
entry: "./src/index.js",
|
|
112
|
+
|
|
113
|
+
output: {
|
|
114
|
+
filename: "main.js",
|
|
115
|
+
path: path.resolve(__dirname, "build"),
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
devtool: "hidden-source-map",
|
|
119
|
+
|
|
120
|
+
plugins: [sentryWebpackPlugin({
|
|
121
|
+
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
122
|
+
org: "my-org",
|
|
123
|
+
project: "my-project"
|
|
124
|
+
})]
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
module.exports = config;`;
|
|
128
|
+
|
|
129
|
+
const arbitrarySourcemapNoPluginsId = `
|
|
130
|
+
const config = {
|
|
131
|
+
entry: "./src/index.js",
|
|
132
|
+
|
|
133
|
+
output: {
|
|
134
|
+
filename: "main.js",
|
|
135
|
+
path: path.resolve(__dirname, "build"),
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
devtool: getSourcemapSetting(),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
module.exports = config;
|
|
142
|
+
`;
|
|
143
|
+
const arbitrarySourcemapNoPluginsIdResult = `const {
|
|
144
|
+
sentryWebpackPlugin
|
|
145
|
+
} = require("@sentry/webpack-plugin");
|
|
146
|
+
|
|
147
|
+
const config = {
|
|
148
|
+
entry: "./src/index.js",
|
|
149
|
+
|
|
150
|
+
output: {
|
|
151
|
+
filename: "main.js",
|
|
152
|
+
path: path.resolve(__dirname, "build"),
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
devtool: "source-map",
|
|
156
|
+
|
|
157
|
+
plugins: [sentryWebpackPlugin({
|
|
158
|
+
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
159
|
+
org: "my-org",
|
|
160
|
+
project: "my-project"
|
|
161
|
+
})]
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
module.exports = config;`;
|
|
165
|
+
|
|
166
|
+
const noSourcemapUndefinedPluginsPojo = `module.exports = {
|
|
167
|
+
entry: "./src/index.js",
|
|
168
|
+
plugins: undefined,
|
|
169
|
+
output: {
|
|
170
|
+
filename: "main.js",
|
|
171
|
+
path: path.resolve(__dirname, "build"),
|
|
172
|
+
},
|
|
173
|
+
};`;
|
|
174
|
+
|
|
175
|
+
const noSourcemapUndefinedPluginsPojoResult = `const {
|
|
176
|
+
sentryWebpackPlugin
|
|
177
|
+
} = require("@sentry/webpack-plugin");
|
|
178
|
+
|
|
179
|
+
module.exports = {
|
|
180
|
+
entry: "./src/index.js",
|
|
181
|
+
|
|
182
|
+
plugins: [sentryWebpackPlugin({
|
|
183
|
+
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
184
|
+
org: "my-org",
|
|
185
|
+
project: "my-project"
|
|
186
|
+
})],
|
|
187
|
+
|
|
188
|
+
output: {
|
|
189
|
+
filename: "main.js",
|
|
190
|
+
path: path.resolve(__dirname, "build"),
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
devtool: "source-map"
|
|
194
|
+
};`;
|
|
195
|
+
|
|
196
|
+
const noSourcemapPluginsPojo = `module.exports = {
|
|
197
|
+
entry: "./src/index.js",
|
|
198
|
+
plugins: [
|
|
199
|
+
new HtmlWebpackPlugin(),
|
|
200
|
+
new MiniCssExtractPlugin(),
|
|
201
|
+
],
|
|
202
|
+
output: {
|
|
203
|
+
filename: "main.js",
|
|
204
|
+
path: path.resolve(__dirname, "build"),
|
|
205
|
+
},
|
|
206
|
+
};`;
|
|
207
|
+
|
|
208
|
+
const noSourcemapPluginsPojoResult = `const {
|
|
209
|
+
sentryWebpackPlugin
|
|
210
|
+
} = require("@sentry/webpack-plugin");
|
|
211
|
+
|
|
212
|
+
module.exports = {
|
|
213
|
+
entry: "./src/index.js",
|
|
214
|
+
|
|
215
|
+
plugins: [new HtmlWebpackPlugin(), new MiniCssExtractPlugin(), sentryWebpackPlugin({
|
|
216
|
+
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
217
|
+
org: "my-org",
|
|
218
|
+
project: "my-project"
|
|
219
|
+
})],
|
|
220
|
+
|
|
221
|
+
output: {
|
|
222
|
+
filename: "main.js",
|
|
223
|
+
path: path.resolve(__dirname, "build"),
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
devtool: "source-map"
|
|
227
|
+
};`;
|
|
228
|
+
|
|
229
|
+
describe('modifyWebpackConfig', () => {
|
|
230
|
+
afterEach(() => {
|
|
231
|
+
fileContent = '';
|
|
232
|
+
jest.clearAllMocks();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it.each([
|
|
236
|
+
[
|
|
237
|
+
'no sourcemap option, no plugins, object',
|
|
238
|
+
noSourcemapNoPluginsPojo,
|
|
239
|
+
noSourcemapNoPluginsPojoResult,
|
|
240
|
+
],
|
|
241
|
+
[
|
|
242
|
+
'no sourcemap option, no plugins, identifier',
|
|
243
|
+
noSourcemapsNoPluginsId,
|
|
244
|
+
noSourcemapsNoPluginsIdResult,
|
|
245
|
+
],
|
|
246
|
+
[
|
|
247
|
+
'hidden sourcemap option, no plugins, identifier',
|
|
248
|
+
hiddenSourcemapNoPluginsId,
|
|
249
|
+
hiddenSourcemapNoPluginsIdResult,
|
|
250
|
+
],
|
|
251
|
+
[
|
|
252
|
+
'arbitrary sourcemap option, no plugins, identifier',
|
|
253
|
+
arbitrarySourcemapNoPluginsId,
|
|
254
|
+
arbitrarySourcemapNoPluginsIdResult,
|
|
255
|
+
],
|
|
256
|
+
[
|
|
257
|
+
'no sourcemap option, plugins, object',
|
|
258
|
+
noSourcemapUndefinedPluginsPojo,
|
|
259
|
+
noSourcemapUndefinedPluginsPojoResult,
|
|
260
|
+
],
|
|
261
|
+
[
|
|
262
|
+
'no sourcemap option, plugins, object',
|
|
263
|
+
noSourcemapPluginsPojo,
|
|
264
|
+
noSourcemapPluginsPojoResult,
|
|
265
|
+
],
|
|
266
|
+
])(
|
|
267
|
+
'adds plugin and source maps emission to the webpack config (%s)',
|
|
268
|
+
async (_, originalCode, expectedCode) => {
|
|
269
|
+
updateFileContent(originalCode);
|
|
270
|
+
|
|
271
|
+
// updateFileContent(originalCode);
|
|
272
|
+
const addedCode = await modifyWebpackConfig('', {
|
|
273
|
+
authToken: '',
|
|
274
|
+
orgSlug: 'my-org',
|
|
275
|
+
projectSlug: 'my-project',
|
|
276
|
+
selfHosted: false,
|
|
277
|
+
url: 'https://sentry.io/',
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
expect(writeFileSpy).toHaveBeenCalledTimes(1);
|
|
281
|
+
const [[, fileContent]] = writeFileSpy.mock.calls;
|
|
282
|
+
expect(fileContent).toBe(expectedCode);
|
|
283
|
+
expect(addedCode).toBe(true);
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
it('adds the url parameter to the webpack plugin options if self-hosted', async () => {
|
|
288
|
+
updateFileContent(noSourcemapNoPluginsPojo);
|
|
289
|
+
|
|
290
|
+
const addedCode = await modifyWebpackConfig('', {
|
|
291
|
+
authToken: '',
|
|
292
|
+
orgSlug: 'my-org',
|
|
293
|
+
projectSlug: 'my-project',
|
|
294
|
+
selfHosted: true,
|
|
295
|
+
url: 'https://santry.io/',
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
expect(writeFileSpy).toHaveBeenCalledTimes(1);
|
|
299
|
+
const [[, fileContent]] = writeFileSpy.mock.calls;
|
|
300
|
+
expect(fileContent).toContain('url: "https://santry.io/"');
|
|
301
|
+
expect(addedCode).toBe(true);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
@@ -1,44 +1,264 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import {
|
|
2
|
+
getObjectProperty,
|
|
3
|
+
getOrSetObjectProperty,
|
|
4
|
+
hasSentryContent,
|
|
5
|
+
parseJsonC,
|
|
6
|
+
printJsonC,
|
|
7
|
+
setOrUpdateObjectProperty,
|
|
8
|
+
} from '../../src/utils/ast-utils';
|
|
9
|
+
|
|
10
|
+
import * as recast from 'recast';
|
|
11
|
+
const b = recast.types.builders;
|
|
12
|
+
|
|
13
|
+
describe('hasSentryContent', () => {
|
|
14
|
+
it.each([
|
|
15
|
+
`
|
|
16
|
+
const { sentryVitePlugin } = require("@sentry/vite-plugin");
|
|
17
|
+
const somethingelse = require('gs');
|
|
18
|
+
`,
|
|
19
|
+
`
|
|
9
20
|
import { sentryVitePlugin } from "@sentry/vite-plugin";
|
|
10
21
|
import * as somethingelse from 'gs';
|
|
11
22
|
|
|
12
23
|
export default {
|
|
13
24
|
plugins: [sentryVitePlugin()]
|
|
14
25
|
}
|
|
15
|
-
|
|
26
|
+
`,
|
|
27
|
+
])(
|
|
28
|
+
"returns true if a require('@sentry/') call was found in the parsed module",
|
|
29
|
+
(code) => {
|
|
30
|
+
// recast.parse returns a Program node (or fails) but it's badly typed as any
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
32
|
+
const program = recast.parse(code)
|
|
33
|
+
.program as recast.types.namedTypes.Program;
|
|
34
|
+
expect(hasSentryContent(program)).toBe(true);
|
|
35
|
+
},
|
|
36
|
+
);
|
|
16
37
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
38
|
+
it.each([
|
|
39
|
+
`const whatever = require('something')`,
|
|
40
|
+
`// const {sentryWebpackPlugin} = require('@sentry/webpack-plugin')`,
|
|
41
|
+
`const {sAntryWebpackPlugin} = require('webpack-plugin-@sentry')`,
|
|
42
|
+
`
|
|
21
43
|
import * as somethingelse from 'gs';
|
|
22
44
|
export default {
|
|
23
45
|
plugins: []
|
|
24
46
|
}
|
|
25
47
|
`,
|
|
26
|
-
|
|
48
|
+
`import * as somethingelse from 'gs';
|
|
27
49
|
// import { sentryVitePlugin } from "@sentry/vite-plugin"
|
|
28
50
|
export default {
|
|
29
51
|
plugins: []
|
|
30
52
|
}
|
|
31
53
|
`,
|
|
32
|
-
|
|
54
|
+
`import * as thirdPartyVitePlugin from "vite-plugin-@sentry"
|
|
33
55
|
export default {
|
|
34
56
|
plugins: [thirdPartyVitePlugin()]
|
|
35
57
|
}
|
|
36
58
|
`,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
59
|
+
])(
|
|
60
|
+
"returns false if the file doesn't contain any require('@sentry/') calls",
|
|
61
|
+
(code) => {
|
|
62
|
+
// recast.parse returns a Program node (or fails) but it's badly typed as any
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
64
|
+
const program = recast.parse(code)
|
|
65
|
+
.program as recast.types.namedTypes.Program;
|
|
66
|
+
expect(hasSentryContent(program)).toBe(false);
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('getObjectProperty', () => {
|
|
72
|
+
it.each([
|
|
73
|
+
[
|
|
74
|
+
'literal',
|
|
75
|
+
b.objectExpression([
|
|
76
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
77
|
+
b.objectProperty(
|
|
78
|
+
b.stringLiteral('needle'),
|
|
79
|
+
b.stringLiteral('haystack'),
|
|
80
|
+
),
|
|
81
|
+
]),
|
|
82
|
+
],
|
|
83
|
+
[
|
|
84
|
+
'stringLiteral',
|
|
85
|
+
b.objectExpression([
|
|
86
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
87
|
+
b.objectProperty(b.literal('needle'), b.stringLiteral('haystack')),
|
|
88
|
+
]),
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
'identifier',
|
|
92
|
+
b.objectExpression([
|
|
93
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
94
|
+
b.objectProperty(b.identifier('needle'), b.stringLiteral('haystack')),
|
|
95
|
+
]),
|
|
96
|
+
],
|
|
97
|
+
])('returns the poperty (%s) if it exists', (_, object) => {
|
|
98
|
+
const property = getObjectProperty(object, 'needle');
|
|
99
|
+
expect(property).toBeDefined();
|
|
100
|
+
// @ts-expect-error we know it's defined due to the expect above
|
|
101
|
+
expect(recast.print(property).code).toEqual(
|
|
102
|
+
expect.stringContaining('needle'),
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('returns undefined if the property does not exist', () => {
|
|
107
|
+
const object = b.objectExpression([
|
|
108
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
109
|
+
]);
|
|
110
|
+
const property = getObjectProperty(object, 'needle');
|
|
111
|
+
expect(property).toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('handles objects without simple properties', () => {
|
|
115
|
+
const object = b.objectExpression([b.spreadElement(b.identifier('foo'))]);
|
|
116
|
+
const property = getObjectProperty(object, 'needle');
|
|
117
|
+
expect(property).toBeUndefined();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('getOrSetObjectProperty', () => {
|
|
122
|
+
it('returns the property if it exists', () => {
|
|
123
|
+
const object = b.objectExpression([
|
|
124
|
+
b.objectProperty(b.identifier('needle'), b.stringLiteral('haystack')),
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
const property = getOrSetObjectProperty(
|
|
128
|
+
object,
|
|
129
|
+
'needle',
|
|
130
|
+
b.stringLiteral('nope'),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(property).toBeDefined();
|
|
134
|
+
expect(property.type).toBe('ObjectProperty');
|
|
135
|
+
// @ts-expect-error we know its type
|
|
136
|
+
expect(property.key.name).toBe('needle');
|
|
137
|
+
// @ts-expect-error we know its type
|
|
138
|
+
expect(property.value.value).toBe('haystack');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('adds the property if it does not exist', () => {
|
|
142
|
+
const object = b.objectExpression([
|
|
143
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
const property = getOrSetObjectProperty(
|
|
147
|
+
object,
|
|
148
|
+
'needle',
|
|
149
|
+
b.stringLiteral('haystack'),
|
|
42
150
|
);
|
|
151
|
+
|
|
152
|
+
expect(property).toBeDefined();
|
|
153
|
+
expect(property.type).toBe('Property');
|
|
154
|
+
// @ts-expect-error we know its type
|
|
155
|
+
expect(property.key.value).toBe('needle');
|
|
156
|
+
// @ts-expect-error we know its type
|
|
157
|
+
expect(property.value.value).toBe('haystack');
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('setOrUpdateObjectProperty', () => {
|
|
162
|
+
it('sets a new property if it does not exist yet', () => {
|
|
163
|
+
const object = b.objectExpression([
|
|
164
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
setOrUpdateObjectProperty(object, 'needle', b.stringLiteral('haystack'));
|
|
168
|
+
|
|
169
|
+
expect(getObjectProperty(object, 'needle')).toBeDefined();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('updates an existing property if it exists', () => {
|
|
173
|
+
const object = b.objectExpression([
|
|
174
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
175
|
+
b.objectProperty(b.identifier('needle'), b.stringLiteral('haystack')),
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
setOrUpdateObjectProperty(object, 'needle', b.stringLiteral('haystack2'));
|
|
179
|
+
|
|
180
|
+
const property = getObjectProperty(object, 'needle');
|
|
181
|
+
// @ts-expect-error it must be defiend, otherwise we fail anyway
|
|
182
|
+
expect(property?.value.value).toBe('haystack2');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('adds a comment to the existing property if provided', () => {
|
|
186
|
+
const object = b.objectExpression([
|
|
187
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
188
|
+
]);
|
|
189
|
+
|
|
190
|
+
setOrUpdateObjectProperty(
|
|
191
|
+
object,
|
|
192
|
+
'needle',
|
|
193
|
+
b.stringLiteral('haystack'),
|
|
194
|
+
'This is a comment',
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const property = getObjectProperty(object, 'needle');
|
|
198
|
+
expect(property?.comments).toHaveLength(1);
|
|
199
|
+
// @ts-expect-error it must be defiend, otherwise we fail anyway
|
|
200
|
+
expect(property?.comments[0].value).toBe(' This is a comment');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('adds a comment to the new property if provided', () => {
|
|
204
|
+
const object = b.objectExpression([
|
|
205
|
+
b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')),
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
setOrUpdateObjectProperty(
|
|
209
|
+
object,
|
|
210
|
+
'needle',
|
|
211
|
+
b.stringLiteral('haystack'),
|
|
212
|
+
'This is a comment',
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const property = getObjectProperty(object, 'needle');
|
|
216
|
+
expect(property?.comments).toHaveLength(1);
|
|
217
|
+
// @ts-expect-error it must be defiend, otherwise we fail anyway
|
|
218
|
+
expect(property?.comments[0].value).toBe(' This is a comment');
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe('parse and print JSON-C', () => {
|
|
223
|
+
it.each([
|
|
224
|
+
['simple JSON', "{'foo': 'bar'}"],
|
|
225
|
+
[
|
|
226
|
+
'JSON-C with inline comment',
|
|
227
|
+
`
|
|
228
|
+
{
|
|
229
|
+
"foo": "bar" // with an inline comment
|
|
230
|
+
}
|
|
231
|
+
`,
|
|
232
|
+
],
|
|
233
|
+
[
|
|
234
|
+
'JSON-C with multiple comments',
|
|
235
|
+
`
|
|
236
|
+
/*
|
|
237
|
+
* let's throw in a block comment for good measure
|
|
238
|
+
*/
|
|
239
|
+
{
|
|
240
|
+
// one line comment
|
|
241
|
+
"foo": "bar", // another inline comment
|
|
242
|
+
/* one more here */
|
|
243
|
+
"dogs": /* and here */ "awesome",
|
|
244
|
+
}
|
|
245
|
+
/* and here */
|
|
246
|
+
`,
|
|
247
|
+
],
|
|
248
|
+
])(`parses and prints JSON-C (%s)`, (_, json) => {
|
|
249
|
+
const { ast, jsonObject } = parseJsonC(json);
|
|
250
|
+
expect(ast?.type).toBe('Program');
|
|
251
|
+
expect(jsonObject).toBeDefined();
|
|
252
|
+
expect(jsonObject?.type).toBe('ObjectExpression');
|
|
253
|
+
// @ts-expect-error we know it's defined due to the expect above
|
|
254
|
+
expect(printJsonC(ast)).toEqual(json);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('returns undefined if the input is not valid JSON-C', () => {
|
|
258
|
+
const { ast, jsonObject } = parseJsonC(`{
|
|
259
|
+
"invalid": // "json"
|
|
260
|
+
}`);
|
|
261
|
+
expect(ast).toBeUndefined();
|
|
262
|
+
expect(jsonObject).toBeUndefined();
|
|
43
263
|
});
|
|
44
264
|
});
|