coffeehaml 0.7.4 → 0.7.6

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.
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // expressions.coffee — CoffeeScript expression compilation bridge
3
- var _coffeeScriptAvailable, _coffeeScriptUnavailableReason, _require, checkCoffeeScript, loadCoffeeScript, stripCoffeeWrapper, stripToConst;
3
+ var _coffeeScriptAvailable, _coffeeScriptUnavailableReason, _require, checkCoffeeScript, collapseWs, loadCoffeeScript, splitTopLevelStatements, stripCoffeeWrapper, stripToConst;
4
4
 
5
5
  import {
6
6
  createRequire
@@ -110,11 +110,11 @@ stripCoffeeWrapper = function(js, _original) {
110
110
  result = js.trim();
111
111
  result = result.replace(/;\s*$/, '');
112
112
  lines = (function() {
113
- var i, len, ref, results;
113
+ var j, len, ref, results;
114
114
  ref = result.split('\n');
115
115
  results = [];
116
- for (i = 0, len = ref.length; i < len; i++) {
117
- l = ref[i];
116
+ for (j = 0, len = ref.length; j < len; j++) {
117
+ l = ref[j];
118
118
  if (l.trim() !== '') {
119
119
  results.push(l);
120
120
  }
@@ -123,10 +123,10 @@ stripCoffeeWrapper = function(js, _original) {
123
123
  })();
124
124
  if (lines.length > 0) {
125
125
  varLines = (function() {
126
- var i, len, results;
126
+ var j, len, results;
127
127
  results = [];
128
- for (i = 0, len = lines.length; i < len; i++) {
129
- l = lines[i];
128
+ for (j = 0, len = lines.length; j < len; j++) {
129
+ l = lines[j];
130
130
  if (/^\s*var\b/.test(l)) {
131
131
  results.push(l);
132
132
  }
@@ -134,10 +134,10 @@ stripCoffeeWrapper = function(js, _original) {
134
134
  return results;
135
135
  })();
136
136
  meaningful = (function() {
137
- var i, len, results;
137
+ var j, len, results;
138
138
  results = [];
139
- for (i = 0, len = lines.length; i < len; i++) {
140
- l = lines[i];
139
+ for (j = 0, len = lines.length; j < len; j++) {
140
+ l = lines[j];
141
141
  if (!/^\s*var\b/.test(l)) {
142
142
  results.push(l);
143
143
  }
@@ -180,44 +180,127 @@ export var compileStatement = function(source, location = null) {
180
180
 
181
181
  // ─── Convert var → const/let ───────────────────────────────
182
182
  stripToConst = function(js) {
183
- var assignLine, assignments, i, l, len, lines, name, varLine, varNames;
184
- lines = (function() {
185
- var i, len, ref, results;
186
- ref = js.split('\n');
187
- results = [];
188
- for (i = 0, len = ref.length; i < len; i++) {
189
- l = ref[i];
190
- if (l.trim() !== '') {
191
- results.push(l);
192
- }
193
- }
194
- return results;
195
- })();
196
- varLine = lines.find(function(l) {
197
- return /^\s*var\b/.test(l);
183
+ var assign, assignments, body, idx, j, len, name, stmts, varIdx, varNames;
184
+ stmts = splitTopLevelStatements(js);
185
+ if (stmts.length === 0) {
186
+ return js.replace(/;\s*$/, '');
187
+ }
188
+ varIdx = stmts.findIndex(function(s) {
189
+ return /^\s*var\b/.test(s);
198
190
  });
199
- if (!varLine) {
191
+ if (varIdx < 0) {
200
192
  return js.replace(/;\s*$/, '');
201
193
  }
202
- varNames = varLine.replace(/^\s*var\s+/, '').replace(/;\s*$/, '').split(',').map(function(s) {
194
+ varNames = stmts[varIdx].replace(/^\s*var\s+/, '').replace(/;\s*$/, '').split(',').map(function(s) {
203
195
  return s.trim();
204
196
  }).filter(Boolean);
197
+ body = stmts.filter(function(s, i) {
198
+ return i !== varIdx;
199
+ });
205
200
  assignments = [];
206
- for (i = 0, len = varNames.length; i < len; i++) {
207
- name = varNames[i];
208
- assignLine = lines.find(function(l) {
201
+ for (j = 0, len = varNames.length; j < len; j++) {
202
+ name = varNames[j];
203
+ idx = body.findIndex(function(s) {
209
204
  var t;
210
- t = l.trim();
205
+ t = s.trim();
211
206
  return t.startsWith(`${name} =`) || t.startsWith(`${name}=`);
212
207
  });
213
- if (assignLine) {
214
- assignments.push('const ' + assignLine.trim().replace(/;\s*$/, ''));
208
+ if (idx >= 0) {
209
+ assign = collapseWs(body[idx]).replace(/;\s*$/, '');
210
+ assignments.push('const ' + assign);
215
211
  }
216
212
  }
217
213
  if (assignments.length > 0) {
218
214
  return assignments.join('; ');
219
215
  }
220
- return lines.filter(function(l) {
221
- return !/^\s*$/.test(l);
222
- }).join(' ').replace(/;\s*$/, '');
216
+ return body.map(function(s) {
217
+ return collapseWs(s);
218
+ }).join(' ');
219
+ };
220
+
221
+ // Split compiled JS into top-level statements, respecting string literals
222
+ // and bracket nesting so multiline object/array/call literals stay intact.
223
+ splitTopLevelStatements = function(js) {
224
+ var ch, depth, i, inString, last, start, stmts;
225
+ stmts = [];
226
+ depth = 0;
227
+ inString = null;
228
+ start = 0;
229
+ i = 0;
230
+ while (i < js.length) {
231
+ ch = js[i];
232
+ if (inString) {
233
+ if (ch === '\\') {
234
+ i += 2;
235
+ continue;
236
+ }
237
+ if (ch === inString) {
238
+ inString = null;
239
+ }
240
+ i++;
241
+ continue;
242
+ }
243
+ if (ch === "'" || ch === '"' || ch === '`') {
244
+ inString = ch;
245
+ } else if (ch === '{' || ch === '[' || ch === '(') {
246
+ depth++;
247
+ } else if (ch === '}' || ch === ']' || ch === ')') {
248
+ depth--;
249
+ } else if (ch === ';' && depth === 0) {
250
+ stmts.push(js.slice(start, i).trim());
251
+ start = i + 1;
252
+ }
253
+ i++;
254
+ }
255
+ last = js.slice(start).trim();
256
+ if (last) {
257
+ stmts.push(last);
258
+ }
259
+ return stmts;
260
+ };
261
+
262
+ // Collapse runs of whitespace to single spaces, preserving string contents.
263
+ collapseWs = function(s) {
264
+ var ch, i, inString, out, pendingSpace;
265
+ out = '';
266
+ inString = null;
267
+ pendingSpace = false;
268
+ i = 0;
269
+ while (i < s.length) {
270
+ ch = s[i];
271
+ if (inString) {
272
+ out += ch;
273
+ if (ch === '\\' && i + 1 < s.length) {
274
+ out += s[i + 1];
275
+ i += 2;
276
+ continue;
277
+ }
278
+ if (ch === inString) {
279
+ inString = null;
280
+ }
281
+ i++;
282
+ continue;
283
+ }
284
+ if (ch === "'" || ch === '"' || ch === '`') {
285
+ if (pendingSpace && out.length) {
286
+ out += ' ';
287
+ }
288
+ pendingSpace = false;
289
+ inString = ch;
290
+ out += ch;
291
+ i++;
292
+ continue;
293
+ }
294
+ if (/\s/.test(ch)) {
295
+ pendingSpace = true;
296
+ } else {
297
+ if (pendingSpace && out.length) {
298
+ out += ' ';
299
+ }
300
+ pendingSpace = false;
301
+ out += ch;
302
+ }
303
+ i++;
304
+ }
305
+ return out;
223
306
  };
@@ -155,10 +155,11 @@ normalizeSpacing = function(tokens, originalSource = null) {
155
155
  }
156
156
  }
157
157
  // Handle INDENT — represents inline then/when/else/etc from CoffeeScript
158
+ // Only emit keyword text (e.g. "then"), skip structural indentation digits
158
159
  if (cur.type === 'INDENT') {
159
160
  text = getTokenText(cur, originalSource);
160
161
  trimmed = text != null ? text.trim() : void 0;
161
- if (trimmed && trimmed !== '') {
162
+ if (trimmed && trimmed !== '' && !/^\d+$/.test(trimmed)) {
162
163
  parts.push(' ' + trimmed + ' ');
163
164
  prevWasAtExprStart = false;
164
165
  afterPrevIsExprStart = true;
@@ -216,7 +216,7 @@ export var defaultOptions = {
216
216
  implicitDivExpansion: false,
217
217
  maxChainLength: 4,
218
218
  inlineThreshold: -1,
219
- voidElementStyle: 'self-closing',
219
+ voidElementStyle: 'explicit',
220
220
  attributeStyle: 'preserve',
221
221
  attributeMultilineThreshold: 1,
222
222
  attributeSort: 'none',
@@ -233,7 +233,7 @@ printNode = function(path, o, printFn) {
233
233
 
234
234
  // ─── Document ──────────────────────────────────────────────
235
235
  printDoc = function(path, o, printFn) {
236
- var childDocs, k, len, n, next, node, ref, ref1;
236
+ var allDocs, childDocs, gap, k, len, n, next, node, prologue, ref, ref1;
237
237
  node = path.node;
238
238
  // Pre-pass: merge statements if enabled
239
239
  if (o.statementMerging === 'merge') {
@@ -250,9 +250,11 @@ printDoc = function(path, o, printFn) {
250
250
  }
251
251
  }
252
252
  }
253
- // Print children
253
+ // Print prologue (bare import/require lines at top of file)
254
+ prologue = node.prologue || [];
254
255
  childDocs = (ref1 = path.map(printFn, 'children')) != null ? ref1 : [];
255
- return join(hardline, childDocs);
256
+ allDocs = prologue.length ? (gap = childDocs.length ? [''] : [], prologue.concat(gap).concat(childDocs)) : childDocs;
257
+ return join(hardline, allDocs);
256
258
  };
257
259
 
258
260
  // ─── Element ───────────────────────────────────────────────
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coffeehaml",
3
- "version": "0.7.4",
3
+ "version": "0.7.6",
4
4
  "description": "Haml structure. CoffeeScript semantics. React runtime. Zero runtime. Vite-native.",
5
5
  "type": "module",
6
6
  "license": "MIT",