coffeehaml 0.7.0 → 0.7.1

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.
Files changed (73) hide show
  1. package/README.md +21 -5
  2. package/dist/ast.js +334 -125
  3. package/dist/cli.js +363 -291
  4. package/dist/compiler.js +126 -89
  5. package/dist/emitter.js +771 -736
  6. package/dist/errors.js +5 -2
  7. package/dist/expressions.js +211 -154
  8. package/dist/index.js +22 -6
  9. package/dist/lexer.js +536 -340
  10. package/dist/parser.js +533 -438
  11. package/dist/prettier/coffeescript-formatter.js +292 -392
  12. package/dist/prettier/index.js +96 -60
  13. package/dist/prettier/options.js +179 -147
  14. package/dist/prettier/printer.js +549 -642
  15. package/dist/src/ast.js +338 -0
  16. package/dist/src/cli.js +430 -0
  17. package/dist/src/compiler.js +132 -0
  18. package/dist/src/emitter.js +791 -0
  19. package/dist/src/errors.js +5 -0
  20. package/dist/src/expressions.js +223 -0
  21. package/dist/src/index.js +22 -0
  22. package/dist/src/lexer.js +504 -0
  23. package/dist/src/parser.js +575 -0
  24. package/dist/src/types.js +122 -0
  25. package/dist/src/vite-plugin.js +78 -0
  26. package/dist/types.js +122 -12
  27. package/dist/vite-plugin.js +78 -63
  28. package/package.json +4 -9
  29. package/dist/ast.d.ts +0 -123
  30. package/dist/ast.d.ts.map +0 -1
  31. package/dist/ast.js.map +0 -1
  32. package/dist/cli.d.ts +0 -3
  33. package/dist/cli.d.ts.map +0 -1
  34. package/dist/cli.js.map +0 -1
  35. package/dist/compiler.d.ts +0 -10
  36. package/dist/compiler.d.ts.map +0 -1
  37. package/dist/compiler.js.map +0 -1
  38. package/dist/emitter.d.ts +0 -4
  39. package/dist/emitter.d.ts.map +0 -1
  40. package/dist/emitter.js.map +0 -1
  41. package/dist/errors.d.ts +0 -3
  42. package/dist/errors.d.ts.map +0 -1
  43. package/dist/errors.js.map +0 -1
  44. package/dist/expressions.d.ts +0 -22
  45. package/dist/expressions.d.ts.map +0 -1
  46. package/dist/expressions.js.map +0 -1
  47. package/dist/index.d.ts +0 -7
  48. package/dist/index.d.ts.map +0 -1
  49. package/dist/index.js.map +0 -1
  50. package/dist/lexer.d.ts +0 -30
  51. package/dist/lexer.d.ts.map +0 -1
  52. package/dist/lexer.js.map +0 -1
  53. package/dist/parser.d.ts +0 -10
  54. package/dist/parser.d.ts.map +0 -1
  55. package/dist/parser.js.map +0 -1
  56. package/dist/prettier/coffeescript-formatter.d.ts +0 -31
  57. package/dist/prettier/coffeescript-formatter.d.ts.map +0 -1
  58. package/dist/prettier/coffeescript-formatter.js.map +0 -1
  59. package/dist/prettier/index.d.ts +0 -8
  60. package/dist/prettier/index.d.ts.map +0 -1
  61. package/dist/prettier/index.js.map +0 -1
  62. package/dist/prettier/options.d.ts +0 -10
  63. package/dist/prettier/options.d.ts.map +0 -1
  64. package/dist/prettier/options.js.map +0 -1
  65. package/dist/prettier/printer.d.ts +0 -45
  66. package/dist/prettier/printer.d.ts.map +0 -1
  67. package/dist/prettier/printer.js.map +0 -1
  68. package/dist/types.d.ts +0 -76
  69. package/dist/types.d.ts.map +0 -1
  70. package/dist/types.js.map +0 -1
  71. package/dist/vite-plugin.d.ts +0 -7
  72. package/dist/vite-plugin.d.ts.map +0 -1
  73. package/dist/vite-plugin.js.map +0 -1
