@code-sentinel-cli/code-sentinel-cli 3.0.3 → 3.0.5

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 (133) hide show
  1. package/README.md +1 -1
  2. package/dist/bin.js +1191 -816
  3. package/dist/builtin-rules/ai/angular/api-service-contract-risk.prompt.md +71 -0
  4. package/dist/builtin-rules/ai/angular/api-service-contract-risk.rule.yaml +22 -0
  5. package/dist/builtin-rules/ai/angular/component-state-flow.prompt.md +79 -0
  6. package/dist/builtin-rules/ai/angular/component-state-flow.rule.yaml +26 -0
  7. package/dist/builtin-rules/ai/angular/cross-feature-api-drift.prompt.md +70 -0
  8. package/dist/builtin-rules/ai/angular/cross-feature-api-drift.rule.yaml +42 -0
  9. package/dist/builtin-rules/ai/angular/cross-feature-route-permission-drift.prompt.md +73 -0
  10. package/dist/builtin-rules/ai/angular/cross-feature-route-permission-drift.rule.yaml +50 -0
  11. package/dist/builtin-rules/ai/angular/reactive-form-flow.prompt.md +82 -0
  12. package/dist/builtin-rules/ai/angular/reactive-form-flow.rule.yaml +23 -0
  13. package/dist/builtin-rules/ai/angular/route-permission-flow.prompt.md +80 -0
  14. package/dist/builtin-rules/ai/angular/route-permission-flow.rule.yaml +23 -0
  15. package/dist/builtin-rules/ai/angular/rxjs-flow-risk.prompt.md +82 -0
  16. package/dist/builtin-rules/ai/angular/rxjs-flow-risk.rule.yaml +24 -0
  17. package/dist/builtin-rules/ai/angular/template-contract-risk.prompt.md +80 -0
  18. package/dist/builtin-rules/ai/angular/template-contract-risk.rule.yaml +25 -0
  19. package/dist/builtin-rules/ai/flutter/dynamic-content-risk.prompt.md +55 -0
  20. package/dist/builtin-rules/ai/flutter/dynamic-content-risk.rule.yaml +18 -0
  21. package/dist/builtin-rules/ai/flutter/feature-state-flow-risk.prompt.md +58 -0
  22. package/dist/builtin-rules/ai/flutter/feature-state-flow-risk.rule.yaml +18 -0
  23. package/dist/builtin-rules/ai/flutter/native-platform-consistency-risk.prompt.md +56 -0
  24. package/dist/builtin-rules/ai/flutter/native-platform-consistency-risk.rule.yaml +18 -0
  25. package/dist/builtin-rules/ai/flutter/network-auth-flow-risk.prompt.md +57 -0
  26. package/dist/builtin-rules/ai/flutter/network-auth-flow-risk.rule.yaml +19 -0
  27. package/dist/builtin-rules/ai/flutter/ui-responsive-accessibility-risk.prompt.md +60 -0
  28. package/dist/builtin-rules/ai/flutter/ui-responsive-accessibility-risk.rule.yaml +18 -0
  29. package/dist/builtin-rules/ai/general/code-clarity.prompt.md +57 -0
  30. package/dist/builtin-rules/ai/general/code-clarity.rule.yaml +20 -0
  31. package/dist/builtin-rules/ai/spring-boot/business-invariant-risk.prompt.md +86 -0
  32. package/dist/builtin-rules/ai/spring-boot/business-invariant-risk.rule.yaml +25 -0
  33. package/dist/builtin-rules/ai/spring-boot/cache-consistency-risk.prompt.md +80 -0
  34. package/dist/builtin-rules/ai/spring-boot/cache-consistency-risk.rule.yaml +25 -0
  35. package/dist/builtin-rules/ai/spring-boot/cross-feature-error-handling-drift.prompt.md +76 -0
  36. package/dist/builtin-rules/ai/spring-boot/cross-feature-error-handling-drift.rule.yaml +63 -0
  37. package/dist/builtin-rules/ai/spring-boot/cross-feature-transaction-boundary-drift.prompt.md +85 -0
  38. package/dist/builtin-rules/ai/spring-boot/cross-feature-transaction-boundary-drift.rule.yaml +68 -0
  39. package/dist/builtin-rules/ai/spring-boot/error-handling-completeness.prompt.md +121 -0
  40. package/dist/builtin-rules/ai/spring-boot/error-handling-completeness.rule.yaml +84 -0
  41. package/dist/builtin-rules/ai/spring-boot/event-publishing-risk.prompt.md +80 -0
  42. package/dist/builtin-rules/ai/spring-boot/event-publishing-risk.rule.yaml +25 -0
  43. package/dist/builtin-rules/ai/spring-boot/io-boundary-risk.prompt.md +78 -0
  44. package/dist/builtin-rules/ai/spring-boot/io-boundary-risk.rule.yaml +25 -0
  45. package/dist/builtin-rules/ai/spring-boot/ownership-scope-risk.prompt.md +83 -0
  46. package/dist/builtin-rules/ai/spring-boot/ownership-scope-risk.rule.yaml +25 -0
  47. package/dist/builtin-rules/ai/spring-boot/pagination-risk.prompt.md +81 -0
  48. package/dist/builtin-rules/ai/spring-boot/pagination-risk.rule.yaml +25 -0
  49. package/dist/builtin-rules/ai/spring-boot/security-authorization-risk.prompt.md +75 -0
  50. package/dist/builtin-rules/ai/spring-boot/security-authorization-risk.rule.yaml +26 -0
  51. package/dist/builtin-rules/ai/spring-boot/security-mass-assignment-risk.prompt.md +75 -0
  52. package/dist/builtin-rules/ai/spring-boot/security-mass-assignment-risk.rule.yaml +24 -0
  53. package/dist/builtin-rules/ai/spring-boot/security-redirect-risk.prompt.md +74 -0
  54. package/dist/builtin-rules/ai/spring-boot/security-redirect-risk.rule.yaml +24 -0
  55. package/dist/builtin-rules/ai/spring-boot/security-sensitive-data-risk.prompt.md +75 -0
  56. package/dist/builtin-rules/ai/spring-boot/security-sensitive-data-risk.rule.yaml +25 -0
  57. package/dist/builtin-rules/ai/spring-boot/transaction-loop-risk.prompt.md +78 -0
  58. package/dist/builtin-rules/ai/spring-boot/transaction-loop-risk.rule.yaml +25 -0
  59. package/dist/builtin-rules/static/angular/component-prefix.rule.js +78 -0
  60. package/dist/builtin-rules/static/angular/image-control-alt-required.rule.js +105 -0
  61. package/dist/builtin-rules/static/angular/img-alt-required.rule.js +96 -0
  62. package/dist/builtin-rules/static/angular/inject-pattern.rule.js +47 -0
  63. package/dist/builtin-rules/static/angular/lifecycle-method-required.rule.js +59 -0
  64. package/dist/builtin-rules/static/angular/no-any-type.rule.yaml +55 -0
  65. package/dist/builtin-rules/static/angular/no-direct-dom-access.rule.js +191 -0
  66. package/dist/builtin-rules/static/angular/no-dom-xss.rule.js +48 -0
  67. package/dist/builtin-rules/static/angular/no-leaky-subscription.rule.js +197 -0
  68. package/dist/builtin-rules/static/angular/no-ngmodule.rule.yaml +44 -0
  69. package/dist/builtin-rules/static/angular/no-non-null-asserted-optional-chain.rule.js +53 -0
  70. package/dist/builtin-rules/static/angular/no-uncalled-signal-in-condition.rule.js +112 -0
  71. package/dist/builtin-rules/static/angular/observable-naming.rule.js +85 -0
  72. package/dist/builtin-rules/static/angular/onpush-required.rule.js +70 -0
  73. package/dist/builtin-rules/static/angular/standalone-component-required.rule.js +65 -0
  74. package/dist/builtin-rules/static/angular/template-dom-xss.rule.js +112 -0
  75. package/dist/builtin-rules/static/angular/template-syntax-modern.rule.js +154 -0
  76. package/dist/builtin-rules/static/angular/trackby-required.rule.js +66 -0
  77. package/dist/builtin-rules/static/flutter/android-exported-component.rule.js +75 -0
  78. package/dist/builtin-rules/static/flutter/android-manifest-hardening.rule.js +117 -0
  79. package/dist/builtin-rules/static/flutter/android-sensitive-permissions.rule.js +87 -0
  80. package/dist/builtin-rules/static/flutter/debug-tooling-release-risk.rule.js +90 -0
  81. package/dist/builtin-rules/static/flutter/dynamic-archive-risk.rule.js +96 -0
  82. package/dist/builtin-rules/static/flutter/hive-sensitive-storage.rule.js +77 -0
  83. package/dist/builtin-rules/static/flutter/ios-privacy-hardening.rule.js +119 -0
  84. package/dist/builtin-rules/static/flutter/packaged-env-risk.rule.js +95 -0
  85. package/dist/builtin-rules/static/flutter/sensitive-log-leak.rule.js +94 -0
  86. package/dist/builtin-rules/static/flutter/webview-bridge-guard.rule.js +102 -0
  87. package/dist/builtin-rules/static/general/file-too-long.rule.js +41 -0
  88. package/dist/builtin-rules/static/general/function-too-long.rule.js +72 -0
  89. package/dist/builtin-rules/static/general/no-circular-import.rule.js +39 -0
  90. package/dist/builtin-rules/static/general/no-code-injection.rule.js +48 -0
  91. package/dist/builtin-rules/static/general/no-console-log.rule.js +110 -0
  92. package/dist/builtin-rules/static/general/no-empty-catch.rule.yaml +56 -0
  93. package/dist/builtin-rules/static/general/no-hardcoded-secret.rule.js +517 -0
  94. package/dist/builtin-rules/static/general/no-magic-number.rule.js +78 -0
  95. package/dist/builtin-rules/static/general/no-open-redirect.rule.js +46 -0
  96. package/dist/builtin-rules/static/general/no-todo-comment.rule.yaml +30 -0
  97. package/dist/builtin-rules/static/general/no-unresolved-import-symbol.rule.js +80 -0
  98. package/dist/builtin-rules/static/general/no-unused-export.rule.js +140 -0
  99. package/dist/builtin-rules/static/spring-boot/no-autowired-field.rule.js +162 -0
  100. package/dist/builtin-rules/static/spring-boot/no-business-logic-in-controller.rule.js +94 -0
  101. package/dist/builtin-rules/static/spring-boot/no-command-injection.rule.js +47 -0
  102. package/dist/builtin-rules/static/spring-boot/no-entity-in-controller.rule.yaml +52 -0
  103. package/dist/builtin-rules/static/spring-boot/no-nplus1-query-in-loop.rule.js +106 -0
  104. package/dist/builtin-rules/static/spring-boot/no-path-traversal.rule.js +47 -0
  105. package/dist/builtin-rules/static/spring-boot/no-spel-injection.rule.js +41 -0
  106. package/dist/builtin-rules/static/spring-boot/no-sql-string-concat.rule.js +194 -0
  107. package/dist/builtin-rules/static/spring-boot/no-ssrf.rule.js +49 -0
  108. package/dist/builtin-rules/static/spring-boot/no-transactional-self-invocation.rule.js +142 -0
  109. package/dist/builtin-rules/static/spring-boot/no-unclosed-resource.rule.js +108 -0
  110. package/dist/builtin-rules/static/spring-boot/no-unsafe-shared-formatter.rule.js +118 -0
  111. package/dist/builtin-rules/static/spring-boot/repository-naming.rule.yaml +42 -0
  112. package/dist/builtin-rules/static/spring-boot/restful-naming.rule.yaml +39 -0
  113. package/dist/builtin-rules/static/spring-boot/transaction-on-service.rule.yaml +58 -0
  114. package/dist/builtin-rules/static/spring-boot/validate-input.rule.js +123 -0
  115. package/dist/builtin-rules/vauthz/static/spring-boot/require-authz-check.rule.js +101 -0
  116. package/dist/index.d.ts +225 -48
  117. package/dist/index.js +1190 -815
  118. package/node_modules/@code-sentinel/core/index.js +55 -0
  119. package/node_modules/@code-sentinel/core/package.json +6 -0
  120. package/node_modules/@code-sentinel/parsers/index.js +13 -0
  121. package/node_modules/@code-sentinel/parsers/package.json +6 -0
  122. package/package.json +34 -29
  123. package/src/migrations/0000_initial.sql +62 -0
  124. package/src/migrations/0001_add_confidence.sql +7 -0
  125. package/src/migrations/0002_add_ai_stats.sql +21 -0
  126. package/src/migrations/0003_add_fix_recipe.sql +29 -0
  127. package/src/migrations/0004_add_ast_graph.sql +61 -0
  128. package/src/migrations/0005_add_project_id.sql +1 -0
  129. package/src/migrations/0006_add_ai_model.sql +5 -0
  130. package/src/migrations/0006_add_rule_results.sql +1 -0
  131. package/src/migrations/0007_add_finding_metadata.sql +4 -0
  132. package/src/migrations/0008_add_coverage.sql +2 -0
  133. package/src/migrations/0009_run_provenance.sql +5 -0
