@canvasengine/compiler 2.0.0-beta.12 → 2.0.0-beta.13

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/grammar.pegjs CHANGED
@@ -92,24 +92,41 @@ attribute
92
92
  / spreadAttribute
93
93
 
94
94
  spreadAttribute
95
- = "..." objectName:identifier {
96
- return "..." + objectName;
95
+ = "..." expr:(functionCallExpr / dotNotation) {
96
+ return "..." + expr;
97
+ }
98
+
99
+ functionCallExpr
100
+ = name:dotNotation "(" args:functionArgs? ")" {
101
+ return `${name}(${args || ''})`;
102
+ }
103
+
104
+ dotNotation
105
+ = first:identifier rest:("." identifier)* {
106
+ return text();
97
107
  }
98
108
 
99
109
  eventHandler
100
110
  = "@" eventName:identifier _ "=" _ "{" _ handlerName:attributeValue _ "}" {
101
- return `${eventName}: ${handlerName}`;
111
+ const needsQuotes = /[^a-zA-Z0-9_$]/.test(eventName);
112
+ const formattedName = needsQuotes ? `'${eventName}'` : eventName;
113
+ return `${formattedName}: ${handlerName}`;
102
114
  }
103
115
  / "@" eventName:attributeName _ {
104
- return eventName;
116
+ const needsQuotes = /[^a-zA-Z0-9_$]/.test(eventName);
117
+ return needsQuotes ? `'${eventName}'` : eventName;
105
118
  }
106
119
 
107
120
  dynamicAttribute
108
121
  = attributeName:attributeName _ "=" _ "{" _ attributeValue:attributeValue _ "}" {
122
+ // Check if attributeName needs to be quoted (contains dash or other invalid JS identifier chars)
123
+ const needsQuotes = /[^a-zA-Z0-9_$]/.test(attributeName);
124
+ const formattedName = needsQuotes ? `'${attributeName}'` : attributeName;
125
+
109
126
  if (attributeValue.startsWith('h(') || attributeValue.includes('=>')) {
110
- return `${attributeName}: ${attributeValue}`;
127
+ return `${formattedName}: ${attributeValue}`;
111
128
  } else if (attributeValue.trim().match(/^[a-zA-Z_]\w*$/)) {
112
- return `${attributeName}: ${attributeValue}`;
129
+ return `${formattedName}: ${attributeValue}`;
113
130
  } else {
114
131
  let foundSignal = false;
115
132
  const computedValue = attributeValue.replace(/@?[a-zA-Z_][a-zA-Z0-9_]*(?!:)/g, (match) => {
@@ -120,13 +137,14 @@ dynamicAttribute
120
137
  return `${match}()`;
121
138
  });
122
139
  if (foundSignal) {
123
- return `${attributeName}: computed(() => ${computedValue})`;
140
+ return `${formattedName}: computed(() => ${computedValue})`;
124
141
  }
125
- return `${attributeName}: ${computedValue}`;
142
+ return `${formattedName}: ${computedValue}`;
126
143
  }
127
144
  }
128
145
  / attributeName:attributeName _ {
129
- return attributeName;
146
+ const needsQuotes = /[^a-zA-Z0-9_$]/.test(attributeName);
147
+ return needsQuotes ? `'${attributeName}'` : attributeName;
130
148
  }
131
149
 
132
150
  attributeValue
@@ -161,7 +179,9 @@ simpleParams
161
179
 
162
180
  staticAttribute
163
181
  = attributeName:attributeName _ "=" _ "\"" attributeValue:staticValue "\"" {
164
- return `${attributeName}: ${attributeValue}`;
182
+ const needsQuotes = /[^a-zA-Z0-9_$]/.test(attributeName);
183
+ const formattedName = needsQuotes ? `'${attributeName}'` : attributeName;
184
+ return `${formattedName}: ${attributeValue}`;
165
185
  }
166
186
 
167
187
  eventAttribute
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canvasengine/compiler",
3
- "version": "2.0.0-beta.12",
3
+ "version": "2.0.0-beta.13",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -69,6 +69,24 @@ describe("Compiler", () => {
69
69
  expect(output).toBe(`h(Canvas, obj)`);
70
70
  });
71
71
 
72
+ test("should compile component with spread operator object", () => {
73
+ const input = `<Canvas ...obj.prop />`;
74
+ const output = parser.parse(input);
75
+ expect(output).toBe(`h(Canvas, obj.prop)`);
76
+ });
77
+
78
+ test("should compile component with spread operator function", () => {
79
+ const input = `<Canvas ...fn() />`;
80
+ const output = parser.parse(input);
81
+ expect(output).toBe(`h(Canvas, fn())`);
82
+ });
83
+
84
+ test("should compile component with spread operator function and params", () => {
85
+ const input = `<Canvas ...fn(x, y) />`;
86
+ const output = parser.parse(input);
87
+ expect(output).toBe(`h(Canvas, fn(x, y))`);
88
+ });
89
+
72
90
  test("should compile component with dynamic attribute but is not a signal", () => {
73
91
  const input = `<Canvas width={20} />`;
74
92
  const output = parser.parse(input);
@@ -141,6 +159,12 @@ describe("Compiler", () => {
141
159
  expect(output).toBe(`h(Canvas, { width: 'val' })`);
142
160
  });
143
161
 
162
+ test("should compile component with static string attribute with dash", () => {
163
+ const input = `<Canvas max-width="val" />`;
164
+ const output = parser.parse(input);
165
+ expect(output).toBe(`h(Canvas, { 'max-width': 'val' })`);
166
+ });
167
+
144
168
  test("should compile component with static attribute (with number)", () => {
145
169
  const input = `<Canvas width="10" />`;
146
170
  const output = parser.parse(input);