@danielx/civet 0.10.6 → 0.11.0

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.
@@ -17,7 +17,7 @@ try {
17
17
  break;
18
18
  }
19
19
  default: {
20
- throw `Unknown operation: ${op}`;
20
+ throw new Error(`Unknown operation: ${op}`);
21
21
  }
22
22
  }
23
23
  return parentPort.postMessage({ id, result, errors: args[1]?.errors });
package/dist/types.d.ts CHANGED
@@ -26,6 +26,13 @@ declare module "@danielx/civet" {
26
26
  implicitReturns: boolean
27
27
  jsxCode: boolean
28
28
  objectIs: boolean
29
+ /** Array of names to treat as operators, or object mapping names to
30
+ * parsable operator behaviors such as "relational" or "same (+)" or
31
+ * "relational same (+)", or ""/undefined for default behavior.
32
+ * (Can also map to OperatorBehavior as defined in source/types.civet,
33
+ * but the details are subject to change.)
34
+ */
35
+ operators: string[] | Record<string, string | undefined>
29
36
  react: boolean
30
37
  solid: boolean
31
38
  client: boolean
@@ -41,25 +48,92 @@ declare module "@danielx/civet" {
41
48
  repl: boolean
42
49
  }>
43
50
  export type CompileOptions = {
51
+ /**
52
+ * If your Civet code comes from a file, provide it here. This gets used
53
+ * in sourcemaps and error messages.
54
+ */
44
55
  filename?: string
56
+ /**
57
+ * Whether to return a source map in addition to transpiled code.
58
+ * If false (the default), `compile` just returns transpiled code.
59
+ * If true (and `inlineMap` is false/unspecified),
60
+ * `compile` returns an object `{code, sourceMap}` whose `code` property
61
+ * is transpiled code and `sourceMap` property is a `SourceMap` object.
62
+ */
45
63
  sourceMap?: boolean
64
+ /**
65
+ * Whether to inline a source map as a final comment in the transpiled code.
66
+ * Default is false.
67
+ */
46
68
  inlineMap?: boolean
69
+ /**
70
+ * Whether to return an AST of the parsed code instead of transpiled code.
71
+ * Default is false.
72
+ * If true, `compile` skips the `generate` step that turns the parsed AST
73
+ * into a code string, and just returns the AST itself.
74
+ * If "raw", `compile` also skips the `prune` step, which leaves some
75
+ * extra properties on the AST nodes (e.g. `parent` pointers) and
76
+ * preserves that `children` is always an array.
77
+ */
47
78
  ast?: boolean | "raw"
79
+ /**
80
+ * Whether Civet should convert TypeScript syntax to JavaScript.
81
+ * This mostly triggers the removal of type annotations, but some
82
+ * TypeScript features such as `enum` are also supported.
83
+ * Default is false.
84
+ */
48
85
  js?: boolean
86
+ /**
87
+ * If set to true, turns off the compiler cache of compiled subexpressions.
88
+ * This should not affect the compilation output,
89
+ * and can make the compiler exponentially slow.
90
+ * It is mainly for testing whether there is a bug in the compiler cache.
91
+ */
49
92
  noCache?: boolean
93
+ /**
94
+ * If specified, also writes data about compiler cache performance
95
+ * into the specified filename. Useful for debugging caching performance.
96
+ */
50
97
  hits?: string
98
+ /**
99
+ * If specified, also writes data about all parse branches considered by
100
+ * the compiler into the specified filename.
101
+ * Useful for debugging why something parsed the way it did.
102
+ */
51
103
  trace?: string
104
+ /**
105
+ * Initial parse options, e.g., read from a config file.
106
+ * They can still be overridden in the code by "civet" pragmas.
107
+ */
52
108
  parseOptions?: ParseOptions
53
- /** Specifying an empty array will prevent ParseErrors from being thrown */
109
+ /**
110
+ * By default, `compile` will throw a `ParseErrors` containing all
111
+ * `ParseError`s encountered during compilation.
112
+ * If you specify an empty array, `compile` will not throw and instead
113
+ * will add to the array all `ParseError`s encountered.
114
+ */
54
115
  errors?: ParseError[]
55
- /** Number of parallel threads to compile with (Node only) */
116
+ /**
117
+ * Number of parallel threads to compile with (Node only).
118
+ * Default is to use the environment variable `CIVET_THREADS`, or 0.
119
+ * If nonzero, spawns up to that many worker threads so that multiple
120
+ * calls to `compile` will end up running in parallel.
121
+ * If `CIVET_THREADS` is set to 0, the `threads` option is ignored.
122
+ */
56
123
  threads?: number
124
+ /**
125
+ * If false (the default), runs the compiler asynchronously and returns
126
+ * a Promise (for the transpiled string or `{code, sourceMap}` object).
127
+ * If true, runs the compiler synchronously and returns the result directly.
128
+ * Sync mode disables some features:
129
+ * - parallel computation via `threads`
130
+ * - comptime code can't return promises
131
+ */
132
+ sync?: boolean
57
133
  }
58
134
  export type GenerateOptions = Omit<CompileOptions, "sourceMap"> & {
59
135
  sourceMap?: undefined | SourceMap
60
136
  }
61
- export type SyncCompileOptions = CompileOptions &
62
- { parseOptions?: { comptime?: false } }
63
137
 
64
138
  export type SourceMapping = [number] | [number, number, number, number]
65
139
 
@@ -74,7 +148,7 @@ declare module "@danielx/civet" {
74
148
  }
75
149
 
76
150
  // TODO: Import ParseError class from Hera
77
- export type ParseError = {
151
+ export class ParseError {
78
152
  name: "ParseError"
79
153
  message: string // filename:line:column header\nbody
80
154
  header: string
@@ -84,7 +158,8 @@ declare module "@danielx/civet" {
84
158
  column: number | string
85
159
  offset: number
86
160
  }
87
- export type ParseErrors = {
161
+ export class ParseErrors {
162
+ constructor(errors: ParseError[])
88
163
  name: "ParseErrors"
89
164
  message: string
90
165
  errors: ParseError[]
@@ -97,7 +172,7 @@ declare module "@danielx/civet" {
97
172
  code: string,
98
173
  sourceMap: SourceMap,
99
174
  } : string
100
- export function compile<const T extends CompileOptions>(source: string, options?: T):
175
+ export function compile<const T extends CompileOptions>(source: string | Buffer, options?: T):
101
176
  T extends { sync: true } ? CompileOutput<T> : Promise<CompileOutput<T>>
102
177
  /** Warning: No caching */
103
178
  export function parse(source: string, options?: CompileOptions & {startRule?: string}): CivetAST
@@ -105,6 +180,7 @@ declare module "@danielx/civet" {
105
180
  export function parseProgram<T extends CompileOptions>(source: string, options?: T):
106
181
  T extends { comptime: true } ? Promise<CivetAST> : CivetAST
107
182
  export function generate(ast: CivetAST, options?: GenerateOptions): string
183
+ export function decode(source: string | Buffer): string
108
184
 
109
185
  export const lib: {
110
186
  gatherRecursive(ast: CivetAST, predicate: (node: CivetAST) => boolean): CivetAST[]
@@ -117,6 +193,7 @@ declare module "@danielx/civet" {
117
193
  isCompileError: typeof isCompileError
118
194
  parse: typeof parse
119
195
  generate: typeof generate
196
+ decode: typeof decode
120
197
  SourceMap: typeof SourceMap
121
198
  ParseError: typeof ParseError
122
199
  ParseErrors: typeof ParseErrors
@@ -150,9 +227,10 @@ declare module "@danielx/civet/config" {
150
227
  export function loadConfig(
151
228
  path: string
152
229
  ): Promise<import("@danielx/civet").CompileOptions>
153
- export default {
154
- findInDir,
155
- findConfig,
156
- loadConfig,
230
+ const Config: {
231
+ findInDir: typeof findInDir,
232
+ findConfig: typeof findConfig,
233
+ loadConfig: typeof loadConfig,
157
234
  }
235
+ export default Config
158
236
  }
@@ -265,9 +265,7 @@ var rawPlugin = (options = {}, meta) => {
265
265
  if (!filename.endsWith(".civet.tsx")) return systemReadFile(filename, encoding);
266
266
  if (fsMap.has(filename)) return fsMap.get(filename);
267
267
  const civetFilename = filename.slice(0, -4);
268
- const rawCivetSource = fs.readFileSync(civetFilename, {
269
- encoding
270
- });
268
+ const rawCivetSource = fs.readFileSync(civetFilename, { encoding });
271
269
  const { code: compiledTS, sourceMap } = import_civet.default.compile(rawCivetSource, {
272
270
  ...tsCompileOptions,
273
271
  filename,
@@ -465,7 +463,7 @@ var rawPlugin = (options = {}, meta) => {
465
463
  filename: id,
466
464
  errors: []
467
465
  };
468
- const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
466
+ const rawCivetSource = (0, import_civet.decode)(await fs.promises.readFile(filename));
469
467
  const ast = await import_civet.default.compile(rawCivetSource, {
470
468
  ...civetOptions,
471
469
  ast: true
@@ -1,6 +1,6 @@
1
1
  // unplugin-civet:C:\Users\edemaine\Projects\Civet\source\unplugin\unplugin.civet.jsx
2
2
  import { createUnplugin } from "unplugin";
3
- import civet, { lib, SourceMap } from "@danielx/civet";
3
+ import civet, { decode, lib, SourceMap } from "@danielx/civet";
4
4
  import { findInDir, loadConfig } from "@danielx/civet/config";
5
5
  import {
6
6
  remapRange,
@@ -233,9 +233,7 @@ var rawPlugin = (options = {}, meta) => {
233
233
  if (!filename.endsWith(".civet.tsx")) return systemReadFile(filename, encoding);
234
234
  if (fsMap.has(filename)) return fsMap.get(filename);
235
235
  const civetFilename = filename.slice(0, -4);
236
- const rawCivetSource = fs.readFileSync(civetFilename, {
237
- encoding
238
- });
236
+ const rawCivetSource = fs.readFileSync(civetFilename, { encoding });
239
237
  const { code: compiledTS, sourceMap } = civet.compile(rawCivetSource, {
240
238
  ...tsCompileOptions,
241
239
  filename,
@@ -433,7 +431,7 @@ var rawPlugin = (options = {}, meta) => {
433
431
  filename: id,
434
432
  errors: []
435
433
  };
436
- const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
434
+ const rawCivetSource = decode(await fs.promises.readFile(filename));
437
435
  const ast = await civet.compile(rawCivetSource, {
438
436
  ...civetOptions,
439
437
  ast: true
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.10.6",
4
+ "version": "0.11.0",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",
@@ -112,7 +112,7 @@
112
112
  },
113
113
  "devDependencies": {
114
114
  "@danielx/civet": "0.9.4",
115
- "@danielx/hera": "^0.8.16",
115
+ "@danielx/hera": "^0.8.17",
116
116
  "@prettier/sync": "^0.5.2",
117
117
  "@types/assert": "^1.5.6",
118
118
  "@types/mocha": "^10.0.8",
package/register.js CHANGED
@@ -58,7 +58,7 @@ const { compile } = require("./");
58
58
  // Old-style CJS registration
59
59
  if (require.extensions) {
60
60
  require.extensions[".civet"] = function (module, filename) {
61
- const js = compile(fs.readFileSync(filename, 'utf8'), {
61
+ const js = compile(fs.readFileSync(filename), {
62
62
  filename,
63
63
  js: true,
64
64
  inlineMap: true,
@@ -83,7 +83,7 @@ function retrieveFile(path) {
83
83
  }
84
84
 
85
85
  if (!outputCache.has(path)) {
86
- outputCache.set(path, compile(fs.readFileSync(path, 'utf8'), {
86
+ outputCache.set(path, compile(fs.readFileSync(path), {
87
87
  filename: path,
88
88
  js: true,
89
89
  inlineMap: true,