-
Notifications
You must be signed in to change notification settings - Fork 4
feat/Change-Detection - add CD strategy tag in the components tree #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { mkdirSync, writeFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { fixtureDir } from './fixture-dir.ts'; | ||
| import { angularMajor } from '../angular-version.ts'; | ||
|
|
||
| describe('angularMajor', () => { | ||
| it('reads the major from the installed package', () => { | ||
| const dir = fixtureDir('ng-devtools-version-'); | ||
| const coreDir = join(dir, 'node_modules', '@angular', 'core'); | ||
| mkdirSync(coreDir, { recursive: true }); | ||
| writeFileSync(join(coreDir, 'package.json'), JSON.stringify({ version: '22.1.7' })); | ||
| expect(angularMajor(dir)).toBe(22); | ||
| }); | ||
|
|
||
| it('prefers the installed version over the declared range', () => { | ||
| const dir = fixtureDir('ng-devtools-version-'); | ||
| const coreDir = join(dir, 'node_modules', '@angular', 'core'); | ||
| mkdirSync(coreDir, { recursive: true }); | ||
| writeFileSync(join(coreDir, 'package.json'), JSON.stringify({ version: '22.1.7' })); | ||
| writeFileSync( | ||
| join(dir, 'package.json'), | ||
| JSON.stringify({ dependencies: { '@angular/core': '^20.0.0' } }), | ||
| ); | ||
| expect(angularMajor(dir)).toBe(22); | ||
| }); | ||
|
|
||
| it('falls back to a pinned or ^/~ range in package.json', () => { | ||
| const dir = fixtureDir('ng-devtools-version-'); | ||
| writeFileSync( | ||
| join(dir, 'package.json'), | ||
| JSON.stringify({ dependencies: { '@angular/core': '^21.0.0' } }), | ||
| ); | ||
| expect(angularMajor(dir)).toBe(21); | ||
|
|
||
| const dir2 = fixtureDir('ng-devtools-version-'); | ||
| writeFileSync( | ||
| join(dir2, 'package.json'), | ||
| JSON.stringify({ devDependencies: { '@angular/core': '~21.1.0' } }), | ||
| ); | ||
| expect(angularMajor(dir2)).toBe(21); | ||
| }); | ||
|
|
||
| it('gives up on a range that names no single version', () => { | ||
| const dir = fixtureDir('ng-devtools-version-'); | ||
| writeFileSync( | ||
| join(dir, 'package.json'), | ||
| JSON.stringify({ dependencies: { '@angular/core': '>=20.0.0' } }), | ||
| ); | ||
| expect(angularMajor(dir)).toBeUndefined(); | ||
|
|
||
| const dir2 = fixtureDir('ng-devtools-version-'); | ||
| writeFileSync( | ||
| join(dir2, 'package.json'), | ||
| JSON.stringify({ dependencies: { '@angular/core': 'latest' } }), | ||
| ); | ||
| expect(angularMajor(dir2)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns undefined when nothing names a version at all', () => { | ||
| const dir = fixtureDir('ng-devtools-version-'); | ||
| expect(angularMajor(dir)).toBeUndefined(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||||||||||||||||||||||||||||||||||
| import { existsSync, readFileSync } from 'node:fs'; | ||||||||||||||||||||||||||||||||||||||||
| import { join } from 'node:path'; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * The major version of `@angular/core` the project builds against, or | ||||||||||||||||||||||||||||||||||||||||
| * `undefined` when it cannot be told apart from the workspace root. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * Angular 22 made `OnPush` the implicit change detection strategy, so a | ||||||||||||||||||||||||||||||||||||||||
| * component without a `changeDetection` key means something different | ||||||||||||||||||||||||||||||||||||||||
| * depending on which major the project is on; this resolves that once per | ||||||||||||||||||||||||||||||||||||||||
| * scan rather than per file. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * Only one version is resolved for the whole workspace root, so a monorepo | ||||||||||||||||||||||||||||||||||||||||
| * that mixes Angular majors across packages is not distinguished per package. | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| export function angularMajor(cwd: string): number | undefined { | ||||||||||||||||||||||||||||||||||||||||
| const installed = readJson(join(cwd, 'node_modules', '@angular', 'core', 'package.json'))[ | ||||||||||||||||||||||||||||||||||||||||
| 'version' | ||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||
| if (typeof installed === 'string') { | ||||||||||||||||||||||||||||||||||||||||
| const major = majorOf(installed); | ||||||||||||||||||||||||||||||||||||||||
| if (major !== undefined) return major; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const pkg = readJson(join(cwd, 'package.json')); | ||||||||||||||||||||||||||||||||||||||||
| const deps = { ...pkg['dependencies'], ...pkg['devDependencies'] }; | ||||||||||||||||||||||||||||||||||||||||
| const range = deps['@angular/core']; | ||||||||||||||||||||||||||||||||||||||||
| // Only a pinned or `^`/`~` range names one version; `>=20`, `latest`, | ||||||||||||||||||||||||||||||||||||||||
| // `workspace:*` and the like could resolve to anything and are left alone | ||||||||||||||||||||||||||||||||||||||||
| // rather than guessed at. | ||||||||||||||||||||||||||||||||||||||||
| if (typeof range === 'string') return majorOf(range.replace(/^[\^~]/, '')); | ||||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| function majorOf(version: string): number | undefined { | ||||||||||||||||||||||||||||||||||||||||
| const match = /^(\d+)\./.exec(version); | ||||||||||||||||||||||||||||||||||||||||
| return match ? Number(match[1]) : undefined; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| function readJson(path: string): Record<string, any> { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| if (!existsSync(path)) return {}; | ||||||||||||||||||||||||||||||||||||||||
| return JSON.parse(readFileSync(path, 'utf-8')); | ||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '1,115p' packages/ng-devtools/src/rpc/angular-version.ts
sed -n '35,115p' packages/ng-devtools/src/rpc/get-components.ts
rg -n 'Avoid the `any`|use `unknown`|angular-version.ts' AGENTS.md packages/AGENTS.md packages/ng-devtools/AGENTS.md .github 2>/dev/nullRepository: santoshyadavdev/angular-devtools Length of output: 4629 🏁 Script executed: cat -n AGENTS.md | sed -n '1,30p'
printf '%s\n' '--- get-components imports and handler ---'
cat -n packages/ng-devtools/src/rpc/get-components.ts | sed -n '1,75p'
printf '%s\n' '--- angular-version callers and tests ---'
rg -n -C 3 'angularMajor|readJson|get-components|scanComponents' packages/ng-devtools/src packages/ng-devtools/test packages/ng-devtools 2>/dev/null | head -240Repository: santoshyadavdev/angular-devtools Length of output: 25085 Validate parsed JSON before property access.
The repository guidance also requires Suggested fix-function readJson(path: string): Record<string, any> {
+function readJson(path: string): Record<string, unknown> {
try {
if (!existsSync(path)) return {};
- return JSON.parse(readFileSync(path, 'utf-8'));
+ const parsed: unknown = JSON.parse(readFileSync(path, 'utf-8'));
+ return parsed && typeof parsed === 'object'
+ ? (parsed as Record<string, unknown>)
+ : {};
} catch {
return {};
}
}Also accept only object-valued 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
The note for an unresolved expression never appears.
Line 155 of
packages/ng-devtools/src/rpc/get-components.tshandles a value it cannot resolve. It returns only{ changeDetection: 'unknown' }and leaves outchangeDetectionDeclared. The test atget-components.test.tslines 171–178 asserts this behavior. Socomp.changeDetectionDeclaredis always undefined when the mode isunknown.As a result, a component with
changeDetection: someHelper()shows "Angular version not detected". The Angular version was detected, so this note is wrong.To fix this, the RPC must report the unresolved case in its own field. One option is to add
changeDetectionUnresolved: true, or a'unresolved'declared value, to the RPC schema. The UI should then branch on that field.Proposed fix (UI side)
Also:
changeDetectionUnresolved?: booleanto bothComponentInfointerfaces and toComponentSchema.get-components.ts.📝 Committable suggestion
🤖 Prompt for AI Agents