@stateward/core 0.0.0-stage → 1.0.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 (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -2
  3. package/dist/backend/comments.d.ts +24 -0
  4. package/dist/backend/comments.js +121 -0
  5. package/dist/backend/fingerprints.d.ts +72 -0
  6. package/dist/backend/fingerprints.js +584 -0
  7. package/dist/backend/package-source.d.ts +10 -0
  8. package/dist/backend/package-source.js +30 -0
  9. package/dist/backend/program-record.d.ts +41 -0
  10. package/dist/backend/program-record.js +94 -0
  11. package/dist/backend/qualified-ids.d.ts +4 -0
  12. package/dist/backend/qualified-ids.js +192 -0
  13. package/dist/backend/typescript6.d.ts +201 -0
  14. package/dist/backend/typescript6.js +2002 -0
  15. package/dist/baseline.d.ts +104 -0
  16. package/dist/baseline.js +352 -0
  17. package/dist/cache.d.ts +138 -0
  18. package/dist/cache.js +349 -0
  19. package/dist/checker.d.ts +147 -0
  20. package/dist/checker.js +223 -0
  21. package/dist/config-file.d.ts +29 -0
  22. package/dist/config-file.js +222 -0
  23. package/dist/config.d.ts +23 -0
  24. package/dist/config.js +953 -0
  25. package/dist/define.d.ts +16 -0
  26. package/dist/define.js +8 -0
  27. package/dist/disk-cache.d.ts +60 -0
  28. package/dist/disk-cache.js +205 -0
  29. package/dist/disk-limits.d.ts +19 -0
  30. package/dist/disk-limits.js +85 -0
  31. package/dist/display-path.d.ts +11 -0
  32. package/dist/display-path.js +13 -0
  33. package/dist/engine.d.ts +146 -0
  34. package/dist/engine.js +985 -0
  35. package/dist/fixes.d.ts +17 -0
  36. package/dist/fixes.js +262 -0
  37. package/dist/graph.d.ts +61 -0
  38. package/dist/graph.js +186 -0
  39. package/dist/ignores.d.ts +78 -0
  40. package/dist/ignores.js +470 -0
  41. package/dist/implementation.d.ts +23 -0
  42. package/dist/implementation.js +313 -0
  43. package/dist/index.d.ts +20 -0
  44. package/dist/index.js +18 -0
  45. package/dist/internal.d.ts +33 -0
  46. package/dist/internal.js +26 -0
  47. package/dist/lazy.d.ts +49 -0
  48. package/dist/lazy.js +120 -0
  49. package/dist/options.d.ts +89 -0
  50. package/dist/options.js +178 -0
  51. package/dist/overrides.d.ts +10 -0
  52. package/dist/overrides.js +36 -0
  53. package/dist/package-cache.d.ts +74 -0
  54. package/dist/package-cache.js +320 -0
  55. package/dist/plan.d.ts +62 -0
  56. package/dist/plan.js +105 -0
  57. package/dist/rollout.d.ts +24 -0
  58. package/dist/rollout.js +106 -0
  59. package/dist/severity.d.ts +32 -0
  60. package/dist/severity.js +112 -0
  61. package/dist/testing.d.ts +33 -0
  62. package/dist/testing.js +165 -0
  63. package/dist/timing.d.ts +85 -0
  64. package/dist/timing.js +141 -0
  65. package/dist/tree.d.ts +39 -0
  66. package/dist/tree.js +64 -0
  67. package/dist/tsconfigs.d.ts +12 -0
  68. package/dist/tsconfigs.js +76 -0
  69. package/dist/types.d.ts +1038 -0
  70. package/dist/types.js +2 -0
  71. package/dist/typescript.d.ts +2 -0
  72. package/dist/typescript.js +5 -0
  73. package/dist/vocabulary.d.ts +15 -0
  74. package/dist/vocabulary.js +27 -0
  75. package/package.json +54 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 the stateward authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,12 @@
1
- # Temporary Holding Version
1
+ # @stateward/core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The engine of [stateward](https://github.com/khoing1111/ts-stateward#readme) and the API for
4
+ writing rules: `defineRule`, `definePlugin`, `option`, and test helpers in
5
+ `@stateward/core/testing`. `stateward` installs it; add it yourself only to write a rule.
6
+
7
+ ```bash
8
+ npm install --save-dev @stateward/core
9
+ ```
10
+
11
+ How to write, load and test a rule:
12
+ [docs/user/write-your-own-rule.md](https://github.com/khoing1111/ts-stateward/blob/main/docs/user/write-your-own-rule.md).
@@ -0,0 +1,24 @@
1
+ import ts from "typescript";
2
+ /** A comment that holds the marker: where it is and its text, with its `//` or `/*` marks. */
3
+ export type MarkedComment = {
4
+ readonly start: number;
5
+ readonly end: number;
6
+ readonly text: string;
7
+ /** Before the first statement of the file. */
8
+ readonly beforeCode: boolean;
9
+ /**
10
+ * The statement (or declaration, member, property) that starts at the next code after the
11
+ * comment: where it starts and where it ends. Undefined when no node starts there (the comment
12
+ * sits inside an expression), or the next code is a lone token.
13
+ */
14
+ readonly next: {
15
+ readonly start: number;
16
+ readonly end: number;
17
+ } | undefined;
18
+ };
19
+ /**
20
+ * The comments of a file that contain `marker`, in order. Syntax only (no type checker): a marker
21
+ * inside a string, a template or JSX text is not in a comment and is left out.
22
+ */
23
+ export declare function markedComments(sourceFile: ts.SourceFile, marker: string): MarkedComment[];
24
+ //# sourceMappingURL=comments.d.ts.map
@@ -0,0 +1,121 @@
1
+ import ts from "typescript";
2
+ /** The offset of the first code after `from`, skipping blanks and comments. */
3
+ function codeAfter(text, from) {
4
+ let at = from;
5
+ for (;;) {
6
+ while (at < text.length && /\s/.test(text.charAt(at)))
7
+ at += 1;
8
+ if (text.startsWith("//", at)) {
9
+ const newline = text.indexOf("\n", at);
10
+ if (newline === -1)
11
+ return text.length;
12
+ at = newline + 1;
13
+ }
14
+ else if (text.startsWith("/*", at)) {
15
+ const close = text.indexOf("*/", at + 2);
16
+ if (close === -1)
17
+ return text.length;
18
+ at = close + 2;
19
+ }
20
+ else
21
+ return at;
22
+ }
23
+ }
24
+ /** The body of a compound statement: an ignore above `if (x) {` covers its head, not its body. */
25
+ function bodyOf(node) {
26
+ if (ts.isIfStatement(node))
27
+ return node.thenStatement;
28
+ if (ts.isForStatement(node) ||
29
+ ts.isForInStatement(node) ||
30
+ ts.isForOfStatement(node) ||
31
+ ts.isWhileStatement(node) ||
32
+ ts.isDoStatement(node) ||
33
+ ts.isLabeledStatement(node) ||
34
+ ts.isWithStatement(node)) {
35
+ return ts.isLabeledStatement(node) ? undefined : node.statement;
36
+ }
37
+ if (ts.isTryStatement(node))
38
+ return node.tryBlock;
39
+ if (ts.isSwitchStatement(node))
40
+ return node.caseBlock;
41
+ return ts.isBlock(node) ? node : undefined;
42
+ }
43
+ /**
44
+ * The outermost node that starts exactly at `offset`, as the range an ignore above it covers: the
45
+ * whole statement or declaration; for `if`, loops, `try` and `switch` only up to the `{` of the
46
+ * body, so an ignore above `if (x) {` does not hide the findings inside it.
47
+ */
48
+ function nodeStartingAt(sourceFile, offset) {
49
+ let node = sourceFile;
50
+ for (;;) {
51
+ let next;
52
+ ts.forEachChild(node, (child) => {
53
+ if (next === undefined && child.getStart(sourceFile) <= offset && offset < child.end) {
54
+ next = child;
55
+ }
56
+ });
57
+ if (next === undefined)
58
+ return undefined;
59
+ if (next.getStart(sourceFile) === offset) {
60
+ const body = bodyOf(next);
61
+ return {
62
+ start: offset,
63
+ end: body === undefined
64
+ ? next.end
65
+ : ts.isBlock(body) || ts.isCaseBlock(body)
66
+ ? body.getStart(sourceFile) + 1
67
+ : body.end,
68
+ };
69
+ }
70
+ node = next;
71
+ }
72
+ }
73
+ /** The comment that contains `offset`, found through the tokens, so text in a string or JSX is not one. */
74
+ function commentAt(sourceFile, offset) {
75
+ let node = sourceFile;
76
+ for (;;) {
77
+ const child = node
78
+ .getChildren(sourceFile)
79
+ .find((candidate) => candidate.pos <= offset && offset < candidate.end);
80
+ if (child === undefined || ts.isJSDoc(child))
81
+ return undefined;
82
+ if (offset < child.getStart(sourceFile)) {
83
+ // Comments after the previous token on its own line are trailing ones; the rest are leading.
84
+ const ranges = [
85
+ ...(ts.getTrailingCommentRanges(sourceFile.text, child.pos) ?? []),
86
+ ...(ts.getLeadingCommentRanges(sourceFile.text, child.pos) ?? []),
87
+ ];
88
+ return ranges.find((range) => range.pos <= offset && offset < range.end);
89
+ }
90
+ if (child.getChildCount(sourceFile) === 0)
91
+ return undefined;
92
+ node = child;
93
+ }
94
+ }
95
+ /**
96
+ * The comments of a file that contain `marker`, in order. Syntax only (no type checker): a marker
97
+ * inside a string, a template or JSX text is not in a comment and is left out.
98
+ */
99
+ export function markedComments(sourceFile, marker) {
100
+ const { text } = sourceFile;
101
+ const firstStatement = sourceFile.statements[0]?.getStart(sourceFile) ?? text.length;
102
+ const found = [];
103
+ let from = text.indexOf(marker);
104
+ while (from !== -1) {
105
+ const range = commentAt(sourceFile, from);
106
+ if (range !== undefined) {
107
+ const code = codeAfter(text, range.end);
108
+ found.push({
109
+ next: code >= text.length ? undefined : nodeStartingAt(sourceFile, code),
110
+ start: range.pos,
111
+ end: range.end,
112
+ text: text.slice(range.pos, range.end),
113
+ beforeCode: range.end <= firstStatement,
114
+ });
115
+ }
116
+ // Continue after this comment, so two markers in one comment give one result.
117
+ from = text.indexOf(marker, Math.max(from + marker.length, range?.end ?? 0));
118
+ }
119
+ return found;
120
+ }
121
+ //# sourceMappingURL=comments.js.map
@@ -0,0 +1,72 @@
1
+ import ts from "typescript";
2
+ /** The hash of a parsed file's text, computed once per parsed file. */
3
+ export declare function textHash(sourceFile: ts.SourceFile): string;
4
+ /**
5
+ * The hash of what a file shows its importers through the type checker (`shapeText`), or of its
6
+ * full text when it has no smaller shape. Computed once per parsed file: it is syntax only.
7
+ */
8
+ export declare function shapeHash(sourceFile: ts.SourceFile): string;
9
+ /**
10
+ * What an own TypeScript module shows the files that import it, as canonical text; undefined for
11
+ * any other file (a declaration file, JavaScript, a script, a library file): its full text is its
12
+ * shape. Syntax only.
13
+ *
14
+ * The module's statements as their tokens, without comments or layout, except:
15
+ *
16
+ * - a body the type checker never reads for the function's type: a function, method or getter
17
+ * with a declared return type, a setter, a constructor, a static block;
18
+ * - the initializer of a variable, property or parameter with a declared type (only that the
19
+ * initializer is there is kept: it makes a parameter optional);
20
+ * - a declaration the module does not export and that nothing kept names (it is only used inside
21
+ * a body left out): nothing outside the module can reach it through types.
22
+ *
23
+ * Kept as well: every parsed JSDoc comment on what is kept (`TypeQueries.calleeTags` reads its
24
+ * tags), the comments at the top of the file that hold a pragma or a directive (`@jsx…`,
25
+ * `/// <reference …/>`), and the offset of each top-level `X.y = …` (the qualified id of `y`
26
+ * counts from the top of the file: qualified-ids.ts).
27
+ *
28
+ * A function without a declared return type keeps its body: its type is inferred from it.
29
+ */
30
+ export declare function shapeText(sourceFile: ts.SourceFile): string | undefined;
31
+ /**
32
+ * The folder above a forward-slash folder, or none at a root: `/`, a drive (`C:`, `C:/`) or a UNC
33
+ * share (`//server/share`). `path.posix.dirname("C:")` is `"."`, which would read the working
34
+ * directory's package.json, so a relative result is never returned either.
35
+ */
36
+ export declare function parentFolder(folder: string): string | undefined;
37
+ /** The name of the package a file belongs to, as `TypeQueries.packageOf` answers it. */
38
+ export type PackageOf = (file: string) => string | undefined;
39
+ /**
40
+ * `packageOf` for one program, reading package.json files through the program's compiler host, so
41
+ * a recorded program (`program-record.ts`) records them and a changed one is not replayed. Each
42
+ * folder is read once per program.
43
+ */
44
+ export declare function packageNames(host: ts.CompilerHost): PackageOf;
45
+ /**
46
+ * The hash of the nearest package.json above a file, read through the program's compiler host as
47
+ * `packageNames` reads it: a library file's fingerprint carries its package's manifest, so a new
48
+ * version of a package (whose facts a rule may have read) changes the
49
+ * fingerprints of the files that import it, even when its declarations are the same.
50
+ */
51
+ export declare function packageManifests(host: ts.CompilerHost): PackageOf;
52
+ /** Whether a file is an entry of its package, as `TypeQueries.isPackageEntry` answers it. */
53
+ export type IsPackageEntry = (file: string) => boolean;
54
+ /**
55
+ * `isPackageEntry` for one program: the nearest package.json (any, named or not) is read through
56
+ * the program's compiler host, like `packageNames`, so a changed manifest is seen by a recorded
57
+ * program.
58
+ */
59
+ export declare function packageEntries(host: ts.CompilerHost): IsPackageEntry;
60
+ /** The packages a file's project depends on, as `TypeQueries.dependenciesOf` answers it. */
61
+ export type DependenciesOf = (file: string) => readonly string[];
62
+ /**
63
+ * `dependenciesOf` for one program: the nearest package.json above a file (any, named or not) is
64
+ * read through the program's compiler host, like `packageEntries`, so a changed manifest is seen
65
+ * by a recorded program and changes the fingerprints of the files under it.
66
+ */
67
+ export declare function packageDependencies(host: ts.CompilerHost): DependenciesOf;
68
+ /** The fingerprint of each source file of `program`, by file name; see above. */
69
+ export declare function computeFingerprints(program: ts.Program, own: ReadonlySet<string>, packageOf: PackageOf, isEntry: IsPackageEntry, dependenciesOf: DependenciesOf,
70
+ /** `packageManifests`: a library file's label carries its package.json's hash. */
71
+ manifestOf?: PackageOf): ReadonlyMap<string, string>;
72
+ //# sourceMappingURL=fingerprints.d.ts.map