@atscript/core 0.2.14 → 0.2.16

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/index.cjs CHANGED
@@ -3035,10 +3035,10 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3035
3035
  //#endregion
3036
3036
  //#region packages/core/src/module-id.ts
3037
3037
  /**
3038
- * The package of each project root (see {@link moduleId}), by its absolute path, with the
3039
- * `package.json` files read for it and their modification times (`-1`: absent) — read again
3040
- * once one of them changed (a package renamed, a manifest added or removed), checked once per
3041
- * documents' epoch (`checked`).
3038
+ * The package of each project root (see {@link moduleId}) and file directory (see
3039
+ * {@link packageOfFile}), by its absolute path, with the `package.json` files read for it and
3040
+ * their modification times (`-1`: absent) — read again once one of them changed (a package
3041
+ * renamed, a manifest added or removed), checked once per documents' epoch (`checked`).
3042
3042
  */ const packages = /* @__PURE__ */ new Map();
3043
3043
  /**
3044
3044
  * The id of `doc`'s module, which orders the blocks of modules at one import depth: the name of
@@ -3051,16 +3051,7 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3051
3051
  */ function moduleId(doc) {
3052
3052
  const file = fileUriToPath(doc.id);
3053
3053
  const root = path.default.resolve(doc.config.rootDir ?? path.default.dirname(file));
3054
- const epoch = documentsEpoch();
3055
- let pkg = packages.get(root);
3056
- if (!pkg || pkg.checked !== epoch && pkg.manifests.some((m) => mtimeOf(m.file) !== m.mtime)) {
3057
- pkg = {
3058
- ...packageOf(root),
3059
- checked: epoch
3060
- };
3061
- packages.set(root, pkg);
3062
- }
3063
- pkg.checked = epoch;
3054
+ const pkg = packageAt(root);
3064
3055
  const known = ids.get(doc);
3065
3056
  if (known?.uri === doc.id && known.root === root && known.pkg === pkg) return known.id;
3066
3057
  const id = `${pkg.scope}:${path.default.relative(pkg.dir, file).replaceAll("\\", "/")}`;
@@ -3073,7 +3064,34 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3073
3064
  return id;
3074
3065
  }
3075
3066
  /** {@link moduleId} by document: valid for its URI, root and package entry. */ const ids = /* @__PURE__ */ new WeakMap();
