comment-json
Advanced tools
Comparing version 3.0.3 to 4.0.0
{ | ||
"name": "comment-json", | ||
"version": "3.0.3", | ||
"version": "4.0.0", | ||
"description": "Parse and stringify JSON with comments. It will retain comments even after saved!", | ||
@@ -53,9 +53,9 @@ "main": "src/index.js", | ||
"@ostai/eslint-config": "^3.5.0", | ||
"ava": "^3.8.2", | ||
"codecov": "^3.7.0", | ||
"eslint": "^7.1.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"nyc": "^15.0.1", | ||
"ava": "^3.12.1", | ||
"codecov": "^3.7.2", | ||
"eslint": "^7.8.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"nyc": "^15.1.0", | ||
"test-fixture": "^2.4.1", | ||
"typescript": "^3.9.3" | ||
"typescript": "^4.0.2" | ||
}, | ||
@@ -62,0 +62,0 @@ "dependencies": { |
@@ -103,3 +103,3 @@ [![Build Status](https://travis-ci.org/kaelzhang/node-comment-json.svg?branch=master)](https://travis-ci.org/kaelzhang/node-comment-json) | ||
// after-value:foo | ||
, // after-comma:foo | ||
, // after:foo | ||
// before:bar | ||
@@ -110,7 +110,7 @@ "bar": [ // before:0 | ||
// after-value:0 | ||
, // before:1 | ||
, // after:0 | ||
"quux" | ||
// after-value:1 | ||
] // after-value:bar | ||
// after-value:bar | ||
// after:1 | ||
] // after:bar | ||
// after:bar | ||
} | ||
@@ -121,5 +121,17 @@ // after-all | ||
```js | ||
const {inspect} = require('util') | ||
const parsed = parse(content) | ||
console.log(parsed) | ||
console.log( | ||
inspect(parsed, { | ||
// Since 4.0.0, symbol properties of comments are not enumerable, | ||
// use `showHidden: true` to print them | ||
showHidden: true | ||
}) | ||
) | ||
console.log(Object.keys(parsed)) | ||
// > ['foo', 'bar'] | ||
console.log(stringify(parsed, null, 2)) | ||
@@ -183,3 +195,3 @@ // 🚀 Exact as the content above! 🚀 | ||
There are **NINE** kinds of symbol properties: | ||
There are **EIGHT** kinds of symbol properties: | ||
@@ -212,10 +224,7 @@ ```js | ||
// comment tokens after the comma(`,`) | ||
Symbol.for(`after-comma:${prop}`) | ||
// comment tokens after | ||
// - comma(`,`) | ||
// - the value of property `prop` if it is the last property | ||
Symbol.for(`after:${prop}`) | ||
// if comments after | ||
// - the last key-value:pair of an object | ||
// - the last item of an array | ||
Symbol.for('after') | ||
// Comments after everything | ||
@@ -222,0 +231,0 @@ Symbol.for('after-all') |
const hasOwnProperty = require('has-own-prop') | ||
const {isObject, isArray} = require('core-util-is') | ||
const {isArray} = require('core-util-is') | ||
const PREFIX_BEFORE = 'before' | ||
const PREFIX_AFTER_PROP = 'after-prop' | ||
const PREFIX_AFTER_COLON = 'after-colon' | ||
const PREFIX_AFTER_VALUE = 'after-value' | ||
const PREFIX_AFTER_COMMA = 'after-comma' | ||
const { | ||
SYMBOL_PREFIXES, | ||
const SYMBOL_PREFIXES = [ | ||
PREFIX_BEFORE, | ||
PREFIX_AFTER_PROP, | ||
PREFIX_AFTER_COLON, | ||
PREFIX_AFTER_VALUE, | ||
PREFIX_AFTER_COMMA | ||
] | ||
UNDEFINED, | ||
const COLON = ':' | ||
const UNDEFINED = undefined | ||
symbol, | ||
define, | ||
copy_comments | ||
} = require('./common') | ||
const symbol = (prefix, key) => Symbol.for(prefix + COLON + key) | ||
const assign_comments = ( | ||
target, source, target_key, source_key, prefix, remove_source | ||
) => { | ||
const source_prop = symbol(prefix, source_key) | ||
if (!hasOwnProperty(source, source_prop)) { | ||
return | ||
} | ||
const target_prop = target_key === source_key | ||
? source_prop | ||
: symbol(prefix, target_key) | ||
target[target_prop] = source[source_prop] | ||
if (remove_source) { | ||
delete source[source_prop] | ||
} | ||
} | ||
// Assign keys and comments | ||
const assign = (target, source, keys) => { | ||
keys.forEach(key => { | ||
if (!hasOwnProperty(source, key)) { | ||
return | ||
} | ||
target[key] = source[key] | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
assign_comments(target, source, key, key, prefix) | ||
}) | ||
}) | ||
return target | ||
} | ||
const swap_comments = (array, from, to) => { | ||
@@ -66,3 +23,3 @@ if (from === to) { | ||
if (!hasOwnProperty(array, target_prop)) { | ||
assign_comments(array, array, to, from, prefix) | ||
copy_comments(array, array, to, from, prefix) | ||
return | ||
@@ -72,4 +29,4 @@ } | ||
const comments = array[target_prop] | ||
assign_comments(array, array, to, from, prefix) | ||
array[symbol(prefix, from)] = comments | ||
copy_comments(array, array, to, from, prefix) | ||
define(array, symbol(prefix, from), comments) | ||
}) | ||
@@ -90,3 +47,3 @@ } | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
assign_comments(target, source, i + offset, i, prefix, remove) | ||
copy_comments(target, source, i + offset, i, prefix, remove) | ||
}) | ||
@@ -288,29 +245,3 @@ } | ||
module.exports = { | ||
CommentArray, | ||
assign (target, source, keys) { | ||
if (!isObject(target)) { | ||
throw new TypeError('Cannot convert undefined or null to object') | ||
} | ||
if (!isObject(source)) { | ||
return target | ||
} | ||
if (keys === UNDEFINED) { | ||
keys = Object.keys(source) | ||
} else if (!isArray(keys)) { | ||
throw new TypeError('keys must be array or undefined') | ||
} | ||
return assign(target, source, keys) | ||
}, | ||
PREFIX_BEFORE, | ||
PREFIX_AFTER_PROP, | ||
PREFIX_AFTER_COLON, | ||
PREFIX_AFTER_VALUE, | ||
PREFIX_AFTER_COMMA, | ||
COLON, | ||
UNDEFINED | ||
CommentArray | ||
} |
const {parse, tokenize} = require('./parse') | ||
const stringify = require('./stringify') | ||
const {CommentArray, assign} = require('./array') | ||
const {CommentArray} = require('./array') | ||
const {assign} = require('./common') | ||
@@ -5,0 +6,0 @@ module.exports = { |
125
src/parse.js
@@ -7,3 +7,7 @@ // JSON formatting | ||
CommentArray, | ||
} = require('./array') | ||
const { | ||
NON_PROP_SYMBOL_KEYS, | ||
PREFIX_BEFORE, | ||
@@ -13,7 +17,22 @@ PREFIX_AFTER_PROP, | ||
PREFIX_AFTER_VALUE, | ||
PREFIX_AFTER_COMMA, | ||
PREFIX_AFTER, | ||
PREFIX_BEFORE_ALL, | ||
PREFIX_AFTER_ALL, | ||
BRACKET_OPEN, | ||
BRACKET_CLOSE, | ||
CURLY_BRACKET_OPEN, | ||
CURLY_BRACKET_CLOSE, | ||
COLON, | ||
UNDEFINED | ||
} = require('./array') | ||
COMMA, | ||
MINUS, | ||
EMPTY, | ||
UNDEFINED, | ||
define | ||
} = require('./common') | ||
const tokenize = code => esprima.tokenize(code, { | ||
@@ -60,17 +79,5 @@ comment: true, | ||
const PREFIX_BEFORE_ALL = 'before-all' | ||
const PREFIX_AFTER = 'after' | ||
const PREFIX_AFTER_ALL = 'after-all' | ||
const BRACKET_OPEN = '[' | ||
const BRACKET_CLOSE = ']' | ||
const CURLY_BRACKET_OPEN = '{' | ||
const CURLY_BRACKET_CLOSE = '}' | ||
const COMMA = ',' | ||
const EMPTY = '' | ||
const MINUS = '-' | ||
const symbolFor = prefix => Symbol.for( | ||
last_prop !== UNDEFINED | ||
? `${prefix}:${last_prop}` | ||
? prefix + COLON + last_prop | ||
: prefix | ||
@@ -142,3 +149,3 @@ ) | ||
const assign_after_comma_comments = () => { | ||
const assign_after_comments = () => { | ||
if (!unassigned_comments) { | ||
@@ -148,3 +155,3 @@ return | ||
const after_comma_comments = [] | ||
const after_comments = [] | ||
@@ -154,3 +161,3 @@ for (const comment of unassigned_comments) { | ||
if (comment.inline) { | ||
after_comma_comments.push(comment) | ||
after_comments.push(comment) | ||
// Otherwise, all comments are before:<next-prop> comment | ||
@@ -162,3 +169,3 @@ } else { | ||
const {length} = after_comma_comments | ||
const {length} = after_comments | ||
if (!length) { | ||
@@ -175,3 +182,3 @@ return | ||
comments_host[symbolFor(PREFIX_AFTER_COMMA)] = after_comma_comments | ||
define(comments_host, symbolFor(PREFIX_AFTER), after_comments) | ||
} | ||
@@ -184,3 +191,4 @@ | ||
comments_host[symbolFor(prefix)] = unassigned_comments | ||
define(comments_host, symbolFor(prefix), unassigned_comments) | ||
unassigned_comments = null | ||
@@ -219,3 +227,3 @@ } | ||
if (prefix) { | ||
comments_host[symbolFor(prefix)] = comments | ||
define(comments_host, symbolFor(prefix), comments) | ||
return | ||
@@ -251,2 +259,4 @@ } | ||
if (started) { | ||
assign_comments(PREFIX_AFTER_VALUE) | ||
// key-value pair delimiter | ||
@@ -257,3 +267,3 @@ expect(COMMA) | ||
assign_after_comma_comments() | ||
assign_after_comments() | ||
@@ -287,5 +297,11 @@ // If there is a trailing comma, we might reach the end | ||
obj[name] = transform(name, walk()) | ||
parse_comments(PREFIX_AFTER_VALUE) | ||
parse_comments() | ||
} | ||
if (started) { | ||
// If there are properties, | ||
// then the unassigned comments are after comments | ||
assign_comments(PREFIX_AFTER) | ||
} | ||
// bypass } | ||
@@ -295,9 +311,6 @@ next() | ||
// If there is no properties in the object, | ||
// try to save unassigned comments | ||
assign_comments( | ||
started | ||
? PREFIX_AFTER | ||
: PREFIX_BEFORE | ||
) | ||
if (!started) { | ||
// Otherwise, they are before comments | ||
assign_comments(PREFIX_BEFORE) | ||
} | ||
@@ -322,2 +335,3 @@ restore_comments_host() | ||
if (started) { | ||
assign_comments(PREFIX_AFTER_VALUE) | ||
expect(COMMA) | ||
@@ -327,3 +341,3 @@ next() | ||
assign_after_comma_comments() | ||
assign_after_comments() | ||
@@ -341,14 +355,17 @@ if (is(BRACKET_CLOSE)) { | ||
array[i] = transform(i, walk()) | ||
parse_comments(PREFIX_AFTER_VALUE) | ||
i ++ | ||
i ++ | ||
parse_comments() | ||
} | ||
if (started) { | ||
assign_comments(PREFIX_AFTER) | ||
} | ||
next() | ||
last_prop = undefined | ||
assign_comments( | ||
started | ||
? PREFIX_AFTER | ||
: PREFIX_BEFORE | ||
) | ||
if (!started) { | ||
assign_comments(PREFIX_BEFORE) | ||
} | ||
@@ -436,3 +453,9 @@ restore_comments_host() | ||
Object.assign(result, comments_host) | ||
NON_PROP_SYMBOL_KEYS.forEach(key => { | ||
const comments = comments_host[key] | ||
if (comments) { | ||
define(result, key, comments) | ||
} | ||
}) | ||
} | ||
@@ -452,21 +475,3 @@ | ||
parse, | ||
tokenize, | ||
PREFIX_BEFORE, | ||
PREFIX_BEFORE_ALL, | ||
PREFIX_AFTER_PROP, | ||
PREFIX_AFTER_COLON, | ||
PREFIX_AFTER_VALUE, | ||
PREFIX_AFTER_COMMA, | ||
PREFIX_AFTER, | ||
PREFIX_AFTER_ALL, | ||
BRACKET_OPEN, | ||
BRACKET_CLOSE, | ||
CURLY_BRACKET_OPEN, | ||
CURLY_BRACKET_CLOSE, | ||
COLON, | ||
COMMA, | ||
EMPTY, | ||
UNDEFINED | ||
tokenize | ||
} |
@@ -12,3 +12,2 @@ const { | ||
PREFIX_AFTER_VALUE, | ||
PREFIX_AFTER_COMMA, | ||
PREFIX_AFTER, | ||
@@ -26,3 +25,3 @@ PREFIX_AFTER_ALL, | ||
UNDEFINED | ||
} = require('./parse') | ||
} = require('./common') | ||
@@ -42,3 +41,3 @@ // eslint-disable-next-line no-control-regex, no-misleading-character-class | ||
const AFTER_VALUE = prop => `${PREFIX_AFTER_VALUE}:${prop}` | ||
const AFTER_COMMA = prop => `${PREFIX_AFTER_COMMA}:${prop}` | ||
const AFTER = prop => `${PREFIX_AFTER}:${prop}` | ||
@@ -173,3 +172,3 @@ // table of character substitutions | ||
after_comma = process_comments(value, AFTER_COMMA(i), deeper_gap) | ||
after_comma = process_comments(value, AFTER(i), deeper_gap) | ||
} | ||
@@ -244,3 +243,3 @@ | ||
after_comma = process_comments(value, AFTER_COMMA(key), deeper_gap) | ||
after_comma = process_comments(value, AFTER(key), deeper_gap) | ||
} | ||
@@ -247,0 +246,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41403
10
1016
497