coffeehaml 0.7.3 → 0.7.5

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/dist/parser.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // parser.coffee — recursive descent parser
3
3
  // Converts token stream into AST
4
- var ParserState, findColon, isElse, parseAttributeBlock, parseBlock, parseComment, parseControlFlow, parseControlKind, parseDoctype, parseElement, parseFilter, parseHtmlComment, parseImplicitDiv, parseNode, parseOutput, parseText, splitAttributePairs, stripControlKeyword;
4
+ var ParserState, cleanAttrName, findColon, findEquals, isElse, parseAttributeBlock, parseBareAttributes, parseBlock, parseComment, parseControlFlow, parseControlKind, parseDoctype, parseElement, parseFilter, parseHtmlComment, parseImplicitDiv, parseNode, parseOutput, parseText, splitAttributePairs, splitBareAttributePairs, stripControlKeyword;
5
5
 
6
6
  import {
7
7
  TokenType
@@ -202,7 +202,7 @@ parseElement = function(state) {
202
202
  attributes = [];
203
203
  isSelfClosing = false;
204
204
  attrStyle = null;
205
- while (state.current() && ((ref1 = state.current().type) === TokenType.CLASS || ref1 === TokenType.ID || ref1 === TokenType.ATTRS_BRACE || ref1 === TokenType.ATTRS_PAREN || ref1 === TokenType.SELF_CLOSE)) {
205
+ while (state.current() && ((ref1 = state.current().type) === TokenType.CLASS || ref1 === TokenType.ID || ref1 === TokenType.ATTRS_BRACE || ref1 === TokenType.ATTRS_PAREN || ref1 === TokenType.ATTRS_BARE || ref1 === TokenType.SELF_CLOSE)) {
206
206
  tok = state.current();
207
207
  switch (tok.type) {
208
208
  case TokenType.CLASS:
@@ -223,6 +223,11 @@ parseElement = function(state) {
223
223
  attributes.push(...parseAttributeBlock(tok.value, '()', tagToken.location));
224
224
  attrStyle = 'parens';
225
225
  break;
226
+ case TokenType.ATTRS_BARE:
227
+ state.advance();
228
+ attributes.push(...parseBareAttributes(tok.value));
229
+ attrStyle = 'bare';
230
+ break;
226
231
  case TokenType.SELF_CLOSE:
227
232
  state.advance();
228
233
  isSelfClosing = true;
@@ -271,7 +276,7 @@ parseImplicitDiv = function(state) {
271
276
  attributes = [];
272
277
  attrStyle = null;
273
278
  firstToken = state.current();
274
- while (state.current() && ((ref = state.current().type) === TokenType.CLASS || ref === TokenType.ID || ref === TokenType.ATTRS_BRACE || ref === TokenType.ATTRS_PAREN)) {
279
+ while (state.current() && ((ref = state.current().type) === TokenType.CLASS || ref === TokenType.ID || ref === TokenType.ATTRS_BRACE || ref === TokenType.ATTRS_PAREN || ref === TokenType.ATTRS_BARE)) {
275
280
  tok = state.current();
276
281
  switch (tok.type) {
277
282
  case TokenType.CLASS:
@@ -291,6 +296,11 @@ parseImplicitDiv = function(state) {
291
296
  state.advance();
292
297
  attributes.push(...parseAttributeBlock(tok.value, '()', firstToken.location));
293
298
  attrStyle = 'parens';
299
+ break;
300
+ case TokenType.ATTRS_BARE:
301
+ state.advance();
302
+ attributes.push(...parseBareAttributes(tok.value));
303
+ attrStyle = 'bare';
294
304
  }
295
305
  }
296
306
  // Parse inline text
@@ -472,6 +482,110 @@ parseText = function(state) {
472
482
  };
473
483
 
474
484
  // ─── Attribute Block Parser ────────────────────────────────
485
+
486
+ // Parse bare HTML-style attributes: key="val", key='val', key={expr}, flag
487
+ parseBareAttributes = function(source) {
488
+ var attrs, eqIdx, j, len, name, part, parts, trimmed, val;
489
+ attrs = [];
490
+ if (!(source != null ? source.trim() : void 0)) {
491
+ return attrs;
492
+ }
493
+ // Split on whitespace respecting quotes and braces
494
+ parts = splitBareAttributePairs(source);
495
+ for (j = 0, len = parts.length; j < len; j++) {
496
+ part = parts[j];
497
+ trimmed = part.trim();
498
+ if (!trimmed) {
499
+ continue;
500
+ }
501
+ // key=value
502
+ eqIdx = findEquals(trimmed);
503
+ if (eqIdx > 0) {
504
+ name = trimmed.slice(0, eqIdx);
505
+ val = trimmed.slice(eqIdx + 1);
506
+ attrs.push(new Attribute(cleanAttrName(name), new Expression(val), false));
507
+ } else {
508
+ // Bare word → shorthand attribute like `flag` → flag={flag}
509
+ attrs.push(new Attribute(trimmed, new Expression(trimmed), true));
510
+ }
511
+ }
512
+ return attrs;
513
+ };
514
+
515
+ // Split bare attributes on whitespace, respecting quotes and braces
516
+ splitBareAttributePairs = function(source) {
517
+ var ch, depth, i, q, result, start;
518
+ result = [];
519
+ depth = 0;
520
+ start = 0;
521
+ i = 0;
522
+ while (i < source.length) {
523
+ ch = source[i];
524
+ if (ch === '{' || ch === '[' || ch === '(') {
525
+ depth++;
526
+ } else if (ch === '}' || ch === ']' || ch === ')') {
527
+ depth--;
528
+ } else if ((ch === '"' || ch === "'") && depth === 0) {
529
+ q = ch;
530
+ i++;
531
+ while (i < source.length && source[i] !== q) {
532
+ if (source[i] === '\\') {
533
+ i++;
534
+ }
535
+ i++;
536
+ }
537
+ } else if (/[\t ]/.test(ch) && depth === 0) {
538
+ if (i > start) {
539
+ result.push(source.slice(start, i));
540
+ }
541
+ start = i + 1;
542
+ }
543
+ i++;
544
+ }
545
+ if (i > start) {
546
+ result.push(source.slice(start));
547
+ }
548
+ return result;
549
+ };
550
+
551
+ // Find = sign (not inside quotes/braces)
552
+ findEquals = function(source) {
553
+ var ch, depth, i, q;
554
+ depth = 0;
555
+ i = 0;
556
+ while (i < source.length) {
557
+ ch = source[i];
558
+ if (ch === '{' || ch === '[' || ch === '(') {
559
+ depth++;
560
+ } else if (ch === '}' || ch === ']' || ch === ')') {
561
+ depth--;
562
+ } else if ((ch === '"' || ch === "'") && depth === 0) {
563
+ q = ch;
564
+ i++;
565
+ while (i < source.length && source[i] !== q) {
566
+ if (source[i] === '\\') {
567
+ i++;
568
+ }
569
+ i++;
570
+ }
571
+ } else if (ch === '=' && depth === 0) {
572
+ return i;
573
+ }
574
+ i++;
575
+ }
576
+ return -1;
577
+ };
578
+
579
+ // Remove surrounding quotes from attr name
580
+ cleanAttrName = function(name) {
581
+ var n;
582
+ n = name.trim();
583
+ if ((n.startsWith('"') && n.endsWith('"')) || (n.startsWith("'") && n.endsWith("'"))) {
584
+ n = n.slice(1, -1);
585
+ }
586
+ return n;
587
+ };
588
+
475
589
  parseAttributeBlock = function(source, _style, _location = null) {
476
590
  var attrs, cleanName, colonIdx, expr, j, len, name, pair, pairs, trimmed, value;
477
591
  attrs = [];
@@ -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;
@@ -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 ───────────────────────────────────────────────
@@ -499,41 +501,65 @@ detectStyle = function(node, o) {
499
501
  };
500
502
 
501
503
  formatAttributes = function(node, style, o) {
502
- var attr, attrs, expr, k, len, name, parts, ref, ref1, ref2, val;
504
+ var attr, attrs, bareParts, expr, k, len, name, parts, ref, ref1, ref2, val;
503
505
  attrs = (ref = node.attributes) != null ? ref : [];
504
506
  if (!(attrs.length > 0)) {
505
507
  return '';
506
508
  }
507
- parts = [];
508
- for (k = 0, len = attrs.length; k < len; k++) {
509
- attr = attrs[k];
510
- if (attr.spread) {
511
- expr = (ref1 = attr.expression.source) != null ? ref1 : '';
509
+ // Bare style: HTML-style key="val" key2={expr} — space-separated
510
+ if (style === 'bare') {
511
+ bareParts = (function() {
512
+ var k, len, ref1, ref2, results;
513
+ results = [];
514
+ for (k = 0, len = attrs.length; k < len; k++) {
515
+ attr = attrs[k];
516
+ if (attr.spread) {
517
+ expr = (ref1 = attr.expression.source) != null ? ref1 : '';
518
+ if (o.coffeeScriptFormat) {
519
+ expr = formatCoffeeScript(expr, o);
520
+ }
521
+ results.push(`{${expr}...}`);
522
+ } else if (attr.shorthand) {
523
+ results.push(attr.name);
524
+ } else {
525
+ results.push(`${attr.name}=${(ref2 = attr.value.source) != null ? ref2 : ''}`);
526
+ }
527
+ }
528
+ return results;
529
+ })();
530
+ return ` ${bareParts.join(' ')}`;
531
+ } else {
532
+ parts = [];
533
+ for (k = 0, len = attrs.length; k < len; k++) {
534
+ attr = attrs[k];
535
+ if (attr.spread) {
536
+ expr = (ref1 = attr.expression.source) != null ? ref1 : '';
537
+ if (o.coffeeScriptFormat) {
538
+ expr = formatCoffeeScript(expr, o);
539
+ }
540
+ parts.push(style === 'parens' ? `...${expr}` : `{${expr}...}`);
541
+ continue;
542
+ }
543
+ name = attr.name;
544
+ val = (ref2 = attr.value.source) != null ? ref2 : '';
512
545
  if (o.coffeeScriptFormat) {
513
- expr = formatCoffeeScript(expr, o);
546
+ val = formatCoffeeScript(val, o);
547
+ }
548
+ if (attr.shorthand) {
549
+ parts.push(name);
550
+ } else {
551
+ parts.push(`${name}: ${val}`);
514
552
  }
515
- parts.push(style === 'parens' ? `...${expr}` : `{${expr}...}`);
516
- continue;
517
- }
518
- name = attr.name;
519
- val = (ref2 = attr.value.source) != null ? ref2 : '';
520
- if (o.coffeeScriptFormat) {
521
- val = formatCoffeeScript(val, o);
522
553
  }
523
- if (attr.shorthand) {
524
- parts.push(name);
525
- } else {
526
- parts.push(`${name}: ${val}`);
554
+ switch (style) {
555
+ case 'braces':
556
+ return `{ ${parts.join(', ')} }`;
557
+ case 'parens':
558
+ return `( ${parts.join(', ')} )`;
559
+ default:
560
+ return parts.join(', ');
527
561
  }
528
562
  }
529
- switch (style) {
530
- case 'braces':
531
- return `{ ${parts.join(', ')} }`;
532
- case 'parens':
533
- return `( ${parts.join(', ')} )`;
534
- default:
535
- return parts.join(', ');
536
- }
537
563
  };
538
564
 
539
565
  // ─── CoffeeScript Embed (for Prettier embed) ───────────────