Skip to content

Commit

Permalink
refactor: make key on ObjectProperty type optional
Browse files Browse the repository at this point in the history
Close GH-58.

```twig
  {# keys can be omitted if it is the same as the variable name #}
  {foo}
  {# is equivalent to the following #}
  {'foo': foo}
```

Ref: https://twig.symfony.com/doc/3.x/templates.html#literals
  • Loading branch information
zackad committed Aug 27, 2024
1 parent e5e9c54 commit bd69277
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
## unreleased

### Features
- Add support for three-way-comparion operator (spaceship operator)
- Add support for three-way-comparison operator (spaceship operator)

### Bugfixes
- Fix handling mapping that omit key part
- Fix documentation about `twigAlwaysBreakObjects` option to reflect actual default value

### Internals
- Make `key` part of `ObjectProperty` type optional to support object declaration that omit key part

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

Expand Down
4 changes: 2 additions & 2 deletions src/melody/melody-parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,13 @@ export default class Parser {
if (tokens.test(Types.COLON)) {
tokens.expect(Types.COLON);
const value = this.matchExpression();
const prop = new n.ObjectProperty(key, value, computed);
const prop = new n.ObjectProperty(value, computed, key);
copyStart(prop, key);
copyEnd(prop, value);
obj.properties.push(prop);
} else {
const value = key;
const prop = new n.ObjectProperty(key, value, computed, true);
const prop = new n.ObjectProperty(value, computed);
copyStart(prop, key);
copyEnd(prop, value);
obj.properties.push(prop);
Expand Down
12 changes: 5 additions & 7 deletions src/melody/melody-types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,15 @@ visitor(ObjectExpression, "properties");

export class ObjectProperty extends Node {
/**
* @param {Node} key
* @param {Node} value
* @param {boolean} computed
* @param {boolean} omitKey
* @param {Node} value Actual object property value
* @param {boolean} computed Whether or not the Node require additional processing
* @param {Node|null} [key] Optional that would allow omitting key part
*/
constructor(key, value, computed, omitKey = false) {
constructor(value, computed, key = null) {
super();
this.key = key;
this.value = value;
this.key = key;
this.computed = computed;
this.omitKey = omitKey;
}
}
type(ObjectProperty, "ObjectProperty");
Expand Down
26 changes: 14 additions & 12 deletions src/print/ObjectProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ const p = (node, path, print, options) => {
!node.computed &&
Node.isStringLiteral(node.key) &&
!isValidIdentifierName(node.key.value);
const shouldPrintKeyAsString = node.key.wasImplicitConcatenation;
const shouldPrintKeyAsString = node.key?.wasImplicitConcatenation;
const needsParentheses = node.computed && !shouldPrintKeyAsString;
const parts = [];
if (needsParentheses) {
parts.push("(");
}
parts.push(path.call(print, "key"));
if (needsParentheses) {
parts.push(")");
}
// handle property that omit key
if (node.omitKey) {
return parts;

// print "key" part if they exist
if (node.key !== null) {
if (needsParentheses) {
parts.push("(");
}
parts.push(path.call(print, "key"));
if (needsParentheses) {
parts.push(")");
}
parts.push(": ");
}
parts.push(": ");

// print "value" part
node[STRING_NEEDS_QUOTES] = true;
parts.push(path.call(print, "value"));
return parts;
Expand Down

0 comments on commit bd69277

Please sign in to comment.