@@ -0,0 +1,197 @@
1
+ import { asTsMorphSourceFile } from '@code-sentinel/parsers';
2
+ import { Node } from 'ts-morph';
3
+
4
+ // builtin-rules/static/angular/no-leaky-subscription.rule.ts
5
+ var TERMINATING_OPERATORS = /* @__PURE__ */ new Set([
6
+ "takeUntil",
7
+ "takeUntilDestroyed",
8
+ "take",
9
+ "takeWhile",
10
+ "first",
11
+ "last",
12
+ "single",
13
+ "toArray"
14
+ ]);
15
+ var LONG_LIVED_MEMBERS = /* @__PURE__ */ new Set([
16
+ "valueChanges",
17
+ "statusChanges",
18
+ "params",
19
+ "queryParams",
20
+ "paramMap",
21
+ "queryParamMap",
22
+ "fragment",
23
+ "events"
24
+ // Router.events
25
+ ]);
26
+ var LONG_LIVED_FACTORIES = /* @__PURE__ */ new Set(["fromEvent", "interval", "timer"]);
27
+ var ONE_SHOT_MEMBERS = /* @__PURE__ */ new Set([
28
+ "afterClose",
29
+ "afterClosed",
30
+ "afterOpened",
31
+ "afterAllClosed",
32
+ "beforeClose",
33
+ "onClose"
34
+ ]);
35
+ var ONE_SHOT_FACTORIES = /* @__PURE__ */ new Set(["forkJoin"]);
36
+ var HTTP_RECEIVER_RE = /(service|http|api|client)$/i;
37
+ var MANAGED_COLLECTOR_METHODS = /* @__PURE__ */ new Set(["add"]);
38
+ var isTerminatingPipe = (expression) => {
39
+ if (!Node.isCallExpression(expression)) return false;
40
+ const inner = expression.getExpression();
41
+ if (!Node.isPropertyAccessExpression(inner)) return false;
42
+ if (inner.getName() !== "pipe") return false;
43
+ for (const arg of expression.getArguments()) {
44
+ if (!Node.isCallExpression(arg)) continue;
45
+ const argExpr = arg.getExpression();
46
+ if (Node.isIdentifier(argExpr) && TERMINATING_OPERATORS.has(argExpr.getText())) {
47
+ return true;
48
+ }
49
+ }
50
+ return false;
51
+ };
52
+ var unwrapPipe = (node) => {
53
+ let cur = node;
54
+ while (Node.isCallExpression(cur)) {
55
+ const e = cur.getExpression();
56
+ if (Node.isPropertyAccessExpression(e) && e.getName() === "pipe") {
57
+ cur = e.getExpression();
58
+ continue;
59
+ }
60
+ break;
61
+ }
62
+ return cur;
63
+ };
64
+ var receiverName = (node) => {
65
+ if (Node.isPropertyAccessExpression(node)) return node.getName();
66
+ if (Node.isIdentifier(node)) return node.getText();
67
+ return "";
68
+ };
69
+ var trailingName = (node) => {
70
+ if (Node.isPropertyAccessExpression(node)) return node.getName();
71
+ if (Node.isIdentifier(node)) return node.getText();
72
+ return void 0;
73
+ };
74
+ var classifySource = (receiver) => {
75
+ const src = unwrapPipe(receiver);
76
+ if (Node.isPropertyAccessExpression(src) && LONG_LIVED_MEMBERS.has(src.getName())) {
77
+ return "long-lived";
78
+ }
79
+ if (Node.isCallExpression(src)) {
80
+ const callee = src.getExpression();
81
+ if (Node.isIdentifier(callee) && LONG_LIVED_FACTORIES.has(callee.getText())) {
82
+ return "long-lived";
83
+ }
84
+ if (Node.isIdentifier(callee) && ONE_SHOT_FACTORIES.has(callee.getText())) {
85
+ return "one-shot";
86
+ }
87
+ if (Node.isPropertyAccessExpression(callee)) {
88
+ if (ONE_SHOT_MEMBERS.has(callee.getName())) return "one-shot";
89
+ if (HTTP_RECEIVER_RE.test(receiverName(callee.getExpression()))) return "one-shot";
90
+ }
91
+ }
92
+ if (Node.isPropertyAccessExpression(src) && ONE_SHOT_MEMBERS.has(src.getName())) {
93
+ return "one-shot";
94
+ }
95
+ return "unclassified";
96
+ };
97
+ var collectUnsubscribedNames = (sf) => {
98
+ const names = /* @__PURE__ */ new Set();
99
+ sf.forEachDescendant((node) => {
100
+ if (!Node.isCallExpression(node)) return;
101
+ const expr = node.getExpression();
102
+ if (!Node.isPropertyAccessExpression(expr)) return;
103
+ if (expr.getName() !== "unsubscribe") return;
104
+ const name = trailingName(expr.getExpression());
105
+ if (name !== void 0) names.add(name);
106
+ });
107
+ return names;
108
+ };
109
+ var assignmentTargetName = (subscribeCall) => {
110
+ const parent = subscribeCall.getParent();
111
+ if (parent === void 0) return void 0;
112
+ if (Node.isBinaryExpression(parent) && parent.getOperatorToken().getText() === "=") {
113
+ return trailingName(parent.getLeft());
114
+ }
115
+ if (Node.isVariableDeclaration(parent)) {
116
+ const nameNode = parent.getNameNode();
117
+ if (Node.isIdentifier(nameNode)) return nameNode.getText();
118
+ }
119
+ return void 0;
120
+ };
121
+ var isManagedSubscription = (subscribeCall, unsubscribedNames) => {
122
+ const parent = subscribeCall.getParent();
123
+ if (parent !== void 0 && Node.isCallExpression(parent)) {
124
+ const callee = parent.getExpression();
125
+ if (Node.isPropertyAccessExpression(callee) && MANAGED_COLLECTOR_METHODS.has(callee.getName())) {
126
+ return true;
127
+ }
128
+ }
129
+ const target = assignmentTargetName(subscribeCall);
130
+ return target !== void 0 && unsubscribedNames.has(target);
131
+ };
132
+ var findingFor = (nameNode, file, kind) => {
133
+ const start = nameNode.getStart();
134
+ const { line, column } = nameNode.getSourceFile().getLineAndColumnAtPos(start);
135
+ const range = { line, column };
136
+ if (kind === "long-lived") {
137
+ return {
138
+ message: "subscribe() v\xE0o stream long-lived (valueChanges/params/fromEvent/interval\u2026) kh\xF4ng h\u1EE7y \u2014 memory leak khi component hu\u1EF7.",
139
+ file,
140
+ range,
141
+ suggestion: "B\u1ECDc `.pipe(takeUntilDestroyed())` / `.pipe(takeUntil(this.destroy$))`, ho\u1EB7c d\xF9ng `async` pipe trong template.",
142
+ confidence: 80,
143
+ severity: "error"
144
+ };
145
+ }
146
+ if (kind === "one-shot") {
147
+ return {
148
+ message: "subscribe() v\xE0o ngu\u1ED3n one-shot (HTTP / afterClosed / forkJoin) kh\xF4ng h\u1EE7y \u2014 n\xEAn d\xF9ng takeUntilDestroyed \u0111\u1EC3 tr\xE1nh callback ch\u1EA1y sau khi component hu\u1EF7 (kh\xF4ng ph\u1EA3i memory leak).",
149
+ file,
150
+ range,
151
+ suggestion: "Th\xEAm `.pipe(takeUntilDestroyed())` ho\u1EB7c d\xF9ng `async` pipe / `firstValueFrom`.",
152
+ confidence: 70,
153
+ severity: "warning"
154
+ };
155
+ }
156
+ return {
157
+ message: "subscribe() kh\xF4ng th\u1EA5y c\u01A1 ch\u1EBF h\u1EE7y (takeUntil/take/unsubscribe/Subscription.add) \u2014 n\xEAn h\u1EE7y \u0111\u1EC3 tr\xE1nh leak ho\u1EB7c callback sau khi hu\u1EF7.",
158
+ file,
159
+ range,
160
+ suggestion: "L\u01B0u subscription r\u1ED3i `unsubscribe()` trong ngOnDestroy, gom v\xE0o m\u1ED9t `Subscription`, ho\u1EB7c d\xF9ng `takeUntilDestroyed()` / `async` pipe.",
161
+ confidence: 60,
162
+ severity: "warning"
163
+ };
164
+ };
165
+ var rule = {
166
+ id: "angular/no-leaky-subscription",
167
+ version: 4,
168
+ kind: "static-ts",
169
+ title: "Subscription ph\u1EA3i \u0111\u01B0\u1EE3c h\u1EE7y (takeUntil / unsubscribe / Subscription.add / async pipe)",
170
+ description: "Flags bare .subscribe() calls WITHOUT visible teardown. v4 (precision-first): SUPPRESSES managed subscriptions (stored + unsubscribe(), or added to a Subscription collection via .add()) and terminating pipes (takeUntil/take/first/\u2026). Severity is per-subscription: long-lived streams (valueChanges/params/fromEvent/interval) stay `error` (genuine leak); one-shot sources (HTTP call/afterClosed/forkJoin) and unclassified streams are `warning`.",
171
+ severity: "error",
172
+ category: "memory-safety",
173
+ languages: ["typescript"],
174
+ scope: "angular",
175
+ requires: "semantic-ts",
176
+ baseConfidence: 80,
177
+ check(ctx) {
178
+ const sf = asTsMorphSourceFile(ctx.parsed);
179
+ const findings = [];
180
+ const unsubscribedNames = collectUnsubscribedNames(sf);
181
+ sf.forEachDescendant((node) => {
182
+ if (!Node.isCallExpression(node)) return;
183
+ const expr = node.getExpression();
184
+ if (!Node.isPropertyAccessExpression(expr)) return;
185
+ if (expr.getName() !== "subscribe") return;
186
+ const receiver = expr.getExpression();
187
+ if (isTerminatingPipe(receiver)) return;
188
+ if (isManagedSubscription(node, unsubscribedNames)) return;
189
+ findings.push(findingFor(expr.getNameNode(), ctx.file.path, classifySource(receiver)));
190
+ });
191
+ return findings;
192
+ }
193
+ };
194
+
195
+ export { rule };
196
+ //# sourceMappingURL=no-leaky-subscription.rule.js.map
197
+ //# sourceMappingURL=no-leaky-subscription.rule.js.map
@@ -0,0 +1,44 @@
1
+ id: angular/no-ngmodule
2
+ version: 1
3
+ kind: static-yaml
4
+ title: "Angular 19 ưu tiên standalone components — tránh tạo NgModule mới"
5
+ description: |
6
+ Từ Angular 14+ standalone components đã ổn định; Angular 19 khuyến nghị
7
+ KHÔNG tạo NgModule mới. Standalone components đơn giản hơn, lazy-load
8
+ tốt hơn, tree-shaking tốt hơn.
9
+
10
+ Nếu đang migrate từ Angular legacy: giữ `@NgModule` cũ tạm thời, nhưng
11
+ code mới nên đi thẳng standalone. Disable rule này trong file `app.module.ts`
12
+ của legacy app nếu cần.
13
+
14
+ Phase 8 đơn giản: flag mọi `@NgModule(...)` decorator. Severity = info
15
+ để không block migration code.
16
+ severity: info
17
+ category: angular-best-practice
18
+ languages: [typescript]
19
+ scope: angular
20
+ baseConfidence: 100
21
+ detection:
22
+ type: tree-sitter
23
+ captureName: violation
24
+ query: |
25
+ (decorator (call_expression function: (identifier) @name)) @violation
26
+ (#eq? @name "NgModule")
27
+ message: "Tránh tạo `@NgModule` mới — Angular 19 ưu tiên standalone components."
28
+ suggestion: "Chuyển sang `@Component({ standalone: true, imports: [...] })` hoặc `@Directive({ standalone: true })`."
29
+ examples:
30
+ good: |
31
+ @Component({
32
+ standalone: true,
33
+ imports: [CommonModule, FormsModule],
34
+ selector: 'app-greeting',
35
+ template: '<p>Hello</p>',
36
+ })
37
+ export class GreetingComponent {}
38
+ bad: |
39
+ @NgModule({
40
+ declarations: [GreetingComponent],
41
+ imports: [CommonModule],
42
+ exports: [GreetingComponent],
43
+ })
44
+ export class GreetingModule {}
@@ -0,0 +1,53 @@
1
+ import { asTsMorphSourceFile } from '@code-sentinel/parsers';
2
+ import { SyntaxKind, Node } from 'ts-morph';
3
+
4
+ // builtin-rules/static/angular/no-non-null-asserted-optional-chain.rule.ts
5
+ function unwrapOuterParens(node) {
6
+ let cur = node;
7
+ while (Node.isParenthesizedExpression(cur)) cur = cur.getExpression();
8
+ return cur;
9
+ }
10
+ function assertsOptionalChain(nn) {
11
+ let cur = unwrapOuterParens(nn.getExpression());
12
+ while (Node.isPropertyAccessExpression(cur) || Node.isElementAccessExpression(cur) || Node.isCallExpression(cur)) {
13
+ if (Node.isCallExpression(cur)) {
14
+ if (cur.getQuestionDotTokenNode() !== void 0) return true;
15
+ } else if (cur.hasQuestionDotToken()) {
16
+ return true;
17
+ }
18
+ cur = cur.getExpression();
19
+ }
20
+ return false;
21
+ }
22
+ var rule = {
23
+ id: "angular/no-non-null-asserted-optional-chain",
24
+ version: 1,
25
+ kind: "static-ts",
26
+ title: "Non-null `!` \xE1p l\xEAn k\u1EBFt qu\u1EA3 optional-chain \u2014 `!` l\xE0 l\u1EDDi h\u1EE9a sai",
27
+ description: "Non-null assertion `!` \xE1p tr\u1EF1c ti\u1EBFp l\xEAn k\u1EBFt qu\u1EA3 optional-chain (`a?.b!`, `a?.[k]!`, `fn?.()!`) l\xE0 m\xE2u thu\u1EABn: `?.` t\u1ED3n t\u1EA1i v\xEC gi\xE1 tr\u1ECB C\xD3 TH\u1EC2 nullish, n\xEAn `a?.b` c\xF3 th\u1EC3 l\xE0 `undefined` (chain ng\u1EAFn m\u1EA1ch). `!` ch\u1EC9 l\xE0 to\xE1n t\u1EED compile-time \u2014 b\u1ECB xo\xE1 \u1EDF runtime n\xEAn KH\xD4NG ng\u0103n \u0111\u01B0\u1EE3c `undefined`, ch\u1EC9 che l\u1ED7i kh\u1ECFi TypeScript v\xE0 g\xE1n l\u1EA1i ki\u1EC3u th\xE0nh non-null \u2192 gi\xE1 tr\u1ECB `undefined` l\xFAc ch\u1EA1y nh\u01B0ng \u0111\u01B0\u1EE3c coi l\xE0 c\xF3 \u2192 code ph\xEDa sau tin v\xE0o ki\u1EC3u s\u1EBD crash. TypeScript kh\xF4ng b\u1EAFt (c\xFA ph\xE1p h\u1EE3p l\u1EC7). B\u1ECF `!` v\xE0 x\u1EED l\xFD null th\u1EADt (`?.`, guard, gi\xE1 tr\u1ECB m\u1EB7c \u0111\u1ECBnh): `this.user?.profile?.name`.",
28
+ severity: "warning",
29
+ category: "correctness",
30
+ languages: ["typescript"],
31
+ scope: "angular",
32
+ requires: "semantic-ts",
33
+ baseConfidence: 85,
34
+ check(ctx) {
35
+ const sf = asTsMorphSourceFile(ctx.parsed);
36
+ const findings = [];
37
+ for (const nn of sf.getDescendantsOfKind(SyntaxKind.NonNullExpression)) {
38
+ if (!assertsOptionalChain(nn)) continue;
39
+ const { line, column } = sf.getLineAndColumnAtPos(nn.getStart());
40
+ findings.push({
41
+ message: "Non-null `!` \xE1p l\xEAn k\u1EBFt qu\u1EA3 optional-chain (`?.`) \u2014 gi\xE1 tr\u1ECB c\xF3 th\u1EC3 `undefined` (chain ng\u1EAFn m\u1EA1ch); `!` ch\u1EC9 che l\u1ED7i kh\u1ECFi TypeScript ch\u1EE9 kh\xF4ng ng\u0103n `undefined` \u1EDF runtime.",
42
+ file: ctx.file.path,
43
+ range: { line, column },
44
+ suggestion: "B\u1ECF `!` v\xE0 x\u1EED l\xFD null th\u1EADt: d\xF9ng `?.`, guard, ho\u1EB7c gi\xE1 tr\u1ECB m\u1EB7c \u0111\u1ECBnh (`?? default`)."
45
+ });
46
+ }
47
+ return findings;
48
+ }
49
+ };
50
+
51
+ export { rule };
52
+ //# sourceMappingURL=no-non-null-asserted-optional-chain.rule.js.map
53
+ //# sourceMappingURL=no-non-null-asserted-optional-chain.rule.js.map
@@ -0,0 +1,112 @@
1
+ import { asTsMorphSourceFile } from '@code-sentinel/parsers';
2
+ import { SyntaxKind, Node } from 'ts-morph';
3
+
4
+ // builtin-rules/static/angular/no-uncalled-signal-in-condition.rule.ts
5
+ var SIGNAL_FACTORIES = /* @__PURE__ */ new Set([
6
+ "signal",
7
+ "computed",
8
+ "linkedSignal",
9
+ "toSignal",
10
+ "input",
11
+ // input() / input.required()
12
+ "model",
13
+ // model() / model.required()
14
+ "viewChild",
15
+ // viewChild() / viewChild.required()
16
+ "viewChildren",
17
+ "contentChild",
18
+ // contentChild() / contentChild.required()
19
+ "contentChildren"
20
+ ]);
21
+ function signalFactoryOf(init) {
22
+ if (!init || !Node.isCallExpression(init)) return void 0;
23
+ const callee = init.getExpression();
24
+ if (Node.isIdentifier(callee)) return callee.getText();
25
+ if (Node.isPropertyAccessExpression(callee)) {
26
+ const base = callee.getExpression();
27
+ if (Node.isIdentifier(base)) return base.getText();
28
+ }
29
+ return void 0;
30
+ }
31
+ function collectSignalFields(cls) {
32
+ const names = /* @__PURE__ */ new Set();
33
+ for (const prop of cls.getProperties()) {
34
+ const factory = signalFactoryOf(prop.getInitializer());
35
+ if (factory !== void 0 && SIGNAL_FACTORIES.has(factory)) names.add(prop.getName());
36
+ }
37
+ return names;
38
+ }
39
+ function thisBindsTo(read, cls) {
40
+ const boundary = read.getFirstAncestor(
41
+ (a) => Node.isClassDeclaration(a) || Node.isClassExpression(a) || Node.isFunctionDeclaration(a) || Node.isFunctionExpression(a)
42
+ );
43
+ return boundary === cls;
44
+ }
45
+ function isUncalledSignalBug(read) {
46
+ const parent = read.getParent();
47
+ if (!parent) return false;
48
+ if (Node.isCallExpression(parent) && parent.getExpression() === read) return false;
49
+ if (Node.isPropertyAccessExpression(parent) && parent.getExpression() === read) return false;
50
+ if (Node.isElementAccessExpression(parent) && parent.getExpression() === read) return false;
51
+ let cur = read;
52
+ let p = parent;
53
+ while (p && (Node.isParenthesizedExpression(p) || Node.isAsExpression(p) || Node.isNonNullExpression(p))) {
54
+ cur = p;
55
+ p = p.getParent();
56
+ }
57
+ if (!p) return false;
58
+ if (Node.isPrefixUnaryExpression(p)) {
59
+ return p.getOperatorToken() === SyntaxKind.ExclamationToken;
60
+ }
61
+ if (Node.isBinaryExpression(p)) {
62
+ const op = p.getOperatorToken().getKind();
63
+ return op === SyntaxKind.AmpersandAmpersandToken || op === SyntaxKind.BarBarToken || op === SyntaxKind.QuestionQuestionToken;
64
+ }
65
+ if (Node.isConditionalExpression(p)) return p.getCondition() === cur;
66
+ if (Node.isIfStatement(p) || Node.isWhileStatement(p) || Node.isDoStatement(p)) {
67
+ return p.getExpression() === cur;
68
+ }
69
+ if (Node.isForStatement(p)) return p.getCondition() === cur;
70
+ if (Node.isTemplateSpan(p)) return true;
71
+ return false;
72
+ }
73
+ var rule = {
74
+ id: "angular/no-uncalled-signal-in-condition",
75
+ version: 1,
76
+ kind: "static-ts",
77
+ title: "\u0110\u1ECDc signal thi\u1EBFu `()` trong \u0111i\u1EC1u ki\u1EC7n \u2014 lu\xF4n truthy",
78
+ description: "Signal (`signal`/`computed`/`input`/`toSignal`/\u2026) l\xE0 m\u1ED9t h\xE0m \u2014 ph\u1EA3i G\u1ECCI (`this.count()`) m\u1EDBi l\u1EA5y \u0111\u01B0\u1EE3c gi\xE1 tr\u1ECB. \u0110\u1ECDc thi\u1EBFu `()` trong \u0111i\u1EC1u ki\u1EC7n (`if`/`while`/ternary/`!`/`&&`/`||`/`??`) ho\u1EB7c trong interpolation (`${...}`) khi\u1EBFn ch\xEDnh h\xE0m signal b\u1ECB d\xF9ng \u2014 lu\xF4n truthy / in ra `function ...`. TypeScript kh\xF4ng b\u1EAFt l\u1ED7i n\xE0y (h\xE0m l\xE0 \u0111i\u1EC1u ki\u1EC7n h\u1EE3p l\u1EC7). Th\xEAm `()`: `this.count()`.",
79
+ severity: "warning",
80
+ category: "correctness",
81
+ languages: ["typescript"],
82
+ scope: "angular",
83
+ requires: "semantic-ts",
84
+ baseConfidence: 80,
85
+ check(ctx) {
86
+ const sf = asTsMorphSourceFile(ctx.parsed);
87
+ const findings = [];
88
+ for (const cls of sf.getClasses()) {
89
+ const signalFields = collectSignalFields(cls);
90
+ if (signalFields.size === 0) continue;
91
+ for (const pae of cls.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression)) {
92
+ if (pae.getExpression().getKind() !== SyntaxKind.ThisKeyword) continue;
93
+ if (!signalFields.has(pae.getName())) continue;
94
+ if (!thisBindsTo(pae, cls)) continue;
95
+ if (!isUncalledSignalBug(pae)) continue;
96
+ const name = pae.getName();
97
+ const { line, column } = sf.getLineAndColumnAtPos(pae.getStart());
98
+ findings.push({
99
+ message: `Signal \`this.${name}\` \u0111\u01B0\u1EE3c \u0111\u1ECDc thi\u1EBFu \`()\` trong ng\u1EEF c\u1EA3nh \u0111i\u1EC1u ki\u1EC7n/interpolation \u2014 signal l\xE0 m\u1ED9t h\xE0m n\xEAn lu\xF4n truthy (gi\xE1 tr\u1ECB th\u1EADt kh\xF4ng \u0111\u01B0\u1EE3c \u0111\xE1nh gi\xE1).`,
100
+ file: ctx.file.path,
101
+ range: { line, column },
102
+ suggestion: `G\u1ECDi signal \u0111\u1EC3 l\u1EA5y gi\xE1 tr\u1ECB: \`this.${name}()\`.`
103
+ });
104
+ }
105
+ }
106
+ return findings;
107
+ }
108
+ };
109
+
110
+ export { rule };
111
+ //# sourceMappingURL=no-uncalled-signal-in-condition.rule.js.map
112
+ //# sourceMappingURL=no-uncalled-signal-in-condition.rule.js.map
@@ -0,0 +1,85 @@
1
+ import { asTsMorphSourceFile } from '@code-sentinel/parsers';
2
+ import { Node } from 'ts-morph';
3
+
4
+ // builtin-rules/static/angular/observable-naming.rule.ts
5
+ var OBSERVABLE_TYPE_NAMES = /* @__PURE__ */ new Set([
6
+ "Observable",
7
+ "Subject",
8
+ "BehaviorSubject",
9
+ "ReplaySubject",
10
+ "AsyncSubject"
11
+ ]);
12
+ var hasOutputDecorator = (node) => node.getDecorators().some((d) => d.getName() === "Output");
13
+ var getTypeName = (typeNode) => {
14
+ if (!typeNode) return void 0;
15
+ const text = typeNode.getText();
16
+ const match = text.match(/^([A-Za-z_$][\w$]*)/);
17
+ return match?.[1];
18
+ };
19
+ var isObservableType = (typeNode) => {
20
+ const name = getTypeName(typeNode);
21
+ return name !== void 0 && OBSERVABLE_TYPE_NAMES.has(name);
22
+ };
23
+ var rule = {
24
+ id: "angular/observable-naming",
25
+ version: 2,
26
+ kind: "static-ts",
27
+ title: "Observable / Subject variables ph\u1EA3i k\u1EBFt th\xFAc b\u1EB1ng `$`",
28
+ description: "Convention Angular: identifier c\xF3 type Observable/Subject/... carry suffix `$` \u0111\u1EC3 ph\xE2n bi\u1EC7t v\u1EDBi sync value t\u1EA1i call site. `users$.subscribe(...)` r\xF5 \xFD h\u01A1n `users.subscribe(...)`.",
29
+ severity: "info",
30
+ category: "naming",
31
+ languages: ["typescript"],
32
+ scope: "angular",
33
+ requires: "semantic-ts",
34
+ baseConfidence: 80,
35
+ check(ctx) {
36
+ const sf = asTsMorphSourceFile(ctx.parsed);
37
+ const findings = [];
38
+ const report = (name, typeName, pos) => {
39
+ const { line, column } = sf.getLineAndColumnAtPos(pos);
40
+ findings.push({
41
+ message: `"${name}" c\xF3 ki\u1EC3u ${typeName} \u2014 n\xEAn \u0111\u1EB7t t\xEAn k\u1EBFt th\xFAc b\u1EB1ng \`$\`.`,
42
+ file: ctx.file.path,
43
+ range: { line, column },
44
+ suggestion: `\u0110\u1ED5i t\xEAn th\xE0nh \`${name}$\` \u0111\u1EC3 b\xE1o hi\u1EC7u \u0111\xE2y l\xE0 Observable.`
45
+ });
46
+ };
47
+ for (const cls of sf.getClasses()) {
48
+ for (const prop of cls.getProperties()) {
49
+ const tn = prop.getTypeNode();
50
+ if (!isObservableType(tn)) continue;
51
+ if (hasOutputDecorator(prop)) continue;
52
+ const name = prop.getName();
53
+ if (name.endsWith("$")) continue;
54
+ report(name, getTypeName(tn) ?? "Observable", prop.getNameNode().getStart());
55
+ }
56
+ const ctor = cls.getConstructors()[0];
57
+ if (!ctor) continue;
58
+ for (const p of ctor.getParameters()) {
59
+ if (p.getScope() === void 0) continue;
60
+ const tn = p.getTypeNode();
61
+ if (!isObservableType(tn)) continue;
62
+ if (hasOutputDecorator(p)) continue;
63
+ const name = p.getName();
64
+ if (name.endsWith("$")) continue;
65
+ report(name, getTypeName(tn) ?? "Observable", p.getNameNode().getStart());
66
+ }
67
+ }
68
+ for (const stmt of sf.getVariableStatements()) {
69
+ for (const decl of stmt.getDeclarations()) {
70
+ const tn = decl.getTypeNode();
71
+ if (!isObservableType(tn)) continue;
72
+ const name = decl.getName();
73
+ if (name.endsWith("$")) continue;
74
+ const nameNode = decl.getNameNode();
75
+ if (!Node.isIdentifier(nameNode)) continue;
76
+ report(name, getTypeName(tn) ?? "Observable", nameNode.getStart());
77
+ }
78
+ }
79
+ return findings;
80
+ }
81
+ };
82
+
83
+ export { rule };
84
+ //# sourceMappingURL=observable-naming.rule.js.map
85
+ //# sourceMappingURL=observable-naming.rule.js.map
@@ -0,0 +1,70 @@
1
+ import { asTsMorphSourceFile } from '@code-sentinel/parsers';
2
+ import { Node } from 'ts-morph';
3
+
4
+ // builtin-rules/static/angular/onpush-required.rule.ts
5
+ var getComponentDecorator = (cls) => {
6
+ for (const d of cls.getDecorators()) {
7
+ if (d.getName() === "Component") return d;
8
+ }
9
+ return void 0;
10
+ };
11
+ var getDecoratorObject = (decorator) => {
12
+ const call = decorator.getCallExpression();
13
+ if (!call) return void 0;
14
+ const [arg] = call.getArguments();
15
+ if (!arg || !Node.isObjectLiteralExpression(arg)) return void 0;
16
+ return arg;
17
+ };
18
+ var usesOnPush = (obj) => {
19
+ for (const prop of obj.getProperties()) {
20
+ if (!Node.isPropertyAssignment(prop)) continue;
21
+ if (prop.getName() !== "changeDetection") continue;
22
+ const init = prop.getInitializer();
23
+ if (!init) return false;
24
+ const text = init.getText();
25
+ return text.endsWith(".OnPush") || text === "0";
26
+ }
27
+ return false;
28
+ };
29
+ var hasSpread = (obj) => obj.getProperties().some((prop) => Node.isSpreadAssignment(prop));
30
+ var rule = {
31
+ id: "angular/onpush-required",
32
+ version: 2,
33
+ kind: "static-ts",
34
+ title: "@Component n\xEAn d\xF9ng ChangeDetectionStrategy.OnPush",
35
+ description: "OnPush gi\u1EA3m s\u1ED1 l\u1EA7n change detection ch\u1EA1y, perf-critical cho app l\u1EDBn. Khuy\u1EBFn ngh\u1ECB \xE1p d\u1EE5ng cho h\u1EA7u h\u1EBFt Component; m\u1ED9t s\u1ED1 Component v\u1EABn h\u1EE3p l\u1EC7 khi d\xF9ng Default \u2192 \u0111\xE2y l\xE0 khuy\u1EBFn ngh\u1ECB perf (warning conf 70), kh\xF4ng ph\u1EA3i bug ch\u1EAFc ch\u1EAFn. B\u1ECF qua Component c\xF3 config spread (kh\xF4ng ch\u1EE9ng minh \u0111\u01B0\u1EE3c).",
36
+ severity: "warning",
37
+ category: "performance",
38
+ languages: ["typescript"],
39
+ scope: "angular",
40
+ requires: "semantic-ts",
41
+ // A4 calibration: 85→70. Detection is deterministic, but "should be OnPush"
42
+ // is a judgment — some Default components are intentional. 70 = "worth a look,
43
+ // not certain", so `--min-confidence` can filter it out of a strict gate.
44
+ baseConfidence: 70,
45
+ check(ctx) {
46
+ const sf = asTsMorphSourceFile(ctx.parsed);
47
+ const findings = [];
48
+ for (const cls of sf.getClasses()) {
49
+ const decorator = getComponentDecorator(cls);
50
+ if (!decorator) continue;
51
+ const obj = getDecoratorObject(decorator);
52
+ if (!obj) continue;
53
+ if (usesOnPush(obj)) continue;
54
+ if (hasSpread(obj)) continue;
55
+ const className = cls.getName() ?? "<anonymous>";
56
+ const { line, column } = sf.getLineAndColumnAtPos(decorator.getStart());
57
+ findings.push({
58
+ message: `Component "${className}" ch\u01B0a khai b\xE1o \`changeDetection: ChangeDetectionStrategy.OnPush\`.`,
59
+ file: ctx.file.path,
60
+ range: { line, column },
61
+ suggestion: "Th\xEAm `changeDetection: ChangeDetectionStrategy.OnPush` v\xE0o @Component v\xE0 import t\u1EEB @angular/core."
62
+ });
63
+ }
64
+ return findings;
65
+ }
66
+ };
67
+
68
+ export { rule };
69
+ //# sourceMappingURL=onpush-required.rule.js.map
70
+ //# sourceMappingURL=onpush-required.rule.js.map
@@ -0,0 +1,65 @@
1
+ import { asTsMorphSourceFile } from '@code-sentinel/parsers';
2
+ import { Node } from 'ts-morph';
3
+
4
+ // builtin-rules/static/angular/standalone-component-required.rule.ts
5
+ var getComponentDecorator = (cls) => {
6
+ for (const d of cls.getDecorators()) {
7
+ if (d.getName() === "Component") return d;
8
+ }
9
+ return void 0;
10
+ };
11
+ var getDecoratorObject = (decorator) => {
12
+ const call = decorator.getCallExpression();
13
+ if (!call) return void 0;
14
+ const [arg] = call.getArguments();
15
+ if (!arg || !Node.isObjectLiteralExpression(arg)) return void 0;
16
+ return arg;
17
+ };
18
+ var declaresStandalone = (obj) => {
19
+ for (const prop of obj.getProperties()) {
20
+ if (!Node.isPropertyAssignment(prop)) continue;
21
+ if (prop.getName() === "standalone") return true;
22
+ }
23
+ return false;
24
+ };
25
+ var rule = {
26
+ id: "angular/standalone-component-required",
27
+ // v2 (Quality Roadmap A2): `standalone: false` is an intentional opt-out
28
+ // (explicit status; the migration nudge is `angular/no-ngmodule`'s job), so
29
+ // it is no longer flagged — the rule fires only when the `standalone` key is
30
+ // ABSENT. Fixes the FP where an explicit `standalone: false` was reported.
31
+ version: 2,
32
+ kind: "static-ts",
33
+ title: "@Component n\xEAn khai b\xE1o `standalone: true` r\xF5 r\xE0ng",
34
+ description: "Angular 19 default standalone=true, nh\u01B0ng vi\u1EC7c khai b\xE1o r\xF5 gi\xFAp code self-document v\xE0 an to\xE0n khi target Angular version c\u0169 h\u01A1n. Ph\u1ED1i h\u1EE3p v\u1EDBi `angular/no-ngmodule` \u0111\u1EC3 push toward standalone pattern.",
35
+ severity: "info",
36
+ category: "angular-best-practice",
37
+ languages: ["typescript"],
38
+ scope: "angular",
39
+ requires: "semantic-ts",
40
+ baseConfidence: 100,
41
+ check(ctx) {
42
+ const sf = asTsMorphSourceFile(ctx.parsed);
43
+ const findings = [];
44
+ for (const cls of sf.getClasses()) {
45
+ const decorator = getComponentDecorator(cls);
46
+ if (!decorator) continue;
47
+ const obj = getDecoratorObject(decorator);
48
+ if (!obj) continue;
49
+ if (declaresStandalone(obj)) continue;
50
+ const className = cls.getName() ?? "<anonymous>";
51
+ const { line, column } = sf.getLineAndColumnAtPos(decorator.getStart());
52
+ findings.push({
53
+ message: `Component "${className}" thi\u1EBFu \`standalone: true\` trong @Component.`,
54
+ file: ctx.file.path,
55
+ range: { line, column },
56
+ suggestion: "Th\xEAm `standalone: true` v\xE0o @Component object \u2014 make standalone status explicit."
57
+ });
58
+ }
59
+ return findings;
60
+ }
61
+ };
62
+
63
+ export { rule };
64
+ //# sourceMappingURL=standalone-component-required.rule.js.map
65
+ //# sourceMappingURL=standalone-component-required.rule.js.map