@weborigami/language 0.0.73 → 0.1.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.
@@ -1,4 +1,4 @@
1
1
  Greetings:
2
2
 
3
- {{ @map(=`Hello, {{ _ }}.
3
+ {{ map(=`Hello, {{ _ }}.
4
4
  `)(names.yaml) }}
@@ -1,9 +1,9 @@
1
1
  import { ObjectTree } from "@weborigami/async-tree";
2
2
  import assert from "node:assert";
3
3
  import { describe, test } from "node:test";
4
- import { handleExtension } from "../../src/runtime/extensions.js";
4
+ import { handleExtension } from "../../src/runtime/handlers.js";
5
5
 
6
- describe("extensions", () => {
6
+ describe("handlers", () => {
7
7
  test("attaches an unpack method to a value with an extension", async () => {
8
8
  const fixture = createFixture();
9
9
  const numberValue = await fixture.get("foo");
@@ -11,6 +11,17 @@ describe("ops", () => {
11
11
  assert.deepEqual(result, [1, 2, 3]);
12
12
  });
13
13
 
14
+ test("ops.builtin gets a value from the top of the scope chain", async () => {
15
+ const root = new ObjectTree({
16
+ a: 1,
17
+ });
18
+ const tree = new ObjectTree({});
19
+ tree.parent = root;
20
+ const code = createCode([ops.builtin, "a"]);
21
+ const result = await evaluate.call(tree, code);
22
+ assert.equal(result, 1);
23
+ });
24
+
14
25
  test("ops.cache looks up a value in scope and memoizes it", async () => {
15
26
  let count = 0;
16
27
  const tree = new ObjectTree({
@@ -36,18 +47,6 @@ describe("ops", () => {
36
47
  assert.equal(result, "Hello, world.");
37
48
  });
38
49
 
39
- test("ops.constructor returns a constructor", async () => {
40
- const scope = new ObjectTree({
41
- "@js": {
42
- Number: Number,
43
- },
44
- });
45
- const fn = await ops.constructor.call(scope, "@js", "Number");
46
- const number = fn("1");
47
- assert(number instanceof Number);
48
- assert.equal(number, 1);
49
- });
50
-
51
50
  test("ops.inherited searches inherited scope", async () => {
52
51
  const parent = new ObjectTree({
53
52
  a: 1, // This is the inherited value we want
@@ -0,0 +1,21 @@
1
+ import assert from "node:assert";
2
+ import { describe, test } from "node:test";
3
+ import { isTypo, typos } from "../../src/runtime/typos.js";
4
+
5
+ describe("typos", () => {
6
+ test("isTypo", () => {
7
+ assert(isTypo("cat", "bat")); // substitution
8
+ assert(isTypo("cat", "cats")); // insertion
9
+ assert(isTypo("cat", "cast")); // insertion
10
+ assert(isTypo("cat", "at")); // deletion
11
+ assert(isTypo("cat", "ca")); // deletion
12
+ assert(isTypo("cat", "cta")); // transposition
13
+ assert(isTypo("cat", "act")); // transposition
14
+ assert(!isTypo("cat", "dog")); // more than 1 edit
15
+ });
16
+
17
+ test("typos", () => {
18
+ const result = typos("cas", ["ask", "cat", "cast", "cats", "cart"]);
19
+ assert.deepEqual(result, ["cat", "cast", "cats"]);
20
+ });
21
+ });
@@ -1,56 +0,0 @@
1
- // Text we look for in an error stack to guess whether a given line represents a
2
-
3
- import codeFragment from "./codeFragment.js";
4
-
5
- // function in the Origami source code.
6
- const origamiSourceSignals = [
7
- "async-tree/src/",
8
- "language/src/",
9
- "origami/src/",
10
- "at Scope.evaluate",
11
- ];
12
-
13
- /**
14
- * Format an error for display in the console.
15
- *
16
- * @param {Error} error
17
- */
18
- export default function formatError(error) {
19
- let message;
20
- if (error.stack) {
21
- // Display the stack only until we reach the Origami source code.
22
- message = "";
23
- let lines = error.stack.split("\n");
24
- for (let i = 0; i < lines.length; i++) {
25
- const line = lines[i];
26
- if (maybeOrigamiSourceCode(line)) {
27
- break;
28
- }
29
- if (message) {
30
- message += "\n";
31
- }
32
- message += lines[i];
33
- }
34
- } else {
35
- message = error.toString();
36
- }
37
-
38
- // Add location
39
- let location = /** @type {any} */ (error).location;
40
- if (location) {
41
- const fragment = codeFragment(location);
42
- let { source, start } = location;
43
-
44
- message += `\nevaluating: ${fragment}`;
45
- if (typeof source === "object" && source.url) {
46
- message += `\n at ${source.url.href}:${start.line}:${start.column}`;
47
- } else if (source.text.includes("\n")) {
48
- message += `\n at line ${start.line}, column ${start.column}`;
49
- }
50
- }
51
- return message;
52
- }
53
-
54
- export function maybeOrigamiSourceCode(text) {
55
- return origamiSourceSignals.some((signal) => text.includes(signal));
56
- }