ic-mops 2.0.1 → 2.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.
Files changed (176) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/RELEASE.md +179 -0
  3. package/bundle/cli.tgz +0 -0
  4. package/check-requirements.ts +3 -8
  5. package/cli.ts +79 -11
  6. package/commands/bench/bench-canister.mo +17 -6
  7. package/commands/bench.ts +2 -13
  8. package/commands/build.ts +2 -4
  9. package/commands/check.ts +117 -0
  10. package/commands/format.ts +3 -18
  11. package/commands/lint.ts +92 -0
  12. package/commands/sync.ts +2 -8
  13. package/commands/test/test.ts +7 -19
  14. package/commands/toolchain/index.ts +21 -8
  15. package/commands/toolchain/lintoko.ts +54 -0
  16. package/commands/toolchain/toolchain-utils.ts +2 -0
  17. package/constants.ts +23 -0
  18. package/dist/check-requirements.js +3 -8
  19. package/dist/cli.js +60 -10
  20. package/dist/commands/bench/bench-canister.mo +17 -6
  21. package/dist/commands/bench.js +2 -11
  22. package/dist/commands/build.js +2 -4
  23. package/dist/commands/check.d.ts +6 -0
  24. package/dist/commands/check.js +78 -0
  25. package/dist/commands/format.js +3 -16
  26. package/dist/commands/lint.d.ts +7 -0
  27. package/dist/commands/lint.js +69 -0
  28. package/dist/commands/sync.js +2 -7
  29. package/dist/commands/test/test.js +7 -17
  30. package/dist/commands/toolchain/index.d.ts +2 -2
  31. package/dist/commands/toolchain/index.js +18 -7
  32. package/dist/commands/toolchain/lintoko.d.ts +8 -0
  33. package/dist/commands/toolchain/lintoko.js +36 -0
  34. package/dist/commands/toolchain/toolchain-utils.d.ts +1 -0
  35. package/dist/commands/toolchain/toolchain-utils.js +1 -0
  36. package/dist/constants.d.ts +15 -0
  37. package/dist/constants.js +21 -0
  38. package/dist/environments/nodejs/cli.js +6 -1
  39. package/dist/helpers/autofix-motoko.d.ts +26 -0
  40. package/dist/helpers/autofix-motoko.js +105 -0
  41. package/dist/helpers/get-moc-version.d.ts +2 -0
  42. package/dist/helpers/get-moc-version.js +10 -1
  43. package/dist/mops.js +2 -1
  44. package/dist/package.json +3 -2
  45. package/dist/tests/build-no-dfx.test.d.ts +1 -0
  46. package/dist/tests/build-no-dfx.test.js +9 -0
  47. package/dist/tests/build.test.d.ts +1 -0
  48. package/dist/tests/build.test.js +18 -0
  49. package/dist/tests/check-candid.test.d.ts +1 -0
  50. package/dist/tests/check-candid.test.js +20 -0
  51. package/dist/tests/check-fix.test.d.ts +1 -0
  52. package/dist/tests/check-fix.test.js +73 -0
  53. package/dist/tests/check.test.d.ts +1 -0
  54. package/dist/tests/check.test.js +33 -0
  55. package/dist/tests/cli.test.js +4 -57
  56. package/dist/tests/helpers.d.ts +22 -0
  57. package/dist/tests/helpers.js +43 -0
  58. package/dist/tests/lint.test.d.ts +1 -0
  59. package/dist/tests/lint.test.js +15 -0
  60. package/dist/tests/toolchain.test.d.ts +1 -0
  61. package/dist/tests/toolchain.test.js +11 -0
  62. package/dist/types.d.ts +5 -1
  63. package/dist/wasm/pkg/nodejs/wasm_bg.wasm +0 -0
  64. package/dist/wasm/pkg/web/wasm_bg.wasm +0 -0
  65. package/environments/nodejs/cli.ts +7 -1
  66. package/helpers/autofix-motoko.ts +170 -0
  67. package/helpers/get-moc-version.ts +12 -1
  68. package/mops.ts +2 -1
  69. package/package.json +3 -2
  70. package/tests/__snapshots__/build-no-dfx.test.ts.snap +11 -0
  71. package/tests/__snapshots__/build.test.ts.snap +77 -0
  72. package/tests/__snapshots__/check-candid.test.ts.snap +73 -0
  73. package/tests/__snapshots__/check-fix.test.ts.snap +242 -0
  74. package/tests/__snapshots__/check.test.ts.snap +72 -0
  75. package/tests/__snapshots__/lint.test.ts.snap +78 -0
  76. package/tests/build/no-dfx/mops.toml +5 -0
  77. package/tests/build/no-dfx/src/Main.mo +5 -0
  78. package/tests/build-no-dfx.test.ts +10 -0
  79. package/tests/build.test.ts +24 -0
  80. package/tests/check/error/Error.mo +7 -0
  81. package/tests/check/error/mops.toml +2 -0
  82. package/tests/check/fix/M0223.mo +11 -0
  83. package/tests/check/fix/M0236.mo +11 -0
  84. package/tests/check/fix/M0237.mo +11 -0
  85. package/tests/check/fix/Ok.mo +7 -0
  86. package/tests/check/fix/edit-suggestions.mo +143 -0
  87. package/tests/check/fix/mops.toml +5 -0
  88. package/tests/check/fix/transitive-lib.mo +9 -0
  89. package/tests/check/fix/transitive-main.mo +9 -0
  90. package/tests/check/success/Ok.mo +5 -0
  91. package/tests/check/success/Warning.mo +5 -0
  92. package/tests/check/success/mops.toml +2 -0
  93. package/tests/check-candid.test.ts +22 -0
  94. package/tests/check-fix.test.ts +111 -0
  95. package/tests/check.test.ts +46 -0
  96. package/tests/cli.test.ts +4 -74
  97. package/tests/helpers.ts +58 -0
  98. package/tests/lint/lints/no-bool-switch.toml +9 -0
  99. package/tests/lint/mops.toml +4 -0
  100. package/tests/lint/src/NoBoolSwitch.mo +8 -0
  101. package/tests/lint/src/Ok.mo +5 -0
  102. package/tests/lint.test.ts +17 -0
  103. package/tests/toolchain/mock +2 -0
  104. package/tests/toolchain/mops.toml +2 -0
  105. package/tests/toolchain.test.ts +12 -0
  106. package/types.ts +5 -1
  107. package/wasm/Cargo.lock +101 -54
  108. package/wasm/pkg/nodejs/wasm_bg.wasm +0 -0
  109. package/wasm/pkg/web/wasm_bg.wasm +0 -0
  110. package/.DS_Store +0 -0
  111. package/bundle/bench/bench-canister.mo +0 -121
  112. package/bundle/bench/user-bench.mo +0 -10
  113. package/bundle/bin/moc-wrapper.sh +0 -40
  114. package/bundle/bin/mops.js +0 -3
  115. package/bundle/cli.js +0 -2144
  116. package/bundle/declarations/bench/bench.did +0 -30
  117. package/bundle/declarations/bench/bench.did.d.ts +0 -33
  118. package/bundle/declarations/bench/bench.did.js +0 -30
  119. package/bundle/declarations/bench/index.d.ts +0 -50
  120. package/bundle/declarations/bench/index.js +0 -40
  121. package/bundle/declarations/main/index.d.ts +0 -50
  122. package/bundle/declarations/main/index.js +0 -40
  123. package/bundle/declarations/main/main.did +0 -428
  124. package/bundle/declarations/main/main.did.d.ts +0 -348
  125. package/bundle/declarations/main/main.did.js +0 -406
  126. package/bundle/declarations/storage/index.d.ts +0 -50
  127. package/bundle/declarations/storage/index.js +0 -30
  128. package/bundle/declarations/storage/storage.did +0 -46
  129. package/bundle/declarations/storage/storage.did.d.ts +0 -40
  130. package/bundle/declarations/storage/storage.did.js +0 -38
  131. package/bundle/package.json +0 -36
  132. package/bundle/templates/README.md +0 -13
  133. package/bundle/templates/licenses/Apache-2.0 +0 -202
  134. package/bundle/templates/licenses/Apache-2.0-NOTICE +0 -13
  135. package/bundle/templates/licenses/MIT +0 -21
  136. package/bundle/templates/mops-publish.yml +0 -17
  137. package/bundle/templates/mops-test.yml +0 -24
  138. package/bundle/templates/src/lib.mo +0 -15
  139. package/bundle/templates/test/lib.test.mo +0 -4
  140. package/bundle/wasm_bg.wasm +0 -0
  141. package/bundle/xhr-sync-worker.js +0 -59
  142. package/dist/wasm/pkg/bundler/package.json +0 -20
  143. package/dist/wasm/pkg/bundler/wasm.d.ts +0 -3
  144. package/dist/wasm/pkg/bundler/wasm.js +0 -5
  145. package/dist/wasm/pkg/bundler/wasm_bg.js +0 -93
  146. package/dist/wasm/pkg/bundler/wasm_bg.wasm +0 -0
  147. package/dist/wasm/pkg/bundler/wasm_bg.wasm.d.ts +0 -8
  148. package/tests/__snapshots__/cli.test.ts.snap +0 -202
  149. package/tests/build/success/.dfx/local/canister_ids.json +0 -17
  150. package/tests/build/success/.dfx/local/canisters/bar/bar.did +0 -3
  151. package/tests/build/success/.dfx/local/canisters/bar/bar.most +0 -4
  152. package/tests/build/success/.dfx/local/canisters/bar/bar.wasm +0 -0
  153. package/tests/build/success/.dfx/local/canisters/bar/constructor.did +0 -3
  154. package/tests/build/success/.dfx/local/canisters/bar/index.js +0 -42
  155. package/tests/build/success/.dfx/local/canisters/bar/init_args.txt +0 -1
  156. package/tests/build/success/.dfx/local/canisters/bar/service.did +0 -3
  157. package/tests/build/success/.dfx/local/canisters/bar/service.did.d.ts +0 -7
  158. package/tests/build/success/.dfx/local/canisters/bar/service.did.js +0 -4
  159. package/tests/build/success/.dfx/local/canisters/foo/constructor.did +0 -3
  160. package/tests/build/success/.dfx/local/canisters/foo/foo.did +0 -3
  161. package/tests/build/success/.dfx/local/canisters/foo/foo.most +0 -4
  162. package/tests/build/success/.dfx/local/canisters/foo/foo.wasm +0 -0
  163. package/tests/build/success/.dfx/local/canisters/foo/index.js +0 -42
  164. package/tests/build/success/.dfx/local/canisters/foo/init_args.txt +0 -1
  165. package/tests/build/success/.dfx/local/canisters/foo/service.did +0 -3
  166. package/tests/build/success/.dfx/local/canisters/foo/service.did.d.ts +0 -7
  167. package/tests/build/success/.dfx/local/canisters/foo/service.did.js +0 -4
  168. package/tests/build/success/.dfx/local/lsp/ucwa4-rx777-77774-qaada-cai.did +0 -3
  169. package/tests/build/success/.dfx/local/lsp/ulvla-h7777-77774-qaacq-cai.did +0 -3
  170. package/tests/build/success/.dfx/local/network-id +0 -4
  171. package/wasm/pkg/bundler/package.json +0 -20
  172. package/wasm/pkg/bundler/wasm.d.ts +0 -3
  173. package/wasm/pkg/bundler/wasm.js +0 -5
  174. package/wasm/pkg/bundler/wasm_bg.js +0 -93
  175. package/wasm/pkg/bundler/wasm_bg.wasm +0 -0
  176. package/wasm/pkg/bundler/wasm_bg.wasm.d.ts +0 -8
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { describe, test } from "@jest/globals";
2
+ import path from "path";
3
+ import { cliSnapshot } from "./helpers";
4
+ describe("build without dfx", () => {
5
+ test("builds using mops toolchain moc", async () => {
6
+ const cwd = path.join(import.meta.dirname, "build/no-dfx");
7
+ await cliSnapshot(["build"], { cwd }, 0);
8
+ }, 120_000);
9
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ import { describe, expect, test } from "@jest/globals";
2
+ import path from "path";
3
+ import { cliSnapshot } from "./helpers";
4
+ describe("build", () => {
5
+ test("ok", async () => {
6
+ const cwd = path.join(import.meta.dirname, "build/success");
7
+ await cliSnapshot(["build", "--verbose"], { cwd }, 0);
8
+ await cliSnapshot(["build", "foo"], { cwd }, 0);
9
+ await cliSnapshot(["build", "bar"], { cwd }, 0);
10
+ await cliSnapshot(["build", "foo", "bar"], { cwd }, 0);
11
+ });
12
+ test("error", async () => {
13
+ const cwd = path.join(import.meta.dirname, "build/error");
14
+ await cliSnapshot(["build", "foo", "--verbose"], { cwd }, 0);
15
+ expect((await cliSnapshot(["build", "bar"], { cwd }, 1)).stderr).toMatch("Candid compatibility check failed for canister bar");
16
+ expect((await cliSnapshot(["build", "foo", "bar"], { cwd }, 1)).stderr).toMatch("Candid compatibility check failed for canister bar");
17
+ });
18
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,20 @@
1
+ import { describe, test } from "@jest/globals";
2
+ import path from "path";
3
+ import { cliSnapshot } from "./helpers";
4
+ describe("check-candid", () => {
5
+ test("ok", async () => {
6
+ const cwd = path.join(import.meta.dirname, "check-candid");
7
+ await cliSnapshot(["check-candid", "a.did", "a.did"], { cwd }, 0);
8
+ await cliSnapshot(["check-candid", "b.did", "b.did"], { cwd }, 0);
9
+ await cliSnapshot(["check-candid", "c.did", "c.did"], { cwd }, 0);
10
+ await cliSnapshot(["check-candid", "a.did", "b.did"], { cwd }, 0);
11
+ await cliSnapshot(["check-candid", "b.did", "a.did"], { cwd }, 0);
12
+ });
13
+ test("error", async () => {
14
+ const cwd = path.join(import.meta.dirname, "check-candid");
15
+ await cliSnapshot(["check-candid", "a.did", "c.did"], { cwd }, 1);
16
+ await cliSnapshot(["check-candid", "c.did", "a.did"], { cwd }, 1);
17
+ await cliSnapshot(["check-candid", "b.did", "c.did"], { cwd }, 1);
18
+ await cliSnapshot(["check-candid", "c.did", "b.did"], { cwd }, 1);
19
+ });
20
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,73 @@
1
+ import { beforeAll, describe, expect, test } from "@jest/globals";
2
+ import { cpSync, readdirSync, readFileSync, unlinkSync } from "node:fs";
3
+ import path from "path";
4
+ import { parseDiagnostics } from "../helpers/autofix-motoko";
5
+ import { cli, normalizePaths } from "./helpers";
6
+ function countCodes(stdout) {
7
+ const counts = {};
8
+ for (const diag of parseDiagnostics(stdout)) {
9
+ counts[diag.code] = (counts[diag.code] ?? 0) + 1;
10
+ }
11
+ return counts;
12
+ }
13
+ describe("check --fix", () => {
14
+ const fixDir = path.join(import.meta.dirname, "check/fix");
15
+ const runDir = path.join(fixDir, "run");
16
+ const warningFlags = "-W=M0223,M0236,M0237";
17
+ const diagnosticFlags = [warningFlags, "--error-format=json"];
18
+ beforeAll(() => {
19
+ for (const file of readdirSync(runDir).filter((f) => f.endsWith(".mo"))) {
20
+ unlinkSync(path.join(runDir, file));
21
+ }
22
+ });
23
+ function copyFixture(file) {
24
+ const dest = path.join(runDir, file);
25
+ cpSync(path.join(fixDir, file), dest);
26
+ return dest;
27
+ }
28
+ async function testCheckFix(file, expectedDiagnostics, expectedAfterDiagnostics = {}) {
29
+ const runFilePath = copyFixture(file);
30
+ const beforeResult = await cli(["check", runFilePath, "--", ...diagnosticFlags], { cwd: fixDir });
31
+ expect(countCodes(beforeResult.stdout)).toEqual(expectedDiagnostics);
32
+ const fixResult = await cli(["check", runFilePath, "--fix", "--", warningFlags], { cwd: fixDir });
33
+ expect(normalizePaths(fixResult.stdout)).toMatchSnapshot("fix output");
34
+ expect(readFileSync(runFilePath, "utf-8")).toMatchSnapshot();
35
+ const afterResult = await cli(["check", runFilePath, "--", ...diagnosticFlags], { cwd: fixDir });
36
+ expect(countCodes(afterResult.stdout)).toEqual(expectedAfterDiagnostics);
37
+ return runFilePath;
38
+ }
39
+ test("M0223", async () => {
40
+ await testCheckFix("M0223.mo", { M0223: 1 });
41
+ });
42
+ test("M0236", async () => {
43
+ await testCheckFix("M0236.mo", { M0236: 1 });
44
+ });
45
+ test("M0237", async () => {
46
+ await testCheckFix("M0237.mo", { M0237: 1 });
47
+ });
48
+ test("edit-suggestions", async () => {
49
+ await testCheckFix("edit-suggestions.mo", {
50
+ M0223: 2,
51
+ M0236: 11,
52
+ M0237: 17,
53
+ });
54
+ });
55
+ test("transitive imports", async () => {
56
+ const runMainPath = copyFixture("transitive-main.mo");
57
+ const runLibPath = copyFixture("transitive-lib.mo");
58
+ const fixResult = await cli(["check", runMainPath, "--fix", "--", warningFlags], { cwd: fixDir });
59
+ expect(normalizePaths(fixResult.stdout)).toMatchSnapshot("fix output");
60
+ expect(readFileSync(runMainPath, "utf-8")).toMatchSnapshot("main file");
61
+ expect(readFileSync(runLibPath, "utf-8")).toMatchSnapshot("lib file");
62
+ const afterResult = await cli(["check", runMainPath, "--", ...diagnosticFlags], { cwd: fixDir });
63
+ expect(countCodes(afterResult.stdout)).toEqual({});
64
+ });
65
+ test("verbose", async () => {
66
+ const result = await cli(["check", "Ok.mo", "--fix", "--verbose"], {
67
+ cwd: fixDir,
68
+ });
69
+ expect(result.exitCode).toBe(0);
70
+ expect(result.stdout).toContain("Attempting to fix files");
71
+ expect(result.stdout).toContain("No fixes were needed");
72
+ });
73
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,33 @@
1
+ import { describe, expect, test } from "@jest/globals";
2
+ import path from "path";
3
+ import { cliSnapshot } from "./helpers";
4
+ describe("check", () => {
5
+ test("ok", async () => {
6
+ const cwd = path.join(import.meta.dirname, "check/success");
7
+ await cliSnapshot(["check", "Ok.mo"], { cwd }, 0);
8
+ await cliSnapshot(["check", "Ok.mo", "--verbose"], { cwd }, 0);
9
+ });
10
+ test("error", async () => {
11
+ const cwd = path.join(import.meta.dirname, "check/error");
12
+ await cliSnapshot(["check", "Error.mo"], { cwd }, 1);
13
+ await cliSnapshot(["check", "Ok.mo", "Error.mo"], { cwd }, 1);
14
+ });
15
+ test("warning", async () => {
16
+ const cwd = path.join(import.meta.dirname, "check/success");
17
+ const result = await cliSnapshot(["check", "Warning.mo"], { cwd }, 0);
18
+ expect(result.stderr).toMatch(/warning \[M0194\]/);
19
+ expect(result.stderr).toMatch(/unused identifier/);
20
+ });
21
+ test("warning verbose", async () => {
22
+ const cwd = path.join(import.meta.dirname, "check/success");
23
+ const result = await cliSnapshot(["check", "Warning.mo", "--verbose"], { cwd }, 0);
24
+ expect(result.stderr).toMatch(/warning \[M0194\]/);
25
+ expect(result.stderr).toMatch(/unused identifier/);
26
+ });
27
+ test("warning with -Werror flag", async () => {
28
+ const cwd = path.join(import.meta.dirname, "check/success");
29
+ const result = await cliSnapshot(["check", "Warning.mo", "--", "-Werror"], { cwd }, 1);
30
+ expect(result.stderr).toMatch(/warning \[M0194\]/);
31
+ expect(result.stderr).toMatch(/unused identifier/);
32
+ });
33
+ });
@@ -1,63 +1,10 @@
1
1
  import { describe, expect, test } from "@jest/globals";
2
- import { execa } from "execa";
3
- import path from "path";
4
- const cli = async (args, { cwd } = {}) => {
5
- return await execa("npm", ["run", "mops", "--", ...args], {
6
- env: { MOPS_CWD: cwd },
7
- stdio: "pipe",
8
- reject: false,
9
- });
10
- };
11
- const cliSnapshot = async (args, options, exitCode) => {
12
- const result = await cli(args, options);
13
- expect({
14
- command: result.command,
15
- exitCode: result.exitCode,
16
- timedOut: result.timedOut,
17
- stdio: Boolean(result.stdout || result.stderr),
18
- }).toEqual({
19
- command: result.command,
20
- exitCode,
21
- timedOut: false,
22
- stdio: true,
23
- });
24
- expect({
25
- exitCode: result.exitCode,
26
- stdout: result.stdout,
27
- stderr: result.stderr,
28
- }).toMatchSnapshot();
29
- return result;
30
- };
31
- describe("mops", () => {
32
- test("version", async () => {
2
+ import { cli } from "./helpers";
3
+ describe("cli", () => {
4
+ test("--version", async () => {
33
5
  expect((await cli(["--version"])).stdout).toMatch(/CLI \d+\.\d+\.\d+/);
34
6
  });
35
- test("help", async () => {
7
+ test("--help", async () => {
36
8
  expect((await cli(["--help"])).stdout).toMatch(/^Usage: mops/m);
37
9
  });
38
- test("build success", async () => {
39
- const cwd = path.join(import.meta.dirname, "build/success");
40
- await cliSnapshot(["build"], { cwd }, 0);
41
- await cliSnapshot(["build", "foo"], { cwd }, 0);
42
- await cliSnapshot(["build", "bar"], { cwd }, 0);
43
- await cliSnapshot(["build", "foo", "bar"], { cwd }, 0);
44
- });
45
- test("build error", async () => {
46
- const cwd = path.join(import.meta.dirname, "build/error");
47
- await cliSnapshot(["build", "foo"], { cwd }, 0);
48
- expect((await cliSnapshot(["build", "bar"], { cwd }, 1)).stderr).toMatch("Candid compatibility check failed for canister bar");
49
- expect((await cliSnapshot(["build", "foo", "bar"], { cwd }, 1)).stderr).toMatch("Candid compatibility check failed for canister bar");
50
- });
51
- test("check-candid", async () => {
52
- const cwd = path.join(import.meta.dirname, "check-candid");
53
- await cliSnapshot(["check-candid", "a.did", "a.did"], { cwd }, 0);
54
- await cliSnapshot(["check-candid", "b.did", "b.did"], { cwd }, 0);
55
- await cliSnapshot(["check-candid", "c.did", "c.did"], { cwd }, 0);
56
- await cliSnapshot(["check-candid", "a.did", "b.did"], { cwd }, 0);
57
- await cliSnapshot(["check-candid", "b.did", "a.did"], { cwd }, 0);
58
- await cliSnapshot(["check-candid", "a.did", "c.did"], { cwd }, 1);
59
- await cliSnapshot(["check-candid", "c.did", "a.did"], { cwd }, 1);
60
- await cliSnapshot(["check-candid", "b.did", "c.did"], { cwd }, 1);
61
- await cliSnapshot(["check-candid", "c.did", "b.did"], { cwd }, 1);
62
- });
63
10
  });
@@ -0,0 +1,22 @@
1
+ export interface CliOptions {
2
+ cwd?: string;
3
+ }
4
+ export declare const cli: (args: string[], { cwd }?: CliOptions) => Promise<import("execa").Result<{
5
+ stdio: "pipe";
6
+ reject: false;
7
+ cwd?: string | undefined;
8
+ env: {
9
+ MOPS_CWD?: string | undefined;
10
+ TZ?: string;
11
+ };
12
+ }>>;
13
+ export declare const normalizePaths: (text: string) => string;
14
+ export declare const cliSnapshot: (args: string[], options: CliOptions, exitCode: number) => Promise<import("execa").Result<{
15
+ stdio: "pipe";
16
+ reject: false;
17
+ cwd?: string | undefined;
18
+ env: {
19
+ MOPS_CWD?: string | undefined;
20
+ TZ?: string;
21
+ };
22
+ }>>;
@@ -0,0 +1,43 @@
1
+ import { expect } from "@jest/globals";
2
+ import { execa } from "execa";
3
+ import { dirname } from "path";
4
+ import { fileURLToPath } from "url";
5
+ export const cli = async (args, { cwd } = {}) => {
6
+ return await execa("npm", ["run", "--silent", "mops", "--", ...args], {
7
+ env: { ...process.env, ...(cwd != null && { MOPS_CWD: cwd }) },
8
+ ...(cwd != null && { cwd }),
9
+ stdio: "pipe",
10
+ reject: false,
11
+ });
12
+ };
13
+ // Strip ANSI escape codes for portable snapshots (avoid control char in regex literal)
14
+ const stripAnsi = (s) => s.replace(new RegExp(`\u001b\\[[0-9;]*m`, "g"), "");
15
+ export const normalizePaths = (text) => {
16
+ // Replace absolute paths with placeholders for CI
17
+ return stripAnsi(text
18
+ .replaceAll(dirname(fileURLToPath(import.meta.url)), "<TEST_DIR>")
19
+ .replace(/\/[^\s"]+\/\.cache\/mops/g, "<CACHE>")
20
+ .replace(/\/[^\s"]+\/Library\/Caches\/mops/g, "<CACHE>")
21
+ .replace(/\/[^\s"[\]]+\/moc(?:-wrapper)?(?=\s|$)/g, "moc-wrapper")
22
+ .replace(/\/[^\s"[\]]+\.motoko\/bin\/moc/g, "moc-wrapper"));
23
+ };
24
+ export const cliSnapshot = async (args, options, exitCode) => {
25
+ const result = await cli(args, options);
26
+ expect({
27
+ command: result.command,
28
+ exitCode: result.exitCode,
29
+ timedOut: result.timedOut,
30
+ stdio: Boolean(result.stdout || result.stderr),
31
+ }).toEqual({
32
+ command: result.command,
33
+ exitCode,
34
+ timedOut: false,
35
+ stdio: true,
36
+ });
37
+ expect({
38
+ exitCode: result.exitCode,
39
+ stdout: normalizePaths(result.stdout),
40
+ stderr: normalizePaths(result.stderr),
41
+ }).toMatchSnapshot();
42
+ return result;
43
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { describe, test } from "@jest/globals";
2
+ import path from "path";
3
+ import { cliSnapshot } from "./helpers";
4
+ describe("lint", () => {
5
+ test("ok", async () => {
6
+ const cwd = path.join(import.meta.dirname, "lint");
7
+ await cliSnapshot(["lint", "Ok", "--verbose"], { cwd }, 0);
8
+ });
9
+ test("error", async () => {
10
+ const cwd = path.join(import.meta.dirname, "lint");
11
+ await cliSnapshot(["lint", "--verbose"], { cwd }, 1);
12
+ await cliSnapshot(["lint", "NoBoolSwitch", "--verbose"], { cwd }, 1);
13
+ await cliSnapshot(["lint", "DoesNotExist"], { cwd }, 1);
14
+ });
15
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import { describe, expect, test } from "@jest/globals";
2
+ import path from "path";
3
+ import { cli } from "./helpers";
4
+ describe("toolchain", () => {
5
+ test("file URI", async () => {
6
+ const cwd = path.join(import.meta.dirname, "toolchain");
7
+ const result = await cli(["toolchain", "bin", "moc"], { cwd });
8
+ expect(result.exitCode).toBe(0);
9
+ expect(result.stdout.trim()).toBe("./mock");
10
+ });
11
+ });
package/dist/types.d.ts CHANGED
@@ -24,6 +24,9 @@ export type Config = {
24
24
  outputDir?: string;
25
25
  args?: string[];
26
26
  };
27
+ lint?: {
28
+ args?: string[];
29
+ };
27
30
  };
28
31
  export type CanisterConfig = {
29
32
  main: string;
@@ -42,8 +45,9 @@ export type Toolchain = {
42
45
  moc?: string;
43
46
  wasmtime?: string;
44
47
  "pocket-ic"?: string;
48
+ lintoko?: string;
45
49
  };
46
- export type Tool = "moc" | "wasmtime" | "pocket-ic";
50
+ export type Tool = "moc" | "wasmtime" | "pocket-ic" | "lintoko";
47
51
  export type Requirements = {
48
52
  moc?: string;
49
53
  };
Binary file
Binary file
@@ -1,6 +1,12 @@
1
- import * as wasm from "../../wasm/pkg/nodejs/wasm.js";
1
+ import { createRequire } from "node:module";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
2
4
  import { setWasmBindings } from "../../wasm.js";
3
5
 
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const require = createRequire(import.meta.url);
8
+ const wasm = require(path.join(__dirname, "../../wasm/pkg/nodejs/wasm.js"));
9
+
4
10
  setWasmBindings(wasm);
5
11
 
6
12
  export * from "../../cli.js";
@@ -0,0 +1,170 @@
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import { resolve } from "node:path";
3
+ import { execa } from "execa";
4
+ import {
5
+ TextDocument,
6
+ type TextEdit,
7
+ } from "vscode-languageserver-textdocument";
8
+
9
+ interface Fix {
10
+ file: string;
11
+ code: string;
12
+ edit: TextEdit;
13
+ }
14
+
15
+ interface MocSpan {
16
+ file: string;
17
+ line_start: number;
18
+ column_start: number;
19
+ line_end: number;
20
+ column_end: number;
21
+ is_primary: boolean;
22
+ label: string | null;
23
+ suggested_replacement: string | null;
24
+ suggestion_applicability: string | null;
25
+ }
26
+
27
+ export interface MocDiagnostic {
28
+ message: string;
29
+ code: string;
30
+ level: string;
31
+ spans: MocSpan[];
32
+ notes: string[];
33
+ }
34
+
35
+ export function parseDiagnostics(stdout: string): MocDiagnostic[] {
36
+ return stdout
37
+ .split("\n")
38
+ .filter((l) => l.trim())
39
+ .map((l) => {
40
+ try {
41
+ return JSON.parse(l) as MocDiagnostic;
42
+ } catch {
43
+ return null;
44
+ }
45
+ })
46
+ .filter((d) => d !== null);
47
+ }
48
+
49
+ function extractFixes(diagnostics: MocDiagnostic[]): Fix[] {
50
+ const fixes: Fix[] = [];
51
+ for (const diag of diagnostics) {
52
+ for (const span of diag.spans) {
53
+ if (
54
+ span.suggestion_applicability === "MachineApplicable" &&
55
+ span.suggested_replacement !== null
56
+ ) {
57
+ fixes.push({
58
+ file: span.file,
59
+ code: diag.code,
60
+ edit: {
61
+ range: {
62
+ start: {
63
+ line: span.line_start - 1,
64
+ character: span.column_start - 1,
65
+ },
66
+ end: {
67
+ line: span.line_end - 1,
68
+ character: span.column_end - 1,
69
+ },
70
+ },
71
+ newText: span.suggested_replacement,
72
+ },
73
+ });
74
+ }
75
+ }
76
+ }
77
+ return fixes;
78
+ }
79
+
80
+ const MAX_FIX_ITERATIONS = 10;
81
+
82
+ export interface AutofixResult {
83
+ /** Map of file path → diagnostic codes fixed in that file */
84
+ fixedFiles: Map<string, string[]>;
85
+ totalFixCount: number;
86
+ }
87
+
88
+ export async function autofixMotoko(
89
+ mocPath: string,
90
+ files: string[],
91
+ mocArgs: string[],
92
+ ): Promise<AutofixResult | null> {
93
+ const fixedFilesCodes = new Map<string, string[]>();
94
+
95
+ for (let iteration = 0; iteration < MAX_FIX_ITERATIONS; iteration++) {
96
+ const allFixes: Fix[] = [];
97
+
98
+ for (const file of files) {
99
+ const result = await execa(
100
+ mocPath,
101
+ [file, "--error-format=json", ...mocArgs],
102
+ { stdio: "pipe", reject: false },
103
+ );
104
+
105
+ const diagnostics = parseDiagnostics(result.stdout);
106
+ allFixes.push(...extractFixes(diagnostics));
107
+ }
108
+
109
+ if (allFixes.length === 0) {
110
+ break;
111
+ }
112
+
113
+ const fixesByFile = new Map<string, Fix[]>();
114
+ for (const fix of allFixes) {
115
+ const normalizedPath = resolve(fix.file);
116
+ const existing = fixesByFile.get(normalizedPath) ?? [];
117
+ existing.push(fix);
118
+ fixesByFile.set(normalizedPath, existing);
119
+ }
120
+
121
+ let progress = false;
122
+
123
+ for (const [file, fixes] of fixesByFile) {
124
+ const original = await readFile(file, "utf-8");
125
+ const doc = TextDocument.create(`file://${file}`, "motoko", 0, original);
126
+
127
+ let result: string;
128
+ try {
129
+ result = TextDocument.applyEdits(
130
+ doc,
131
+ fixes.map((f) => f.edit),
132
+ );
133
+ } catch (err) {
134
+ console.warn(`Warning: could not apply fixes to ${file}: ${err}`);
135
+ continue;
136
+ }
137
+
138
+ if (result === original) {
139
+ continue;
140
+ }
141
+
142
+ await writeFile(file, result, "utf-8");
143
+ progress = true;
144
+
145
+ const existing = fixedFilesCodes.get(file) ?? [];
146
+ for (const fix of fixes) {
147
+ existing.push(fix.code);
148
+ }
149
+ fixedFilesCodes.set(file, existing);
150
+ }
151
+
152
+ if (!progress) {
153
+ break;
154
+ }
155
+ }
156
+
157
+ if (fixedFilesCodes.size === 0) {
158
+ return null;
159
+ }
160
+
161
+ let totalFixCount = 0;
162
+ for (const codes of fixedFilesCodes.values()) {
163
+ totalFixCount += codes.length;
164
+ }
165
+
166
+ return {
167
+ fixedFiles: fixedFilesCodes,
168
+ totalFixCount,
169
+ };
170
+ }
@@ -1,8 +1,19 @@
1
1
  import { execFileSync } from "node:child_process";
2
+ import { type SemVer, parse } from "semver";
3
+ import { readConfig } from "../mops.js";
2
4
  import { getMocPath } from "./get-moc-path.js";
3
5
 
6
+ export function getMocSemVer(): SemVer | null {
7
+ return parse(getMocVersion(false));
8
+ }
9
+
4
10
  export function getMocVersion(throwOnError = false): string {
5
- let mocPath = getMocPath(false);
11
+ let configVersion = readConfig().toolchain?.moc;
12
+ if (configVersion) {
13
+ return configVersion;
14
+ }
15
+
16
+ const mocPath = getMocPath(false);
6
17
  if (!mocPath) {
7
18
  return "";
8
19
  }
package/mops.ts CHANGED
@@ -13,6 +13,7 @@ import { mainActor, storageActor } from "./api/actors.js";
13
13
  import { getNetwork } from "./api/network.js";
14
14
  import { getHighestVersion } from "./api/getHighestVersion.js";
15
15
  import { getPackageId } from "./helpers/get-package-id.js";
16
+ import { FILE_PATH_REGEX } from "./constants.js";
16
17
 
17
18
  if (!globalThis.fetch) {
18
19
  globalThis.fetch = fetch as any;
@@ -157,7 +158,7 @@ export function getDependencyType(version: string) {
157
158
  }
158
159
  if (version.startsWith("https://github.com/")) {
159
160
  return "github";
160
- } else if (version.match(/^(\.?\.)?\//)) {
161
+ } else if (version.match(FILE_PATH_REGEX)) {
161
162
  return "local";
162
163
  } else {
163
164
  return "mops";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/bin/mops.js",
@@ -92,7 +92,8 @@
92
92
  "stream-to-promise": "3.0.0",
93
93
  "string-width": "7.2.0",
94
94
  "tar": "7.5.6",
95
- "terminal-size": "4.0.0"
95
+ "terminal-size": "4.0.0",
96
+ "vscode-languageserver-textdocument": "1.0.12"
96
97
  },
97
98
  "devDependencies": {
98
99
  "@tsconfig/strictest": "2.0.5",
@@ -0,0 +1,11 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`build without dfx builds using mops toolchain moc 1`] = `
4
+ {
5
+ "exitCode": 0,
6
+ "stderr": "",
7
+ "stdout": "build canister main
8
+
9
+ ✓ Built 1 canister successfully",
10
+ }
11
+ `;