-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { NgParselOutput } from '../shared/model/types.model.js'; | ||
import { NgParselMethod } from '../shared/model/method.model.js'; | ||
import { NgParselField } from '../shared/model/field.model.js'; | ||
|
||
export interface NgParselService extends NgParselOutput { | ||
fieldsPublicExplicit: NgParselField[]; | ||
methodsPublicExplicit: NgParselMethod[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface NgParselField { | ||
name: string; | ||
type: string | 'inferred'; | ||
value?: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { parseExplicitPublicFields } from './field.parser.js'; | ||
import { tsquery } from '@phenomnomnominal/tsquery'; | ||
|
||
describe('Field parser', () => { | ||
it('should extract a public Signal field', () => { | ||
const ast = tsquery.ast(` | ||
export class MyTestClass { | ||
public visible = signal('0'); | ||
private notVisibleToYou = 'foo'; | ||
} | ||
`); | ||
|
||
const expectedFields = [ | ||
{ | ||
name: 'visible', | ||
type: 'inferred', | ||
value: `signal('0')`, | ||
}, | ||
]; | ||
|
||
const fieldsPublicExplicit = parseExplicitPublicFields(ast); | ||
expect(fieldsPublicExplicit).toEqual(expectedFields); | ||
}); | ||
|
||
it('should extract a public property field', () => { | ||
const ast = tsquery.ast(` | ||
export class MyTestClass { | ||
public visible = 'some value'; | ||
private notVisibleToYou = 'foo'; | ||
} | ||
`); | ||
|
||
const expectedFields = [ | ||
{ | ||
name: 'visible', | ||
type: 'inferred', | ||
value: `'some value'`, | ||
}, | ||
]; | ||
|
||
const fieldsPublicExplicit = parseExplicitPublicFields(ast); | ||
expect(fieldsPublicExplicit).toEqual(expectedFields); | ||
}); | ||
|
||
it('should extract a public Subject', () => { | ||
const ast = tsquery.ast(` | ||
export class MyTestClass { | ||
public visible = new Subject('some value'); | ||
private notVisibleToYou = 'foo'; | ||
} | ||
`); | ||
|
||
const expectedFields = [ | ||
{ | ||
name: 'visible', | ||
type: 'inferred', | ||
value: `Subject('some value')`, | ||
}, | ||
]; | ||
|
||
const fieldsPublicExplicit = parseExplicitPublicFields(ast); | ||
expect(fieldsPublicExplicit).toEqual(expectedFields); | ||
}); | ||
|
||
it('should extract a public Subject with type void', () => { | ||
const ast = tsquery.ast(` | ||
export class MyTestClass { | ||
public visible = new Subject<void>(); | ||
private notVisibleToYou = 'foo'; | ||
} | ||
`); | ||
|
||
const expectedFields = [ | ||
{ | ||
name: 'visible', | ||
type: 'Subject<void>', | ||
value: undefined, | ||
}, | ||
]; | ||
|
||
const fieldsPublicExplicit = parseExplicitPublicFields(ast); | ||
expect(fieldsPublicExplicit).toEqual(expectedFields); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as ts from 'typescript'; | ||
import { tsquery } from '@phenomnomnominal/tsquery'; | ||
|
||
import { NgParselField } from '../model/field.model.js'; | ||
|
||
export function parseExplicitPublicFields(ast: ts.SourceFile): NgParselField[] { | ||
const fieldsExplicitPublic: NgParselField[] = []; | ||
|
||
const publicFields = tsquery(ast, 'PropertyDeclaration:has(PublicKeyword)'); | ||
|
||
publicFields.forEach((field) => { | ||
const nameNode = tsquery(field.getText(), 'Identifier')[0]; | ||
let typeNode = null; | ||
const valueNode = | ||
tsquery(field.getText(), 'CallExpression')[0] || | ||
tsquery( | ||
field.getText(), | ||
'NewExpression:has(NullKeyword, ObjectLiteralExpression, ArrayLiteralExpression, TrueKeyword, FalseKeyword, StringLiteral, Identifier[name=undefined], NumericLiteral, TemplateExpression, NoSubstitutionTemplateLiteral)' | ||
)[0] || | ||
tsquery( | ||
field.getText(), | ||
'NullKeyword, ObjectLiteralExpression, ArrayLiteralExpression, TrueKeyword, FalseKeyword, StringLiteral, Identifier[name=undefined], NumericLiteral, TemplateExpression, NoSubstitutionTemplateLiteral' | ||
)[0]; | ||
|
||
if (!valueNode) { | ||
typeNode = | ||
tsquery(field.getText(), 'TypeReference')[0] || | ||
tsquery(field.getText(), 'NewExpression')[0] || | ||
tsquery(field.getText(), 'CallExpression')[0]; | ||
} | ||
|
||
fieldsExplicitPublic.push({ | ||
name: nameNode!.getText(), | ||
type: typeNode?.getText().replace('new ', '').replace('()', '') || 'inferred', | ||
value: valueNode?.getText().replace('new ', '').replace('()', ''), | ||
}); | ||
}); | ||
|
||
return fieldsExplicitPublic; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable, signal } from '@angular/core'; | ||
import { BehaviorSubject, Subject } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CounterService { | ||
public counter = signal(0); | ||
public counter$ = new BehaviorSubject(0); | ||
public increment = new Subject<number>(); | ||
|
||
public foo = true; | ||
|
||
private notVisible = 'blub'; | ||
} |