package/dist/errors.js CHANGED
@@ -1,2 +1,5 @@
1
- export { CompileError } from './types.js';
2
- //# sourceMappingURL=errors.js.map
1
+ // Generated by CoffeeScript 2.7.0
2
+ // errors.coffee — re-exports for convenience
3
+ export {
4
+ CompileError
5
+ } from './types.js';
@@ -1,166 +1,223 @@
1
- import { createRequire } from 'module';
2
- import { Expression } from './ast.js';
3
- import { CompileError } from './types.js';
4
- // ESM-safe CoffeeScript loader — uses createRequire for projects with "type": "module"
5
- const _require = createRequire(import.meta.url);
6
- function loadCoffeeScript() {
7
- try {
8
- return _require('coffeescript');
1
+ // Generated by CoffeeScript 2.7.0
2
+ // expressions.coffee — CoffeeScript expression compilation bridge
3
+ var _coffeeScriptAvailable, _coffeeScriptUnavailableReason, _require, checkCoffeeScript, loadCoffeeScript, stripCoffeeWrapper, stripToConst;
4
+
5
+ import {
6
+ createRequire
7
+ } from 'module';
8
+
9
+ import {
10
+ Expression
11
+ } from './ast.js';
12
+
13
+ import {
14
+ CompileError
15
+ } from './types.js';
16
+
17
+ // ESM-safe CoffeeScript loader
18
+ _require = createRequire(import.meta.url);
19
+
20
+ loadCoffeeScript = function() {
21
+ try {
22
+ return _require('coffeescript');
23
+ } catch (error) {
24
+ return null;
25
+ }
26
+ };
27
+
28
+ // ─── Availability check ────────────────────────────────────
29
+ _coffeeScriptAvailable = null;
30
+
31
+ _coffeeScriptUnavailableReason = '';
32
+
33
+ checkCoffeeScript = function() {
34
+ var cs, e;
35
+ if (_coffeeScriptAvailable !== null) {
36
+ return _coffeeScriptAvailable;
37
+ }
38
+ try {
39
+ cs = loadCoffeeScript();
40
+ if (cs && typeof cs.compile === 'function') {
41
+ _coffeeScriptAvailable = true;
42
+ return true;
9
43
  }
10
- catch {
11
- return null;
44
+ _coffeeScriptAvailable = false;
45
+ _coffeeScriptUnavailableReason = 'CoffeeScript module loaded but has no compile() function';
46
+ return false;
47
+ } catch (error) {
48
+ e = error;
49
+ _coffeeScriptAvailable = false;
50
+ _coffeeScriptUnavailableReason = `CoffeeScript unavailable: ${e instanceof Error ? e.message : String(e)}`;
51
+ return false;
52
+ }
53
+ };
54
+
55
+ export var isCoffeeScriptAvailable = function() {
56
+ return checkCoffeeScript();
57
+ };
58
+
59
+ export var getCoffeeScriptUnavailableReason = function() {
60
+ checkCoffeeScript();
61
+ return _coffeeScriptUnavailableReason;
62
+ };
63
+
64
+ // ─── Parse expression ──────────────────────────────────────
65
+ export var parseExpression = function(source) {
66
+ var CoffeeScript, expr;
67
+ expr = new Expression(source);
68
+ try {
69
+ CoffeeScript = loadCoffeeScript();
70
+ if (CoffeeScript && typeof CoffeeScript.parse === 'function') {
71
+ expr.parsed = CoffeeScript.parse(source, {
72
+ bare: true
73
+ });
12
74
  }
13
- }
14
- /** Track CoffeeScript availability and warnings. */
15
- let _coffeeScriptAvailable = null;
16
- let _coffeeScriptUnavailableReason = '';
17
- function checkCoffeeScript() {
18
- if (_coffeeScriptAvailable !== null)
19
- return _coffeeScriptAvailable;
20
- try {
21
- const cs = loadCoffeeScript();
22
- if (cs && typeof cs.compile === 'function') {
23
- _coffeeScriptAvailable = true;
24
- return true;
25
- }
26
- _coffeeScriptAvailable = false;
27
- _coffeeScriptUnavailableReason = 'CoffeeScript module loaded but has no compile() function';
28
- return false;
29
- }
30
- catch (e) {
31
- _coffeeScriptAvailable = false;
32
- _coffeeScriptUnavailableReason = `CoffeeScript unavailable: ${e instanceof Error ? e.message : String(e)}`;
33
- return false;
75
+ } catch (error) {
76
+
77
+ }
78
+ // Parse error — store raw source
79
+ return expr;
80
+ };
81
+
82
+ // ─── Compile expression to JS ──────────────────────────────
83
+ export var compileExpression = function(expr, location = null) {
84
+ var CoffeeScript, e, js, msg;
85
+ if (expr.source.trim() === '') {
86
+ return '';
87
+ }
88
+ try {
89
+ CoffeeScript = loadCoffeeScript();
90
+ if (CoffeeScript && typeof CoffeeScript.compile === 'function') {
91
+ js = CoffeeScript.compile(expr.source, {
92
+ bare: true,
93
+ inlineMap: false
94
+ });
95
+ return stripCoffeeWrapper(js, expr.source);
34
96
  }
35
- }
36
- /** Returns true if CoffeeScript is available for expression compilation. */
37
- export function isCoffeeScriptAvailable() {
38
- return checkCoffeeScript();
39
- }
40
- /** Get a human-readable reason if CoffeeScript is not available. */
41
- export function getCoffeeScriptUnavailableReason() {
42
- checkCoffeeScript(); // ensure initialized
43
- return _coffeeScriptUnavailableReason;
44
- }
45
- /** Parse a CoffeeScript expression string into its AST representation.
46
- * Uses the host's CoffeeScript installation. Returns null gracefully
47
- * if CoffeeScript is unavailable or parsing fails. */
48
- export function parseExpression(source) {
49
- const expr = new Expression(source);
50
- try {
51
- const CoffeeScript = loadCoffeeScript();
52
- if (CoffeeScript && typeof CoffeeScript.parse === 'function') {
53
- expr.parsed = CoffeeScript.parse(source, { bare: true });
54
- }
97
+ } catch (error) {
98
+ e = error;
99
+ msg = e instanceof Error ? e.message : String(e);
100
+ if (location) {
101
+ throw new CompileError(`CoffeeScript expression error: ${msg}`, 'emitter', 'EXPRESSION_ERROR', location, `Check expression: ${expr.source.slice(0, 80)}`);
55
102
  }
56
- catch {
57
- // Parse error — store raw source, let emitter handle it
103
+ }
104
+ return expr.source;
105
+ };
106
+
107
+ // ─── Strip CoffeeScript wrapper ────────────────────────────
108
+ stripCoffeeWrapper = function(js, _original) {
109
+ var body, l, lines, meaningful, result, varLines;
110
+ result = js.trim();
111
+ result = result.replace(/;\s*$/, '');
112
+ lines = (function() {
113
+ var i, len, ref, results;
114
+ ref = result.split('\n');
115
+ results = [];
116
+ for (i = 0, len = ref.length; i < len; i++) {
117
+ l = ref[i];
118
+ if (l.trim() !== '') {
119
+ results.push(l);
120
+ }
58
121
  }
59
- return expr;
60
- }
61
- /** Compile a CoffeeScript expression to JavaScript.
62
- * If no parsed AST, compiles from source directly.
63
- * Returns the source as-is if compilation fails.
64
- * @param location Source location for error context. */
65
- export function compileExpression(expr, location) {
66
- if (expr.source.trim() === '')
67
- return '';
68
- try {
69
- const CoffeeScript = loadCoffeeScript();
70
- if (CoffeeScript && typeof CoffeeScript.compile === 'function') {
71
- const js = CoffeeScript.compile(expr.source, {
72
- bare: true,
73
- inlineMap: false,
74
- });
75
- // CoffeeScript.compile wraps result in an IIFE or adds var — strip to get expression
76
- return stripCoffeeWrapper(js, expr.source);
122
+ return results;
123
+ })();
124
+ if (lines.length > 0) {
125
+ varLines = (function() {
126
+ var i, len, results;
127
+ results = [];
128
+ for (i = 0, len = lines.length; i < len; i++) {
129
+ l = lines[i];
130
+ if (/^\s*var\b/.test(l)) {
131
+ results.push(l);
77
132
  }
78
- }
79
- catch (e) {
80
- const msg = e instanceof Error ? e.message : String(e);
81
- if (location) {
82
- throw new CompileError(`CoffeeScript expression error: ${msg}`, 'emitter', 'EXPRESSION_ERROR', location, `Check expression: ${expr.source.slice(0, 80)}`);
133
+ }
134
+ return results;
135
+ })();
136
+ meaningful = (function() {
137
+ var i, len, results;
138
+ results = [];
139
+ for (i = 0, len = lines.length; i < len; i++) {
140
+ l = lines[i];
141
+ if (!/^\s*var\b/.test(l)) {
142
+ results.push(l);
83
143
  }
84
- // Without location, return raw source with warning
144
+ }
145
+ return results;
146
+ })();
147
+ if (meaningful.length > 0) {
148
+ if (varLines.length > 0) {
149
+ body = meaningful.join(' ');
150
+ return `(() => { ${varLines.join(' ')} return ${body}; })()`;
151
+ }
152
+ return meaningful.join('\n');
85
153
  }
86
- return expr.source;
87
- }
88
- /** Strip CoffeeScript's wrapper to get a clean expression.
89
- * CoffeeScript.compile('x + 1', {bare: true}) → "var x;\n\nx + 1;\n"
90
- * We want just "x + 1".
91
- *
92
- * When CoffeeScript generates var declarations (e.g. for ?. operator:
93
- * "var ref;\n(ref = a) != null ? ref.b : void 0"), stripping them leaves
94
- * undeclared refs. We wrap in an IIFE to scope the vars locally. */
95
- function stripCoffeeWrapper(js, _original) {
96
- let result = js.trim();
97
- // Remove trailing semicolon and whitespace
98
- result = result.replace(/;\s*$/, '');
99
- // If result is multiline, take the last significant line
100
- const lines = result.split('\n').filter(l => l.trim() !== '');
101
- if (lines.length > 0) {
102
- const varLines = lines.filter(l => /^\s*var\b/.test(l));
103
- const meaningful = lines.filter(l => !/^\s*var\b/.test(l));
104
- if (meaningful.length > 0) {
105
- if (varLines.length > 0) {
106
- // Var declarations stripped — wrap in IIFE to keep refs scoped
107
- const body = meaningful.join(' ');
108
- return `(() => { ${varLines.join(' ')} return ${body}; })()`;
109
- }
110
- return meaningful.join('\n');
111
- }
112
- return lines[lines.length - 1];
154
+ return lines[lines.length - 1];
155
+ }
156
+ return result;
157
+ };
158
+
159
+ // ─── Compile statement to JS ───────────────────────────────
160
+ export var compileStatement = function(source, location = null) {
161
+ var CoffeeScript, e, js, msg;
162
+ try {
163
+ CoffeeScript = loadCoffeeScript();
164
+ if (CoffeeScript && typeof CoffeeScript.compile === 'function') {
165
+ js = CoffeeScript.compile(source, {
166
+ bare: true,
167
+ inlineMap: false
168
+ });
169
+ return stripToConst(js.trim());
113
170
  }
114
- return result;
115
- }
116
- /** Compile a CoffeeScript statement, preserving variable declarations.
117
- * CoffeeScript.compile('sim = useSimulation()', {bare: true}) → "var sim;\n\nsim = useSimulation();\n"
118
- * We want: "const sim = useSimulation();"
119
- * Multi-var: "var a, b; a = 1; b = 2;" → "let a = 1; let b = 2;"
120
- * @param location Source location for error context. */
121
- export function compileStatement(source, location) {
122
- try {
123
- const CoffeeScript = loadCoffeeScript();
124
- if (CoffeeScript && typeof CoffeeScript.compile === 'function') {
125
- const js = CoffeeScript.compile(source, {
126
- bare: true,
127
- inlineMap: false,
128
- });
129
- return stripToConst(js.trim());
130
- }
171
+ } catch (error) {
172
+ e = error;
173
+ msg = e instanceof Error ? e.message : String(e);
174
+ if (location) {
175
+ throw new CompileError(`CoffeeScript compilation error: ${msg}`, 'emitter', 'COMPILE_ERROR', location, `Check statement: ${source.slice(0, 80)}`);
131
176
  }
132
- catch (e) {
133
- const msg = e instanceof Error ? e.message : String(e);
134
- if (location) {
135
- throw new CompileError(`CoffeeScript compilation error: ${msg}`, 'emitter', 'COMPILE_ERROR', location, `Check statement: ${source.slice(0, 80)}`);
136
- }
137
- // Without location, fall through to raw passthrough
177
+ }
178
+ return source;
179
+ };
180
+
181
+ // ─── Convert var → const/let ───────────────────────────────
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
+ }
138
193
  }
139
- return source;
140
- }
141
- /** Convert CoffeeScript var declarations to const/let statements. */
142
- function stripToConst(js) {
143
- const lines = js.split('\n').filter(l => l.trim() !== '');
144
- // Pattern: "var x, y;" followed by assignment lines
145
- const varLine = lines.find(l => /^\s*var\b/.test(l));
146
- if (!varLine)
147
- return js.replace(/;\s*$/, '');
148
- // Extract var names
149
- const varNames = varLine.replace(/^\s*var\s+/, '').replace(/;\s*$/, '').split(',').map(s => s.trim()).filter(Boolean);
150
- // Find assignment lines
151
- const assignments = [];
152
- for (const name of varNames) {
153
- const assignLine = lines.find(l => {
154
- const trimmed = l.trim();
155
- return trimmed.startsWith(`${name} =`) || trimmed.startsWith(`${name}=`);
156
- });
157
- if (assignLine) {
158
- assignments.push('const ' + assignLine.trim().replace(/;\s*$/, ''));
159
- }
194
+ return results;
195
+ })();
196
+ varLine = lines.find(function(l) {
197
+ return /^\s*var\b/.test(l);
198
+ });
199
+ if (!varLine) {
200
+ return js.replace(/;\s*$/, '');
201
+ }
202
+ varNames = varLine.replace(/^\s*var\s+/, '').replace(/;\s*$/, '').split(',').map(function(s) {
203
+ return s.trim();
204
+ }).filter(Boolean);
205
+ assignments = [];
206
+ for (i = 0, len = varNames.length; i < len; i++) {
207
+ name = varNames[i];
208
+ assignLine = lines.find(function(l) {
209
+ var t;
210
+ t = l.trim();
211
+ return t.startsWith(`${name} =`) || t.startsWith(`${name}=`);
212
+ });
213
+ if (assignLine) {
214
+ assignments.push('const ' + assignLine.trim().replace(/;\s*$/, ''));
160
215
  }
161
- if (assignments.length > 0)
162
- return assignments.join('; ');
163
- // Fallback: keep var line, drop empty lines
164
- return lines.filter(l => !/^\s*$/.test(l)).join(' ').replace(/;\s*$/, '');
165
- }
166
- //# sourceMappingURL=expressions.js.map
216
+ }
217
+ if (assignments.length > 0) {
218
+ return assignments.join('; ');
219
+ }
220
+ return lines.filter(function(l) {
221
+ return !/^\s*$/.test(l);
222
+ }).join(' ').replace(/;\s*$/, '');
223
+ };
package/dist/index.js CHANGED
@@ -1,6 +1,22 @@
1
- export { compile, compileFile, resolveCoffeeHamlFile, COFFEEHAML_EXTENSIONS } from './compiler.js';
2
- export { isCoffeeScriptAvailable } from './expressions.js';
3
- export { CompileError } from './types.js';
4
- // Prettier plugin (imported programmatically, or auto-discovered by Prettier)
5
- export { default as prettierPlugin } from './prettier/index.js';
6
- //# sourceMappingURL=index.js.map
1
+ // Generated by CoffeeScript 2.7.0
2
+ // index.coffee — public API
3
+ export {
4
+ compile,
5
+ compileFile,
6
+ resolveCoffeeHamlFile,
7
+ COFFEEHAML_EXTENSIONS
8
+ } from './compiler.js';
9
+
10
+ export {
11
+ isCoffeeScriptAvailable
12
+ } from './expressions.js';
13
+
14
+ export {
15
+ CompileError,
16
+ CompileWarning
17
+ } from './types.js';
18
+
19
+ export {
20
+ // Prettier plugin
21
+ default as prettierPlugin
22
+ } from './prettier/index.js';