3076
- /** The package of project root `root` (see {@link moduleId}). */ function packageOf(root) {
3067
+ /**
3068
+ * The package holding the file at path `file`: the name and directory of the nearest
3069
+ * `package.json` with a `name` above it, and the file's path in it (`src/a.as`) — `undefined`
3070
+ * without one.
3071
+ */ function packageOfFile(file) {
3072
+ const abs = path.default.resolve(file);
3073
+ const pkg = packageAt(path.default.dirname(abs));
3074
+ if (pkg.name === void 0) return;
3075
+ return {
3076
+ name: pkg.name,
3077
+ dir: pkg.dir,
3078
+ path: path.default.relative(pkg.dir, abs).replaceAll("\\", "/")
3079
+ };
3080
+ }
3081
+ /** {@link packageOf} directory `root`, cached until one of the manifests it read changes. */ function packageAt(root) {
3082
+ const epoch = documentsEpoch();
3083
+ let pkg = packages.get(root);
3084
+ if (!pkg || pkg.checked !== epoch && pkg.manifests.some((m) => mtimeOf(m.file) !== m.mtime)) {
3085
+ pkg = {
3086
+ ...packageOf(root),
3087
+ checked: epoch
3088
+ };
3089
+ packages.set(root, pkg);
3090
+ }
3091
+ pkg.checked = epoch;
3092
+ return pkg;
3093
+ }
3094
+ /** The package of directory `root` (a project root, see {@link moduleId}). */ function packageOf(root) {
3077
3095
  const manifests = [];
3078
3096
  let unnamed;
3079
3097
  for (let dir = root;; dir = path.default.dirname(dir)) {
@@ -3086,6 +3104,7 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3086
3104
  const name = mtime >= 0 ? packageName(file) : void 0;
3087
3105
  if (name) return {
3088
3106
  scope: name,
3107
+ name,
3089
3108
  dir,
3090
3109
  manifests
3091
3110
  };
@@ -3096,6 +3115,7 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3096
3115
  const relative = path.default.relative(base, root).replaceAll("\\", "/");
3097
3116
  return {
3098
3117
  scope: `#${(0, crypto.createHash)("sha256").update(relative).digest("hex").slice(0, 12)}`,
3118
+ name: void 0,
3099
3119
  dir: root,
3100
3120
  manifests
3101
3121
  };
@@ -9725,17 +9745,13 @@ var AtscriptDoc = class AtscriptDoc {
9725
9745
  return messages;
9726
9746
  }
9727
9747
  /**
9728
- * Errors for references to a type another document does not export, in definitions inlined
9729
- * from that document (`extends`, an intersection member, `annotate … as …`): the generated
9730
- * code of this document would have to import it from there. Checks what codegen emits (see
9731
- * {@link emittedRefs}).
9732
- */ /**
9733
9748
  * Warnings for the mutating `annotate` blocks of this document whose target another document
9734
9749
  * declares, when the block is not reachable: the declaring document does not import this one
9735
9750
  * (directly or not), and a document using the target does not either. A block is part of its
9736
9751
  * target's definition, built into whatever uses the target in the same build — a build of a
9737
9752
  * document using it without this one (another entry, the target's package built on its own)
9738
- * would not have it.
9753
+ * would not have it. When the target and documents using it belong to another package (see
9754
+ * `packageOfFile`), those documents cannot import this one: they get a warning of their own.
9739
9755
  */ unreachableBlockMessages() {
9740
9756
  const messages = [];
9741
9757
  const known = /* @__PURE__ */ new Map();
@@ -9747,21 +9763,54 @@ var AtscriptDoc = class AtscriptDoc {
9747
9763
  }
9748
9764
  return hit;
9749
9765
  };
9766
+ let own;
9767
+ let ownFound = false;
9768
+ const places = /* @__PURE__ */ new Map();
9769
+ const placeOf = (d) => {
9770
+ let place = places.get(d);
9771
+ if (!place) {
9772
+ if (!ownFound) {
9773
+ own = packageOfDoc(this);
9774
+ ownFound = true;
9775
+ }
9776
+ const pkg = this.id.startsWith("file://") ? packageOfDoc(d) : void 0;
9777
+ place = pkg && pkg.dir !== own?.dir ? {
9778
+ other: true,
9779
+ path: `${pkg.name}/${pkg.path}`
9780
+ } : {
9781
+ other: false,
9782
+ path: relDocPath(this.id, d.id)
9783
+ };
9784
+ places.set(d, place);
9785
+ }
9786
+ return place;
9787
+ };
9750
9788
  for (const node of this.nodes) {
9751
9789
  if (!isAnnotate(node) || !node.isMutating) continue;
9752
9790
  const owner = this.blockTarget(node);
9753
9791
  const target = owner?.doc;
9754
9792
  if (!target || !owner.node || target === this || importsThis(target)) continue;
9755
9793
  const missing = [...target.documentsUsing(owner.node)].filter((d) => d !== this && !importsThis(d));
9756
- if (missing.length > 0) {
9757
- const token = node.token("target");
9758
- const users = missing.map((d) => relDocPath(this.id, d.id)).sort();
9794
+ if (missing.length === 0) continue;
9795
+ const range = node.token("target").range;
9796
+ const name = owner.node.id;
9797
+ const declared = placeOf(target);
9798
+ const users = missing.map(placeOf);
9799
+ const local = users.filter((u) => !(declared.other && u.other));
9800
+ const other = users.filter((u) => declared.other && u.other);
9801
+ if (local.length > 0) {
9802
+ const from = declared.other ? "" : `${declared.path} or from `;
9759
9803
  messages.push({
9760
9804
  severity: 2,
9761
- message: `"${owner.node.id}" (declared in ${relDocPath(this.id, target.id)}) is used by ${users.join(", ")} without importing this file (directly or not): this annotate applies only in builds including this file — import it from ${relDocPath(this.id, target.id)} or from the files using "${owner.node.id}"`,
9762
- range: token.range
9805
+ message: `"${name}" (declared in ${declared.path}) is used by ${pathList(local)} without importing this file (directly or not): this annotate applies only in builds including this file — import it from ${from}the files using "${name}"`,
9806
+ range
9763
9807
  });
9764
9808
  }
9809
+ if (other.length > 0) messages.push({
9810
+ severity: 2,
9811
+ message: `"${name}" (declared in ${declared.path}) is used by files of other packages (${pathList(other)}), which cannot import this file: their packages' own builds never include this annotate, and at runtime it applies to those files only if this file is loaded before they first use "${name}" — to change only your own types, annotate a type of your own (interface X extends ${name}, or annotate ${name} as X)`,
9812
+ range
9813
+ });
9765
9814
  }
9766
9815
  return messages;
9767
9816
  }
@@ -10457,6 +10506,12 @@ const zeroRange = {
10457
10506
  for (const part of chain) ref.addChain(token(part));
10458
10507
  return ref;
10459
10508
  }
10509
+ /** The `path`s of `items`, sorted, comma-separated. */ function pathList(items) {
10510
+ return items.map((u) => u.path).sort().join(", ");
10511
+ }
10512
+ /** The package of file document `doc` (see `packageOfFile`); `undefined` for any other document. */ function packageOfDoc(doc) {
10513
+ return doc.id.startsWith("file://") ? packageOfFile(fileUriToPath(doc.id)) : void 0;
10514
+ }
10460
10515
  /** `to`'s path relative to `from` (`./b.as`), or its id when not a file. */ function relDocPath(from, to) {
10461
10516
  try {
10462
10517
  return `${getRelPath(from, to)}.as`;
package/dist/index.d.ts CHANGED
@@ -2159,19 +2159,14 @@ declare class AtscriptDoc {
2159
2159
  * `never` (see `StructureModel.neverWarnings`).
2160
2160
  */
2161
2161
  private neverMessages;
2162
- /**
2163
- * Errors for references to a type another document does not export, in definitions inlined
2164
- * from that document (`extends`, an intersection member, `annotate … as …`): the generated
2165
- * code of this document would have to import it from there. Checks what codegen emits (see
2166
- * {@link emittedRefs}).
2167
- */
2168
2162
  /**
2169
2163
  * Warnings for the mutating `annotate` blocks of this document whose target another document
2170
2164
  * declares, when the block is not reachable: the declaring document does not import this one
2171
2165
  * (directly or not), and a document using the target does not either. A block is part of its
2172
2166
  * target's definition, built into whatever uses the target in the same build — a build of a
2173
2167
  * document using it without this one (another entry, the target's package built on its own)
2174
- * would not have it.
2168
+ * would not have it. When the target and documents using it belong to another package (see
2169
+ * `packageOfFile`), those documents cannot import this one: they get a warning of their own.
2175
2170
  */
2176
2171
  private unreachableBlockMessages;
2177
2172
  /**
package/dist/index.mjs CHANGED
@@ -3004,10 +3004,10 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3004
3004
  //#endregion
3005
3005
  //#region packages/core/src/module-id.ts
3006
3006
  /**
3007
- * The package of each project root (see {@link moduleId}), by its absolute path, with the
3008
- * `package.json` files read for it and their modification times (`-1`: absent) — read again
3009
- * once one of them changed (a package renamed, a manifest added or removed), checked once per
3010
- * documents' epoch (`checked`).
3007
+ * The package of each project root (see {@link moduleId}) and file directory (see
3008
+ * {@link packageOfFile}), by its absolute path, with the `package.json` files read for it and
3009
+ * their modification times (`-1`: absent) — read again once one of them changed (a package
3010
+ * renamed, a manifest added or removed), checked once per documents' epoch (`checked`).
3011
3011
  */ const packages = /* @__PURE__ */ new Map();
3012
3012
  /**
3013
3013
  * The id of `doc`'s module, which orders the blocks of modules at one import depth: the name of
@@ -3020,16 +3020,7 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3020
3020
  */ function moduleId(doc) {
3021
3021
  const file = fileUriToPath(doc.id);
3022
3022
  const root = path.resolve(doc.config.rootDir ?? path.dirname(file));
3023
- const epoch = documentsEpoch();
3024
- let pkg = packages.get(root);
3025
- if (!pkg || pkg.checked !== epoch && pkg.manifests.some((m) => mtimeOf(m.file) !== m.mtime)) {
3026
- pkg = {
3027
- ...packageOf(root),
3028
- checked: epoch
3029
- };
3030
- packages.set(root, pkg);
3031
- }
3032
- pkg.checked = epoch;
3023
+ const pkg = packageAt(root);
3033
3024
  const known = ids.get(doc);
3034
3025
  if (known?.uri === doc.id && known.root === root && known.pkg === pkg) return known.id;
3035
3026
  const id = `${pkg.scope}:${path.relative(pkg.dir, file).replaceAll("\\", "/")}`;
@@ -3042,7 +3033,34 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3042
3033
  return id;
3043
3034
  }
3044
3035
  /** {@link moduleId} by document: valid for its URI, root and package entry. */ const ids = /* @__PURE__ */ new WeakMap();
3045
- /** The package of project root `root` (see {@link moduleId}). */ function packageOf(root) {
3036
+ /**
3037
+ * The package holding the file at path `file`: the name and directory of the nearest
3038
+ * `package.json` with a `name` above it, and the file's path in it (`src/a.as`) — `undefined`
3039
+ * without one.
3040
+ */ function packageOfFile(file) {
3041
+ const abs = path.resolve(file);
3042
+ const pkg = packageAt(path.dirname(abs));
3043
+ if (pkg.name === void 0) return;
3044
+ return {
3045
+ name: pkg.name,
3046
+ dir: pkg.dir,
3047
+ path: path.relative(pkg.dir, abs).replaceAll("\\", "/")
3048
+ };
3049
+ }
3050
+ /** {@link packageOf} directory `root`, cached until one of the manifests it read changes. */ function packageAt(root) {
3051
+ const epoch = documentsEpoch();
3052
+ let pkg = packages.get(root);
3053
+ if (!pkg || pkg.checked !== epoch && pkg.manifests.some((m) => mtimeOf(m.file) !== m.mtime)) {
3054
+ pkg = {
3055
+ ...packageOf(root),
3056
+ checked: epoch
3057
+ };
3058
+ packages.set(root, pkg);
3059
+ }
3060
+ pkg.checked = epoch;
3061
+ return pkg;
3062
+ }
3063
+ /** The package of directory `root` (a project root, see {@link moduleId}). */ function packageOf(root) {
3046
3064
  const manifests = [];
3047
3065
  let unnamed;
3048
3066
  for (let dir = root;; dir = path.dirname(dir)) {
@@ -3055,6 +3073,7 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3055
3073
  const name = mtime >= 0 ? packageName(file) : void 0;
3056
3074
  if (name) return {
3057
3075
  scope: name,
3076
+ name,
3058
3077
  dir,
3059
3078
  manifests
3060
3079
  };
@@ -3065,6 +3084,7 @@ const ARITHMETIC_OPS = new Set(BACKTICK_ARG_VOCABULARY.arithmeticOps);
3065
3084
  const relative = path.relative(base, root).replaceAll("\\", "/");
3066
3085
  return {
3067
3086
  scope: `#${createHash("sha256").update(relative).digest("hex").slice(0, 12)}`,
3087
+ name: void 0,
3068
3088
  dir: root,
3069
3089
  manifests
3070
3090
  };
@@ -9694,17 +9714,13 @@ var AtscriptDoc = class AtscriptDoc {
9694
9714
  return messages;
9695
9715
  }
9696
9716
  /**
9697
- * Errors for references to a type another document does not export, in definitions inlined
9698
- * from that document (`extends`, an intersection member, `annotate … as …`): the generated
9699
- * code of this document would have to import it from there. Checks what codegen emits (see
9700
- * {@link emittedRefs}).
9701
- */ /**
9702
9717
  * Warnings for the mutating `annotate` blocks of this document whose target another document
9703
9718
  * declares, when the block is not reachable: the declaring document does not import this one
9704
9719
  * (directly or not), and a document using the target does not either. A block is part of its
9705
9720
  * target's definition, built into whatever uses the target in the same build — a build of a
9706
9721
  * document using it without this one (another entry, the target's package built on its own)
9707
- * would not have it.
9722
+ * would not have it. When the target and documents using it belong to another package (see
9723
+ * `packageOfFile`), those documents cannot import this one: they get a warning of their own.
9708
9724
  */ unreachableBlockMessages() {
9709
9725
  const messages = [];
9710
9726
  const known = /* @__PURE__ */ new Map();
@@ -9716,21 +9732,54 @@ var AtscriptDoc = class AtscriptDoc {
9716
9732
  }
9717
9733
  return hit;
9718
9734
  };
9735
+ let own;
9736
+ let ownFound = false;
9737
+ const places = /* @__PURE__ */ new Map();
9738
+ const placeOf = (d) => {
9739
+ let place = places.get(d);
9740
+ if (!place) {
9741
+ if (!ownFound) {
9742
+ own = packageOfDoc(this);
9743
+ ownFound = true;
9744
+ }
9745
+ const pkg = this.id.startsWith("file://") ? packageOfDoc(d) : void 0;
9746
+ place = pkg && pkg.dir !== own?.dir ? {
9747
+ other: true,
9748
+ path: `${pkg.name}/${pkg.path}`
9749
+ } : {
9750
+ other: false,
9751
+ path: relDocPath(this.id, d.id)
9752
+ };
9753
+ places.set(d, place);
9754
+ }
9755
+ return place;
9756
+ };
9719
9757
  for (const node of this.nodes) {
9720
9758
  if (!isAnnotate(node) || !node.isMutating) continue;
9721
9759
  const owner = this.blockTarget(node);
9722
9760
  const target = owner?.doc;
9723
9761
  if (!target || !owner.node || target === this || importsThis(target)) continue;
9724
9762
  const missing = [...target.documentsUsing(owner.node)].filter((d) => d !== this && !importsThis(d));
9725
- if (missing.length > 0) {
9726
- const token = node.token("target");
9727
- const users = missing.map((d) => relDocPath(this.id, d.id)).sort();
9763
+ if (missing.length === 0) continue;
9764
+ const range = node.token("target").range;
9765
+ const name = owner.node.id;
9766
+ const declared = placeOf(target);
9767
+ const users = missing.map(placeOf);
9768
+ const local = users.filter((u) => !(declared.other && u.other));
9769
+ const other = users.filter((u) => declared.other && u.other);
9770
+ if (local.length > 0) {
9771
+ const from = declared.other ? "" : `${declared.path} or from `;
9728
9772
  messages.push({
9729
9773
  severity: 2,
9730
- message: `"${owner.node.id}" (declared in ${relDocPath(this.id, target.id)}) is used by ${users.join(", ")} without importing this file (directly or not): this annotate applies only in builds including this file — import it from ${relDocPath(this.id, target.id)} or from the files using "${owner.node.id}"`,
9731
- range: token.range
9774
+ message: `"${name}" (declared in ${declared.path}) is used by ${pathList(local)} without importing this file (directly or not): this annotate applies only in builds including this file — import it from ${from}the files using "${name}"`,
9775
+ range
9732
9776
  });
9733
9777
  }
9778
+ if (other.length > 0) messages.push({
9779
+ severity: 2,
9780
+ message: `"${name}" (declared in ${declared.path}) is used by files of other packages (${pathList(other)}), which cannot import this file: their packages' own builds never include this annotate, and at runtime it applies to those files only if this file is loaded before they first use "${name}" — to change only your own types, annotate a type of your own (interface X extends ${name}, or annotate ${name} as X)`,
9781
+ range
9782
+ });
9734
9783
  }
9735
9784
  return messages;
9736
9785
  }
@@ -10426,6 +10475,12 @@ const zeroRange = {
10426
10475
  for (const part of chain) ref.addChain(token(part));
10427
10476
  return ref;
10428
10477
  }
10478
+ /** The `path`s of `items`, sorted, comma-separated. */ function pathList(items) {
10479
+ return items.map((u) => u.path).sort().join(", ");
10480
+ }
10481
+ /** The package of file document `doc` (see `packageOfFile`); `undefined` for any other document. */ function packageOfDoc(doc) {
10482
+ return doc.id.startsWith("file://") ? packageOfFile(fileUriToPath(doc.id)) : void 0;
10483
+ }
10429
10484
  /** `to`'s path relative to `from` (`./b.as`), or its id when not a file. */ function relDocPath(from, to) {
10430
10485
  try {
10431
10486
  return `${getRelPath(from, to)}.as`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",