Skip to content

Commit

Permalink
fix: handle mapping that omit key part
Browse files Browse the repository at this point in the history
  • Loading branch information
zackad committed Aug 19, 2024
1 parent de25ec4 commit b0dccf8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## unreleased

### Bugfixes
- Fix handling mapping that omit key part

---
## 0.8.0 (2024-08-09)

Expand Down
27 changes: 21 additions & 6 deletions src/melody/melody-parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const TAG = Symbol("TAG");
const TEST = Symbol("TEST");

export default class Parser {
/**
* @param {TokenStream} tokenStream
* @param {Object} options
*/
constructor(tokenStream, options) {
this.tokens = tokenStream;
this[UNARY] = {};
Expand Down Expand Up @@ -765,6 +769,9 @@ export default class Parser {
if (!n.is(key, "StringLiteral")) {
computed = true;
}
} else if ((token = tokens.nextIf(Types.EXPRESSION_START))) {
key = this.matchExpression();
computed = true;
} else if ((token = tokens.nextIf(Types.SYMBOL))) {
key = createNode(n.Identifier, token, token.text);
} else if ((token = tokens.nextIf(Types.NUMBER))) {
Expand All @@ -781,12 +788,20 @@ export default class Parser {
tokens.next()
});
}
tokens.expect(Types.COLON);
const value = this.matchExpression();
const prop = new n.ObjectProperty(key, value, computed);
copyStart(prop, key);
copyEnd(prop, value);
obj.properties.push(prop);
if (tokens.test(Types.COLON)) {
tokens.expect(Types.COLON);
const value = this.matchExpression();
const prop = new n.ObjectProperty(key, value, computed);
copyStart(prop, key);
copyEnd(prop, value);
obj.properties.push(prop);
} else {
const value = key;
const prop = new n.ObjectProperty(key, value, computed, true);
copyStart(prop, key);
copyEnd(prop, value);
obj.properties.push(prop);
}
if (!tokens.test(Types.RBRACKET)) {
tokens.expect(Types.COMMA);
// support trailing comma
Expand Down
4 changes: 3 additions & 1 deletion src/melody/melody-types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,14 @@ export class ObjectProperty extends Node {
* @param {Node} key
* @param {Node} value
* @param {boolean} computed
* @param {boolean} omitKey
*/
constructor(key, value, computed) {
constructor(key, value, computed, omitKey = false) {
super();
this.key = key;
this.value = value;
this.computed = computed;
this.omitKey = omitKey;
}
}
type(ObjectProperty, "ObjectProperty");
Expand Down
4 changes: 4 additions & 0 deletions src/print/ObjectProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const p = (node, path, print, options) => {
if (needsParentheses) {
parts.push(")");
}
// handle property that omit key
if (node.omitKey) {
return parts;
}
parts.push(": ");
node[STRING_NEEDS_QUOTES] = true;
parts.push(path.call(print, "value"));
Expand Down

0 comments on commit b0dccf8

Please sign in to comment.