@sentry/wizard 3.12.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 +13 -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 +6 -4
- package/dist/src/android/android-wizard.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 +3 -13
- package/dist/src/sourcemaps/tools/vite.js.map +1 -1
- package/dist/src/sourcemaps/tools/webpack.js +3 -13
- package/dist/src/sourcemaps/tools/webpack.js.map +1 -1
- package/dist/src/sveltekit/sdk-setup.js +122 -48
- 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 +70 -0
- package/dist/src/utils/ast-utils.js +152 -1
- package/dist/src/utils/ast-utils.js.map +1 -1
- package/dist/src/utils/clack-utils.d.ts +38 -6
- package/dist/src/utils/clack-utils.js +57 -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/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/utils/ast-utils.test.js +157 -26
- package/dist/test/utils/ast-utils.test.js.map +1 -1
- package/lib/Steps/ChooseIntegration.ts +1 -0
- package/package.json +1 -1
- package/src/android/android-wizard.ts +8 -5
- 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 +15 -39
- package/src/sourcemaps/tools/webpack.ts +16 -39
- package/src/sveltekit/sdk-setup.ts +109 -37
- package/src/sveltekit/sveltekit-wizard.ts +93 -25
- package/src/sveltekit/utils.ts +50 -0
- package/src/utils/ast-utils.ts +180 -0
- package/src/utils/clack-utils.ts +68 -49
- package/src/utils/package-manager.ts +12 -6
- package/test/sourcemaps/tools/sentry-cli.test.ts +51 -0
- package/test/sourcemaps/tools/tsc.test.ts +181 -0
- package/test/utils/ast-utils.test.ts +233 -32
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
|
|
3
|
+
import { addSentryCommandToBuildCommand } from '../../../src/sourcemaps/tools/sentry-cli';
|
|
4
|
+
|
|
5
|
+
import * as packageManagerHelpers from '../../../src/utils/package-manager';
|
|
6
|
+
|
|
7
|
+
const writeFileSpy = jest
|
|
8
|
+
.spyOn(fs.promises, 'writeFile')
|
|
9
|
+
.mockImplementation(() => Promise.resolve());
|
|
10
|
+
|
|
11
|
+
jest.mock('@clack/prompts', () => {
|
|
12
|
+
return {
|
|
13
|
+
log: {
|
|
14
|
+
info: jest.fn(),
|
|
15
|
+
success: jest.fn(),
|
|
16
|
+
},
|
|
17
|
+
confirm: jest.fn().mockResolvedValue(true),
|
|
18
|
+
isCancel: jest.fn().mockReturnValue(false),
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('addSentryCommandToBuildCommand', () => {
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
jest.clearAllMocks();
|
|
25
|
+
});
|
|
26
|
+
it.each([
|
|
27
|
+
[
|
|
28
|
+
packageManagerHelpers.NPM,
|
|
29
|
+
packageManagerHelpers.PNPM,
|
|
30
|
+
packageManagerHelpers.YARN,
|
|
31
|
+
packageManagerHelpers.BUN,
|
|
32
|
+
],
|
|
33
|
+
])('adds the cli command to the script command (%s)', async (_, pacMan) => {
|
|
34
|
+
jest
|
|
35
|
+
.spyOn(packageManagerHelpers, 'detectPackageManger')
|
|
36
|
+
.mockReturnValue(pacMan);
|
|
37
|
+
const packageJson = {
|
|
38
|
+
scripts: {
|
|
39
|
+
build: 'tsc',
|
|
40
|
+
},
|
|
41
|
+
version: '1.0.0',
|
|
42
|
+
};
|
|
43
|
+
await addSentryCommandToBuildCommand(packageJson);
|
|
44
|
+
expect(writeFileSpy).toHaveBeenCalledWith(
|
|
45
|
+
expect.stringContaining('package.json'),
|
|
46
|
+
expect.stringContaining(
|
|
47
|
+
`tsc && ${pacMan.runScriptCommand} sentry:sourcemaps`,
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { enableSourcemaps } from '../../../src/sourcemaps/tools/tsc';
|
|
3
|
+
|
|
4
|
+
function updateFileContent(content: string): void {
|
|
5
|
+
fileContent = content;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let fileContent = '';
|
|
9
|
+
|
|
10
|
+
jest.mock('@clack/prompts', () => {
|
|
11
|
+
return {
|
|
12
|
+
log: {
|
|
13
|
+
info: jest.fn(),
|
|
14
|
+
success: jest.fn(),
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
jest
|
|
20
|
+
.spyOn(fs.promises, 'readFile')
|
|
21
|
+
.mockImplementation(() => Promise.resolve(fileContent));
|
|
22
|
+
|
|
23
|
+
const writeFileSpy = jest
|
|
24
|
+
.spyOn(fs.promises, 'writeFile')
|
|
25
|
+
.mockImplementation(() => Promise.resolve(void 0));
|
|
26
|
+
|
|
27
|
+
describe('enableSourcemaps', () => {
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
fileContent = '';
|
|
30
|
+
jest.clearAllMocks();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it.each([
|
|
34
|
+
[
|
|
35
|
+
'no sourcemaps options',
|
|
36
|
+
`
|
|
37
|
+
/**
|
|
38
|
+
* My TS config with comments
|
|
39
|
+
*/
|
|
40
|
+
{
|
|
41
|
+
"extends": "./tsconfig.build.json",
|
|
42
|
+
|
|
43
|
+
"compilerOptions": {
|
|
44
|
+
// line comment which should stay
|
|
45
|
+
"moduleResolution": "node16",
|
|
46
|
+
"outDir": "dist" // another inline comment
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
"include": [
|
|
50
|
+
"src/**/*",
|
|
51
|
+
"test/**/*"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
`,
|
|
55
|
+
`
|
|
56
|
+
/**
|
|
57
|
+
* My TS config with comments
|
|
58
|
+
*/
|
|
59
|
+
{
|
|
60
|
+
"extends": "./tsconfig.build.json",
|
|
61
|
+
|
|
62
|
+
"compilerOptions": {
|
|
63
|
+
// line comment which should stay
|
|
64
|
+
"moduleResolution": "node16",
|
|
65
|
+
|
|
66
|
+
// another inline comment
|
|
67
|
+
"outDir": "dist",
|
|
68
|
+
|
|
69
|
+
"sourceMap": true,
|
|
70
|
+
"inlineSources": true,
|
|
71
|
+
|
|
72
|
+
// Set \`sourceRoot\` to "/" to strip the build path prefix
|
|
73
|
+
// from generated source code references.
|
|
74
|
+
// This improves issue grouping in Sentry.
|
|
75
|
+
"sourceRoot": "/"
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
"include": [
|
|
79
|
+
"src/**/*",
|
|
80
|
+
"test/**/*"
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
`,
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
'a few sourcemaps options',
|
|
87
|
+
`
|
|
88
|
+
/**
|
|
89
|
+
* My TS config with comments
|
|
90
|
+
*/
|
|
91
|
+
{
|
|
92
|
+
"extends": "./tsconfig.build.json",
|
|
93
|
+
|
|
94
|
+
"compilerOptions": {
|
|
95
|
+
// line comment which should stay
|
|
96
|
+
"moduleResolution": "node16",
|
|
97
|
+
"outDir": "dist", // another inline comment
|
|
98
|
+
"sourceMap": false,
|
|
99
|
+
"sourceRoot": "/src"
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
"include": [
|
|
103
|
+
"src/**/*",
|
|
104
|
+
"test/**/*"
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
`,
|
|
108
|
+
`
|
|
109
|
+
/**
|
|
110
|
+
* My TS config with comments
|
|
111
|
+
*/
|
|
112
|
+
{
|
|
113
|
+
"extends": "./tsconfig.build.json",
|
|
114
|
+
|
|
115
|
+
"compilerOptions": {
|
|
116
|
+
// line comment which should stay
|
|
117
|
+
"moduleResolution": "node16",
|
|
118
|
+
|
|
119
|
+
// another inline comment
|
|
120
|
+
"outDir": "dist",
|
|
121
|
+
|
|
122
|
+
"sourceMap": true,
|
|
123
|
+
|
|
124
|
+
// Set \`sourceRoot\` to "/" to strip the build path prefix
|
|
125
|
+
// from generated source code references.
|
|
126
|
+
// This improves issue grouping in Sentry.
|
|
127
|
+
"sourceRoot": "/",
|
|
128
|
+
|
|
129
|
+
"inlineSources": true
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
"include": [
|
|
133
|
+
"src/**/*",
|
|
134
|
+
"test/**/*"
|
|
135
|
+
]
|
|
136
|
+
}
|
|
137
|
+
`,
|
|
138
|
+
],
|
|
139
|
+
[
|
|
140
|
+
'no compiler options',
|
|
141
|
+
`
|
|
142
|
+
{
|
|
143
|
+
"include": [
|
|
144
|
+
"src/**/*",
|
|
145
|
+
"test/**/*"
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
`,
|
|
149
|
+
`
|
|
150
|
+
{
|
|
151
|
+
"include": [
|
|
152
|
+
"src/**/*",
|
|
153
|
+
"test/**/*"
|
|
154
|
+
],
|
|
155
|
+
|
|
156
|
+
"compilerOptions": {
|
|
157
|
+
"sourceMap": true,
|
|
158
|
+
"inlineSources": true,
|
|
159
|
+
|
|
160
|
+
// Set \`sourceRoot\` to "/" to strip the build path prefix
|
|
161
|
+
// from generated source code references.
|
|
162
|
+
// This improves issue grouping in Sentry.
|
|
163
|
+
"sourceRoot": "/"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
`,
|
|
167
|
+
],
|
|
168
|
+
])(
|
|
169
|
+
'adds the plugin and enables source maps generation (%s)',
|
|
170
|
+
async (_, originalCode, expectedCode) => {
|
|
171
|
+
updateFileContent(originalCode);
|
|
172
|
+
|
|
173
|
+
const addedCode = await enableSourcemaps('');
|
|
174
|
+
|
|
175
|
+
expect(writeFileSpy).toHaveBeenCalledTimes(1);
|
|
176
|
+
const [[, fileContent]] = writeFileSpy.mock.calls;
|
|
177
|
+
expect(fileContent).toBe(expectedCode);
|
|
178
|
+
expect(addedCode).toBe(true);
|
|
179
|
+
},
|
|
180
|
+
);
|
|
181
|
+
});
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getObjectProperty,
|
|
3
|
+
getOrSetObjectProperty,
|
|
4
|
+
hasSentryContent,
|
|
5
|
+
parseJsonC,
|
|
6
|
+
printJsonC,
|
|
7
|
+
setOrUpdateObjectProperty,
|
|
8
|
+
} from '../../src/utils/ast-utils';
|
|
2
9
|
|
|
3
10
|
import * as recast from 'recast';
|
|
11
|
+
const b = recast.types.builders;
|
|
4
12
|
|
|
5
|
-
describe('
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
`
|
|
13
|
+
describe('hasSentryContent', () => {
|
|
14
|
+
it.each([
|
|
15
|
+
`
|
|
9
16
|
const { sentryVitePlugin } = require("@sentry/vite-plugin");
|
|
10
17
|
const somethingelse = require('gs');
|
|
11
18
|
`,
|
|
12
|
-
|
|
19
|
+
`
|
|
13
20
|
import { sentryVitePlugin } from "@sentry/vite-plugin";
|
|
14
21
|
import * as somethingelse from 'gs';
|
|
15
22
|
|
|
@@ -17,47 +24,241 @@ describe('AST utils', () => {
|
|
|
17
24
|
plugins: [sentryVitePlugin()]
|
|
18
25
|
}
|
|
19
26
|
`,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
+
);
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
it.each([
|
|
39
|
+
`const whatever = require('something')`,
|
|
40
|
+
`// const {sentryWebpackPlugin} = require('@sentry/webpack-plugin')`,
|
|
41
|
+
`const {sAntryWebpackPlugin} = require('webpack-plugin-@sentry')`,
|
|
42
|
+
`
|
|
36
43
|
import * as somethingelse from 'gs';
|
|
37
44
|
export default {
|
|
38
45
|
plugins: []
|
|
39
46
|
}
|
|
40
47
|
`,
|
|
41
|
-
|
|
48
|
+
`import * as somethingelse from 'gs';
|
|
42
49
|
// import { sentryVitePlugin } from "@sentry/vite-plugin"
|
|
43
50
|
export default {
|
|
44
51
|
plugins: []
|
|
45
52
|
}
|
|
46
53
|
`,
|
|
47
|
-
|
|
54
|
+
`import * as thirdPartyVitePlugin from "vite-plugin-@sentry"
|
|
48
55
|
export default {
|
|
49
56
|
plugins: [thirdPartyVitePlugin()]
|
|
50
57
|
}
|
|
51
58
|
`,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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'),
|
|
61
103
|
);
|
|
62
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'),
|
|
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();
|
|
263
|
+
});
|
|
63
264
|
});
|