projen 0.65.49 → 0.65.50
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/.jsii +27 -10
- package/docs/api/API.md +8 -0
- package/lib/awscdk/auto-discover.js +5 -5
- package/lib/awscdk/awscdk-app-java.js +1 -1
- package/lib/awscdk/awscdk-app-py.js +1 -1
- package/lib/awscdk/awscdk-app-ts.js +1 -1
- package/lib/awscdk/awscdk-construct.js +2 -2
- package/lib/awscdk/awscdk-deps-java.js +1 -1
- package/lib/awscdk/awscdk-deps-js.js +1 -1
- package/lib/awscdk/awscdk-deps-py.js +1 -1
- package/lib/awscdk/awscdk-deps.js +1 -1
- package/lib/awscdk/cdk-config.js +1 -1
- package/lib/awscdk/cdk-tasks.js +1 -1
- package/lib/awscdk/integration-test.js +1 -1
- package/lib/awscdk/lambda-extension.js +1 -1
- package/lib/awscdk/lambda-function.js +2 -2
- package/lib/build/build-workflow.js +1 -1
- package/lib/cdk/auto-discover-base.js +2 -2
- package/lib/cdk/construct-lib.js +1 -1
- package/lib/cdk/integration-test-base.js +1 -1
- package/lib/cdk/jsii-docgen.js +1 -1
- package/lib/cdk/jsii-project.js +1 -1
- package/lib/cdk8s/auto-discover.js +2 -2
- package/lib/cdk8s/cdk8s-app-py.js +1 -1
- package/lib/cdk8s/cdk8s-app-ts.js +1 -1
- package/lib/cdk8s/cdk8s-construct.js +1 -1
- package/lib/cdk8s/cdk8s-deps-py.js +1 -1
- package/lib/cdk8s/cdk8s-deps.js +1 -1
- package/lib/cdk8s/integration-test.js +1 -1
- package/lib/cdktf/cdktf-construct.js +1 -1
- package/lib/circleci/circleci.js +1 -1
- package/lib/component.js +1 -1
- package/lib/dependencies.js +1 -1
- package/lib/dev-env.js +1 -1
- package/lib/docker-compose.js +2 -2
- package/lib/file.js +1 -1
- package/lib/gitattributes.js +1 -1
- package/lib/github/auto-approve.js +1 -1
- package/lib/github/auto-merge.js +1 -1
- package/lib/github/dependabot.js +1 -1
- package/lib/github/github-credentials.js +1 -1
- package/lib/github/github-project.js +1 -1
- package/lib/github/github.js +1 -1
- package/lib/github/mergify.js +1 -1
- package/lib/github/pr-template.js +1 -1
- package/lib/github/pull-request-lint.js +1 -1
- package/lib/github/stale.js +1 -1
- package/lib/github/task-workflow.js +1 -1
- package/lib/github/workflows.js +1 -1
- package/lib/gitlab/configuration.js +1 -1
- package/lib/gitlab/gitlab-configuration.js +1 -1
- package/lib/gitlab/nested-configuration.js +1 -1
- package/lib/gitpod.js +1 -1
- package/lib/ignore-file.js +1 -1
- package/lib/ini.js +1 -1
- package/lib/java/java-project.js +1 -1
- package/lib/java/junit.js +1 -1
- package/lib/java/maven-compile.js +1 -1
- package/lib/java/maven-packaging.js +1 -1
- package/lib/java/maven-sample.js +1 -1
- package/lib/java/pom.js +1 -1
- package/lib/java/projenrc.js +1 -1
- package/lib/javascript/bundler.js +1 -1
- package/lib/javascript/eslint.js +6 -4
- package/lib/javascript/jest.js +1 -1
- package/lib/javascript/node-package.js +1 -1
- package/lib/javascript/node-project.js +1 -1
- package/lib/javascript/npm-config.js +1 -1
- package/lib/javascript/prettier.js +1 -1
- package/lib/javascript/projenrc.js +1 -1
- package/lib/javascript/typescript-config.js +1 -1
- package/lib/javascript/upgrade-dependencies.js +2 -2
- package/lib/json-patch.js +1 -1
- package/lib/json.d.ts +1 -1
- package/lib/json.js +3 -3
- package/lib/license.js +1 -1
- package/lib/logger.js +1 -1
- package/lib/makefile.js +1 -1
- package/lib/object-file.js +1 -1
- package/lib/project-build.js +1 -1
- package/lib/project.js +1 -1
- package/lib/projects.js +1 -1
- package/lib/projenrc-json.js +1 -1
- package/lib/python/pip.js +1 -1
- package/lib/python/poetry.js +2 -2
- package/lib/python/projenrc.js +1 -1
- package/lib/python/pytest-sample.js +1 -1
- package/lib/python/pytest.js +1 -1
- package/lib/python/python-project.js +1 -1
- package/lib/python/python-sample.js +1 -1
- package/lib/python/requirements-file.js +1 -1
- package/lib/python/setuppy.js +1 -1
- package/lib/python/setuptools.js +1 -1
- package/lib/python/venv.js +1 -1
- package/lib/readme.js +1 -1
- package/lib/release/publisher.js +1 -1
- package/lib/release/release-trigger.js +1 -1
- package/lib/release/release.js +1 -1
- package/lib/renovatebot.js +1 -1
- package/lib/run-task.js +1845 -0
- package/lib/sample-file.js +2 -2
- package/lib/semver.js +1 -1
- package/lib/source-code.js +1 -1
- package/lib/task-runtime.js +1 -1
- package/lib/task.js +1 -1
- package/lib/tasks.js +1 -1
- package/lib/testing.js +1 -1
- package/lib/textfile.js +1 -1
- package/lib/toml.js +1 -1
- package/lib/typescript/projenrc.js +1 -1
- package/lib/typescript/typescript-typedoc.js +1 -1
- package/lib/typescript/typescript.js +3 -3
- package/lib/util/synth.d.ts +6 -0
- package/lib/util/synth.js +33 -5
- package/lib/version.js +1 -1
- package/lib/vscode/devcontainer.js +1 -1
- package/lib/vscode/extensions.js +1 -1
- package/lib/vscode/launch-config.js +1 -1
- package/lib/vscode/settings.js +1 -1
- package/lib/vscode/vscode.js +1 -1
- package/lib/web/next.js +3 -3
- package/lib/web/postcss.js +1 -1
- package/lib/web/react.js +4 -4
- package/lib/web/tailwind.js +1 -1
- package/lib/xmlfile.js +1 -1
- package/lib/yaml.js +1 -1
- package/node_modules/array-timsort/LICENSE +21 -0
- package/node_modules/array-timsort/README.md +288 -0
- package/node_modules/array-timsort/package.json +55 -0
- package/node_modules/array-timsort/src/index.js +1080 -0
- package/node_modules/comment-json/LICENSE +21 -0
- package/node_modules/comment-json/README.md +580 -0
- package/node_modules/comment-json/index.d.ts +97 -0
- package/node_modules/comment-json/package.json +68 -0
- package/node_modules/comment-json/src/array.js +288 -0
- package/node_modules/comment-json/src/common.js +181 -0
- package/node_modules/comment-json/src/index.js +13 -0
- package/node_modules/comment-json/src/parse.js +451 -0
- package/node_modules/comment-json/src/stringify.js +366 -0
- package/node_modules/core-util-is/LICENSE +19 -0
- package/node_modules/core-util-is/README.md +3 -0
- package/node_modules/core-util-is/lib/util.js +107 -0
- package/node_modules/core-util-is/package.json +38 -0
- package/node_modules/has-own-prop/index.d.ts +17 -0
- package/node_modules/has-own-prop/index.js +4 -0
- package/node_modules/has-own-prop/license +9 -0
- package/node_modules/has-own-prop/package.json +33 -0
- package/node_modules/has-own-prop/readme.md +30 -0
- package/node_modules/repeat-string/LICENSE +21 -0
- package/node_modules/repeat-string/README.md +136 -0
- package/node_modules/repeat-string/index.js +70 -0
- package/node_modules/repeat-string/package.json +77 -0
- package/package.json +3 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Copyright (c) 2013 kaelzhang <>, contributors
|
|
2
|
+
http://kael.me/
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
a copy of this software and associated documentation files (the
|
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
[](https://travis-ci.org/kaelzhang/node-comment-json)
|
|
2
|
+
[](https://codecov.io/gh/kaelzhang/node-comment-json)
|
|
3
|
+
[](https://www.npmjs.org/package/comment-json)
|
|
4
|
+
<!-- optional appveyor tst
|
|
5
|
+
[](https://ci.appveyor.com/project/kaelzhang/node-comment-json)
|
|
6
|
+
-->
|
|
7
|
+
<!-- optional npm version
|
|
8
|
+
[](http://badge.fury.io/js/comment-json)
|
|
9
|
+
-->
|
|
10
|
+
<!-- optional dependency status
|
|
11
|
+
[](https://david-dm.org/kaelzhang/node-comment-json)
|
|
12
|
+
-->
|
|
13
|
+
|
|
14
|
+
# comment-json
|
|
15
|
+
|
|
16
|
+
Parse and stringify JSON with comments. It will retain comments even after saved!
|
|
17
|
+
|
|
18
|
+
- [Parse](#parse) JSON strings with comments into JavaScript objects and MAINTAIN comments
|
|
19
|
+
- supports comments everywhere, yes, **EVERYWHERE** in a JSON file, eventually 😆
|
|
20
|
+
- fixes the known issue about comments inside arrays.
|
|
21
|
+
- [Stringify](#stringify) the objects into JSON strings with comments if there are
|
|
22
|
+
|
|
23
|
+
The usage of `comment-json` is exactly the same as the vanilla [`JSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) object.
|
|
24
|
+
|
|
25
|
+
## Table of Contents
|
|
26
|
+
|
|
27
|
+
- [Why](#why) and [How](#how)
|
|
28
|
+
- [Usage and examples](#usage)
|
|
29
|
+
- API reference
|
|
30
|
+
- [parse](#parse)
|
|
31
|
+
- [stringify](#stringify)
|
|
32
|
+
- [assign](#assigntarget-object-source-object-keys-array)
|
|
33
|
+
- [CommentArray](#commentarray)
|
|
34
|
+
- [Change Logs](https://github.com/kaelzhang/node-comment-json/releases)
|
|
35
|
+
|
|
36
|
+
## Why?
|
|
37
|
+
|
|
38
|
+
There are many other libraries that can deal with JSON with comments, such as [json5](https://npmjs.org/package/json5), or [strip-json-comments](https://npmjs.org/package/strip-json-comments), but none of them can stringify the parsed object and return back a JSON string the same as the original content.
|
|
39
|
+
|
|
40
|
+
Imagine that if the user settings are saved in `${library}.json`, and the user has written a lot of comments to improve readability. If the library `library` need to modify the user setting, such as modifying some property values and adding new fields, and if the library uses `json5` to read the settings, all comments will disappear after modified which will drive people insane.
|
|
41
|
+
|
|
42
|
+
So, **if you want to parse a JSON string with comments, modify it, then save it back**, `comment-json` is your must choice!
|
|
43
|
+
|
|
44
|
+
## How?
|
|
45
|
+
|
|
46
|
+
`comment-json` parse JSON strings with comments and save comment tokens into [symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) properties.
|
|
47
|
+
|
|
48
|
+
For JSON array with comments, `comment-json` extends the vanilla `Array` object into [`CommentArray`](#commentarray) whose instances could handle comments changes even after a comment array is modified.
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
$ npm i comment-json
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
~~For TypeScript developers, [`@types/comment-json`](https://www.npmjs.com/package/@types/comment-json) could be used~~
|
|
57
|
+
|
|
58
|
+
Since `2.4.1`, `comment-json` contains typescript declarations, so you might as well remove `@types/comment-json`.
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
package.json:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
{
|
|
66
|
+
// package name
|
|
67
|
+
"name": "comment-json"
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
const {
|
|
73
|
+
parse,
|
|
74
|
+
stringify,
|
|
75
|
+
assign
|
|
76
|
+
} = require('comment-json')
|
|
77
|
+
const fs = require('fs')
|
|
78
|
+
|
|
79
|
+
const obj = parse(fs.readFileSync('package.json').toString())
|
|
80
|
+
|
|
81
|
+
console.log(obj.name) // comment-json
|
|
82
|
+
|
|
83
|
+
stringify(obj, null, 2)
|
|
84
|
+
// Will be the same as package.json, Oh yeah! 😆
|
|
85
|
+
// which will be very useful if we use a json file to store configurations.
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Sort keys
|
|
89
|
+
|
|
90
|
+
It is a common use case to sort the keys of a JSON file
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
const parsed = parse(`{
|
|
94
|
+
// b
|
|
95
|
+
"b": 2
|
|
96
|
+
// a
|
|
97
|
+
"a": 1
|
|
98
|
+
}`)
|
|
99
|
+
|
|
100
|
+
// Copy the properties including comments from `parsed` to the new object `{}`
|
|
101
|
+
// according to the sequence of the given keys
|
|
102
|
+
const sorted = assign(
|
|
103
|
+
{},
|
|
104
|
+
parsed,
|
|
105
|
+
// You could also use your custom sorting function
|
|
106
|
+
Object.keys(parsed).sort()
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
console.log(stringify(sorted, null, 2))
|
|
110
|
+
// {
|
|
111
|
+
// // a
|
|
112
|
+
// "a": 1,
|
|
113
|
+
// // b
|
|
114
|
+
// "b": 2
|
|
115
|
+
// }
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
For details about `assign`, see [here](#assigntarget-object-source-object-keys-array).
|
|
119
|
+
|
|
120
|
+
## parse()
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
parse(text, reviver? = null, remove_comments? = false)
|
|
124
|
+
: object | string | number | boolean | null
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
- **text** `string` The string to parse as JSON. See the [JSON](http://json.org/) object for a description of JSON syntax.
|
|
128
|
+
- **reviver?** `Function() | null` Default to `null`. It acts the same as the second parameter of [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). If a function, prescribes how the value originally produced by parsing is transformed, before being returned.
|
|
129
|
+
- **remove_comments?** `boolean = false` If true, the comments won't be maintained, which is often used when we want to get a clean object.
|
|
130
|
+
|
|
131
|
+
Returns `CommentJSONValue` (`object | string | number | boolean | null`) corresponding to the given JSON text.
|
|
132
|
+
|
|
133
|
+
If the `content` is:
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
/**
|
|
137
|
+
before-all
|
|
138
|
+
*/
|
|
139
|
+
// before-all
|
|
140
|
+
{ // before:foo
|
|
141
|
+
// before:foo
|
|
142
|
+
/* before:foo */
|
|
143
|
+
"foo" /* after-prop:foo */: // after-colon:foo
|
|
144
|
+
1 // after-value:foo
|
|
145
|
+
// after-value:foo
|
|
146
|
+
, // after:foo
|
|
147
|
+
// before:bar
|
|
148
|
+
"bar": [ // before:0
|
|
149
|
+
// before:0
|
|
150
|
+
"baz" // after-value:0
|
|
151
|
+
// after-value:0
|
|
152
|
+
, // after:0
|
|
153
|
+
"quux"
|
|
154
|
+
// after:1
|
|
155
|
+
] // after:bar
|
|
156
|
+
// after:bar
|
|
157
|
+
}
|
|
158
|
+
// after-all
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
const {inspect} = require('util')
|
|
163
|
+
|
|
164
|
+
const parsed = parse(content)
|
|
165
|
+
|
|
166
|
+
console.log(
|
|
167
|
+
inspect(parsed, {
|
|
168
|
+
// Since 4.0.0, symbol properties of comments are not enumerable,
|
|
169
|
+
// use `showHidden: true` to print them
|
|
170
|
+
showHidden: true
|
|
171
|
+
})
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
console.log(Object.keys(parsed))
|
|
175
|
+
// > ['foo', 'bar']
|
|
176
|
+
|
|
177
|
+
console.log(stringify(parsed, null, 2))
|
|
178
|
+
// 🚀 Exact as the content above! 🚀
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
And the value of `parsed` will be:
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
{
|
|
185
|
+
// Comments before the JSON object
|
|
186
|
+
[Symbol.for('before-all')]: [{
|
|
187
|
+
type: 'BlockComment',
|
|
188
|
+
value: '\n before-all\n ',
|
|
189
|
+
inline: false,
|
|
190
|
+
loc: {
|
|
191
|
+
// The start location of `/**`
|
|
192
|
+
start: {
|
|
193
|
+
line: 1,
|
|
194
|
+
column: 0
|
|
195
|
+
},
|
|
196
|
+
// The end location of `*/`
|
|
197
|
+
end: {
|
|
198
|
+
line: 3,
|
|
199
|
+
column: 3
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}, {
|
|
203
|
+
type: 'LineComment',
|
|
204
|
+
value: ' before-all',
|
|
205
|
+
inline: false,
|
|
206
|
+
loc: ...
|
|
207
|
+
}],
|
|
208
|
+
...
|
|
209
|
+
|
|
210
|
+
[Symbol.for('after-prop:foo')]: [{
|
|
211
|
+
type: 'BlockComment',
|
|
212
|
+
value: ' after-prop:foo ',
|
|
213
|
+
inline: true,
|
|
214
|
+
loc: ...
|
|
215
|
+
}],
|
|
216
|
+
|
|
217
|
+
// The real value
|
|
218
|
+
foo: 1,
|
|
219
|
+
bar: [
|
|
220
|
+
"baz",
|
|
221
|
+
"quux",
|
|
222
|
+
|
|
223
|
+
// The property of the array
|
|
224
|
+
[Symbol.for('after-value:0')]: [{
|
|
225
|
+
type: 'LineComment',
|
|
226
|
+
value: ' after-value:0',
|
|
227
|
+
inline: true,
|
|
228
|
+
loc: ...
|
|
229
|
+
}, ...],
|
|
230
|
+
...
|
|
231
|
+
]
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
There are **EIGHT** kinds of symbol properties:
|
|
236
|
+
|
|
237
|
+
```js
|
|
238
|
+
// Comments before everything
|
|
239
|
+
Symbol.for('before-all')
|
|
240
|
+
|
|
241
|
+
// If all things inside an object or an array are comments
|
|
242
|
+
Symbol.for('before')
|
|
243
|
+
|
|
244
|
+
// comment tokens before
|
|
245
|
+
// - a property of an object
|
|
246
|
+
// - an item of an array
|
|
247
|
+
// and after the previous comma(`,`) or the opening bracket(`{` or `[`)
|
|
248
|
+
Symbol.for(`before:${prop}`)
|
|
249
|
+
|
|
250
|
+
// comment tokens after property key `prop` and before colon(`:`)
|
|
251
|
+
Symbol.for(`after-prop:${prop}`)
|
|
252
|
+
|
|
253
|
+
// comment tokens after the colon(`:`) of property `prop` and before property value
|
|
254
|
+
Symbol.for(`after-colon:${prop}`)
|
|
255
|
+
|
|
256
|
+
// comment tokens after
|
|
257
|
+
// - the value of property `prop` inside an object
|
|
258
|
+
// - the item of index `prop` inside an array
|
|
259
|
+
// and before the next key-value/item delimiter(`,`)
|
|
260
|
+
// or the closing bracket(`}` or `]`)
|
|
261
|
+
Symbol.for(`after-value:${prop}`)
|
|
262
|
+
|
|
263
|
+
// comment tokens after
|
|
264
|
+
// - comma(`,`)
|
|
265
|
+
// - the value of property `prop` if it is the last property
|
|
266
|
+
Symbol.for(`after:${prop}`)
|
|
267
|
+
|
|
268
|
+
// Comments after everything
|
|
269
|
+
Symbol.for('after-all')
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
And the value of each symbol property is an **array** of `CommentToken`
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
interface CommentToken {
|
|
276
|
+
type: 'BlockComment' | 'LineComment'
|
|
277
|
+
// The content of the comment, including whitespaces and line breaks
|
|
278
|
+
value: string
|
|
279
|
+
// If the start location is the same line as the previous token,
|
|
280
|
+
// then `inline` is `true`
|
|
281
|
+
inline: boolean
|
|
282
|
+
|
|
283
|
+
// But pay attention that,
|
|
284
|
+
// locations will NOT be maintained when stringified
|
|
285
|
+
loc: CommentLocation
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
interface CommentLocation {
|
|
289
|
+
// The start location begins at the `//` or `/*` symbol
|
|
290
|
+
start: Location
|
|
291
|
+
// The end location of multi-line comment ends at the `*/` symbol
|
|
292
|
+
end: Location
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
interface Location {
|
|
296
|
+
line: number
|
|
297
|
+
column: number
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Parse into an object without comments
|
|
302
|
+
|
|
303
|
+
```js
|
|
304
|
+
console.log(parse(content, null, true))
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
And the result will be:
|
|
308
|
+
|
|
309
|
+
```js
|
|
310
|
+
{
|
|
311
|
+
foo: 1,
|
|
312
|
+
bar: [
|
|
313
|
+
"baz",
|
|
314
|
+
"quux"
|
|
315
|
+
]
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Special cases
|
|
320
|
+
|
|
321
|
+
```js
|
|
322
|
+
const parsed = parse(`
|
|
323
|
+
// comment
|
|
324
|
+
1
|
|
325
|
+
`)
|
|
326
|
+
|
|
327
|
+
console.log(parsed === 1)
|
|
328
|
+
// false
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
If we parse a JSON of primative type with `remove_comments:false`, then the return value of `parse()` will be of object type.
|
|
332
|
+
|
|
333
|
+
The value of `parsed` is equivalent to:
|
|
334
|
+
|
|
335
|
+
```js
|
|
336
|
+
const parsed = new Number(1)
|
|
337
|
+
|
|
338
|
+
parsed[Symbol.for('before-all')] = [{
|
|
339
|
+
type: 'LineComment',
|
|
340
|
+
value: ' comment',
|
|
341
|
+
inline: false,
|
|
342
|
+
loc: ...
|
|
343
|
+
}]
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
Which is similar for:
|
|
347
|
+
|
|
348
|
+
- `Boolean` type
|
|
349
|
+
- `String` type
|
|
350
|
+
|
|
351
|
+
For example
|
|
352
|
+
|
|
353
|
+
```js
|
|
354
|
+
const parsed = parse(`
|
|
355
|
+
"foo" /* comment */
|
|
356
|
+
`)
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Which is equivalent to
|
|
360
|
+
|
|
361
|
+
```js
|
|
362
|
+
const parsed = new String('foo')
|
|
363
|
+
|
|
364
|
+
parsed[Symbol.for('after-all')] = [{
|
|
365
|
+
type: 'BlockComment',
|
|
366
|
+
value: ' comment ',
|
|
367
|
+
inline: true,
|
|
368
|
+
loc: ...
|
|
369
|
+
}]
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
But there is one exception:
|
|
373
|
+
|
|
374
|
+
```js
|
|
375
|
+
const parsed = parse(`
|
|
376
|
+
// comment
|
|
377
|
+
null
|
|
378
|
+
`)
|
|
379
|
+
|
|
380
|
+
console.log(parsed === null) // true
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## stringify()
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
stringify(object: any, replacer?, space?): string
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
The arguments are the same as the vanilla [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
|
|
390
|
+
|
|
391
|
+
And it does the similar thing as the vanilla one, but also deal with extra properties and convert them into comments.
|
|
392
|
+
|
|
393
|
+
```js
|
|
394
|
+
console.log(stringify(parsed, null, 2))
|
|
395
|
+
// Exactly the same as `content`
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
#### space
|
|
399
|
+
|
|
400
|
+
If space is not specified, or the space is an empty string, the result of `stringify()` will have no comments.
|
|
401
|
+
|
|
402
|
+
For the case above:
|
|
403
|
+
|
|
404
|
+
```js
|
|
405
|
+
console.log(stringify(result)) // {"a":1}
|
|
406
|
+
console.log(stringify(result, null, 2)) // is the same as `code`
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
## assign(target: object, source?: object, keys?: Array<string>)
|
|
410
|
+
|
|
411
|
+
- **target** `object` the target object
|
|
412
|
+
- **source?** `object` the source object. This parameter is optional but it is silly to not pass this argument.
|
|
413
|
+
- **keys?** `Array<string>` If not specified, all enumerable own properties of `source` will be used.
|
|
414
|
+
|
|
415
|
+
This method is used to copy the enumerable own properties and their corresponding comment symbol properties to the target object.
|
|
416
|
+
|
|
417
|
+
```js
|
|
418
|
+
const parsed = parse(`// before all
|
|
419
|
+
{
|
|
420
|
+
// This is a comment
|
|
421
|
+
"foo": "bar"
|
|
422
|
+
}`)
|
|
423
|
+
|
|
424
|
+
const obj = assign({
|
|
425
|
+
bar: 'baz'
|
|
426
|
+
}, parsed)
|
|
427
|
+
|
|
428
|
+
stringify(obj, null, 2)
|
|
429
|
+
// // before all
|
|
430
|
+
// {
|
|
431
|
+
// "bar": "baz",
|
|
432
|
+
// // This is a comment
|
|
433
|
+
// "foo": "bar"
|
|
434
|
+
// }
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### Special cases about `keys`
|
|
438
|
+
|
|
439
|
+
But if argument `keys` is specified and is not empty, then comment ` before all`, which belongs to no properties, will **NOT** be copied.
|
|
440
|
+
|
|
441
|
+
```js
|
|
442
|
+
const obj = assign({
|
|
443
|
+
bar: 'baz'
|
|
444
|
+
}, parsed, ['foo'])
|
|
445
|
+
|
|
446
|
+
stringify(obj, null, 2)
|
|
447
|
+
// {
|
|
448
|
+
// "bar": "baz",
|
|
449
|
+
// // This is a comment
|
|
450
|
+
// "foo": "bar"
|
|
451
|
+
// }
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
Specifying the argument `keys` as an empty array indicates that it will only copy non-property symbols of comments
|
|
455
|
+
|
|
456
|
+
```js
|
|
457
|
+
const obj = assign({
|
|
458
|
+
bar: 'baz'
|
|
459
|
+
}, parsed, [])
|
|
460
|
+
|
|
461
|
+
stringify(obj, null, 2)
|
|
462
|
+
// // before all
|
|
463
|
+
// {
|
|
464
|
+
// "bar": "baz",
|
|
465
|
+
// }
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
Non-property symbols include:
|
|
469
|
+
|
|
470
|
+
```js
|
|
471
|
+
Symbol.for('before-all')
|
|
472
|
+
Symbol.for('before')
|
|
473
|
+
Symbol.for('after-all')
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
## `CommentArray`
|
|
477
|
+
|
|
478
|
+
> Advanced Section
|
|
479
|
+
|
|
480
|
+
All arrays of the parsed object are `CommentArray`s.
|
|
481
|
+
|
|
482
|
+
The constructor of `CommentArray` could be accessed by:
|
|
483
|
+
|
|
484
|
+
```js
|
|
485
|
+
const {CommentArray} = require('comment-json')
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
If we modify a comment array, its comment symbol properties could be handled automatically.
|
|
489
|
+
|
|
490
|
+
```js
|
|
491
|
+
const parsed = parse(`{
|
|
492
|
+
"foo": [
|
|
493
|
+
// bar
|
|
494
|
+
"bar",
|
|
495
|
+
// baz,
|
|
496
|
+
"baz"
|
|
497
|
+
]
|
|
498
|
+
}`)
|
|
499
|
+
|
|
500
|
+
parsed.foo.unshift('qux')
|
|
501
|
+
|
|
502
|
+
stringify(parsed, null, 2)
|
|
503
|
+
// {
|
|
504
|
+
// "foo": [
|
|
505
|
+
// "qux",
|
|
506
|
+
// // bar
|
|
507
|
+
// "bar",
|
|
508
|
+
// // baz
|
|
509
|
+
// "baz"
|
|
510
|
+
// ]
|
|
511
|
+
// }
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
Oh yeah! 😆
|
|
515
|
+
|
|
516
|
+
But pay attention, if you reassign the property of a comment array with a normal array, all comments will be gone:
|
|
517
|
+
|
|
518
|
+
```js
|
|
519
|
+
parsed.foo = ['quux'].concat(parsed.foo)
|
|
520
|
+
stringify(parsed, null, 2)
|
|
521
|
+
// {
|
|
522
|
+
// "foo": [
|
|
523
|
+
// "quux",
|
|
524
|
+
// "qux",
|
|
525
|
+
// "bar",
|
|
526
|
+
// "baz"
|
|
527
|
+
// ]
|
|
528
|
+
// }
|
|
529
|
+
|
|
530
|
+
// Whoooops!! 😩 Comments are gone
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
Instead, we should:
|
|
534
|
+
|
|
535
|
+
```js
|
|
536
|
+
parsed.foo = new CommentArray('quux').concat(parsed.foo)
|
|
537
|
+
stringify(parsed, null, 2)
|
|
538
|
+
// {
|
|
539
|
+
// "foo": [
|
|
540
|
+
// "quux",
|
|
541
|
+
// "qux",
|
|
542
|
+
// // bar
|
|
543
|
+
// "bar",
|
|
544
|
+
// // baz
|
|
545
|
+
// "baz"
|
|
546
|
+
// ]
|
|
547
|
+
// }
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
## Special Cases about Trailing Comma
|
|
551
|
+
|
|
552
|
+
If we have a JSON string `str`
|
|
553
|
+
|
|
554
|
+
```js
|
|
555
|
+
{
|
|
556
|
+
"foo": "bar", // comment
|
|
557
|
+
}
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
```js
|
|
561
|
+
// When stringify, trailing commas will be eliminated
|
|
562
|
+
const stringified = stringify(parse(str), null, 2)
|
|
563
|
+
console.log(stringified)
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
And it will print:
|
|
567
|
+
|
|
568
|
+
```js
|
|
569
|
+
{
|
|
570
|
+
"foo": "bar" // comment
|
|
571
|
+
}
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
## License
|
|
575
|
+
|
|
576
|
+
[MIT](LICENSE)
|
|
577
|
+
|
|
578
|
+
## Change Logs
|
|
579
|
+
|
|
580
|
+
See [releases](https://github.com/kaelzhang/node-comment-json/releases)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Original from DefinitelyTyped. Thanks a million
|
|
2
|
+
// Type definitions for comment-json 1.1
|
|
3
|
+
// Project: https://github.com/kaelzhang/node-comment-json
|
|
4
|
+
// Definitions by: Jason Dent <https://github.com/Jason3S>
|
|
5
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
6
|
+
|
|
7
|
+
export class CommentArray<TValue> extends Array<TValue> {
|
|
8
|
+
[key: symbol]: CommentToken
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type CommentJSONValue = number
|
|
12
|
+
| string
|
|
13
|
+
| null
|
|
14
|
+
| boolean
|
|
15
|
+
| CommentArray<CommentJSONValue>
|
|
16
|
+
| CommentObject
|
|
17
|
+
|
|
18
|
+
export interface CommentObject {
|
|
19
|
+
[key: string]: CommentJSONValue
|
|
20
|
+
[key: symbol]: CommentToken
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CommentToken {
|
|
24
|
+
type: 'BlockComment' | 'LineComment'
|
|
25
|
+
// The content of the comment, including whitespaces and line breaks
|
|
26
|
+
value: string
|
|
27
|
+
// If the start location is the same line as the previous token,
|
|
28
|
+
// then `inline` is `true`
|
|
29
|
+
inline: boolean
|
|
30
|
+
// But pay attention that,
|
|
31
|
+
// locations will NOT be maintained when stringified
|
|
32
|
+
loc: CommentLocation
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface CommentLocation {
|
|
36
|
+
// The start location begins at the `//` or `/*` symbol
|
|
37
|
+
start: Location
|
|
38
|
+
// The end location of multi-line comment ends at the `*/` symbol
|
|
39
|
+
end: Location
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Location {
|
|
43
|
+
line: number
|
|
44
|
+
column: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type Reviver = (k: number | string, v: unknown) => unknown
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Converts a JavaScript Object Notation (JSON) string into an object.
|
|
51
|
+
* @param json A valid JSON string.
|
|
52
|
+
* @param reviver A function that transforms the results. This function is called for each member of the object.
|
|
53
|
+
* @param removesComments If true, the comments won't be maintained, which is often used when we want to get a clean object.
|
|
54
|
+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
|
|
55
|
+
*/
|
|
56
|
+
export function parse(
|
|
57
|
+
json: string,
|
|
58
|
+
reviver?: Reviver,
|
|
59
|
+
removesComments?: boolean
|
|
60
|
+
): CommentJSONValue
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
|
|
64
|
+
* @param value A JavaScript value, usually an object or array, to be converted.
|
|
65
|
+
* @param replacer A function that transforms the results or an array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
|
|
66
|
+
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
|
|
67
|
+
*/
|
|
68
|
+
export function stringify(
|
|
69
|
+
value: unknown,
|
|
70
|
+
replacer?: (
|
|
71
|
+
(key: string, value: unknown) => unknown
|
|
72
|
+
) | Array<number | string> | null,
|
|
73
|
+
space?: string | number
|
|
74
|
+
): string
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
export function tokenize(input: string, config?: TokenizeOptions): Token[]
|
|
78
|
+
|
|
79
|
+
export interface Token {
|
|
80
|
+
type: string
|
|
81
|
+
value: string
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface TokenizeOptions {
|
|
85
|
+
tolerant?: boolean
|
|
86
|
+
range?: boolean
|
|
87
|
+
loc?: boolean
|
|
88
|
+
comment?: boolean
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function assign<TTarget, TSource>(
|
|
92
|
+
target: TTarget,
|
|
93
|
+
source: TSource,
|
|
94
|
+
// Although it actually accepts more key types and filters then`,
|
|
95
|
+
// we set the type of `keys` stricter
|
|
96
|
+
keys?: readonly (number | string)[]
|
|
97
|
+
): TTarget
|