Skip to content
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

How can I remove a node and all leading comments? #1529

Open
POFerro opened this issue Apr 19, 2024 · 0 comments
Open

How can I remove a node and all leading comments? #1529

POFerro opened this issue Apr 19, 2024 · 0 comments

Comments

@POFerro
Copy link

POFerro commented Apr 19, 2024

Hi :),

I'm trying to remove all nodes from a source file except a function and it's ascendants.
The original sourceFile looks like this:
`
export class PDVModalitiesCardsSearchModalLookupFieldComponent {

protected get ngModelValue() {
return this.keys.codigoModalidadeCard;
}
protected set ngModelValue(newValue: any) {
this.keys.codigoModalidadeCard = newValue;
}

// #region Fields from xx

/**
* Número de Meses
*/
public get numeroMeses() { return this.result.numeroMeses; }
public set numeroMeses(value: number) {
if (value !== this.result.numeroMeses) {
this.result.numeroMeses = value;
this.numeroMesesChange.emit(this.result.numeroMeses);
}
}
public numeroMesesChange = new EventEmitter();

/**
* Taxa (%)
*/
public get taxa() { return this.result.taxa; }
public set taxa(value: number) {
if (value !== this.result.taxa) {
this.result.taxa = value;
this.taxaChange.emit(this.result.taxa);
}
}
public taxaChange = new EventEmitter();

// #endregion Fields from xx

constructor(
announcer: LiveAnnouncer
, private dialog: MatDialog
, private http: HttpClient
, @self() @optional() autocomplete?: MatAutoCompleteTriggerLcfExtension<models.PDVModalitiesCardsSearchResultVM>
, @optional() @self() ngModel?: NgModel
) {
super(announcer, (searchString) => this.loadAutoComplete(searchString), autocomplete, ngModel);
}

private loadAutoComplete(searchString: string) {
return this.http.get<models.PDVModalitiesCardsSearchResultVM[]>(
'api/PDV/PDVModalitiesCardsSearchModal/AutoCompleteSearch'
, { params: { searchString: searchString } }
)
;
}

protected performLoad() {
console.log("[PDVModalitiesCardsSearchModalLookupFieldComponent][load] codigoModalidadeCard: ", this.ngModelValue);

return this.http.get<models.PDVModalitiesCardsSearchResultVM>(
  'api/PDV/PDVModalitiesCardsSearchModal/GetCurrent'
  , {
    params: this.keys as any
  }
);

}
}
`

I'm trying to leave the class only with loadAutoComplete method so the code I'm using is:
`
function composeDeltaTree(relevantNode: Node<ts.Node>) {
const parents = relevantNode.getAncestors();

const sourceFile = relevantNode.getSourceFile();
sourceFile.forEachDescendant((node, traversal) => {
if (node === relevantNode) {
console.debug("node: ", node.getKindName(), (node.compilerNode as ts.NamedDeclaration).name?.getText(), " is our relevantNode, returning it");
traversal.skip();
}
else if (!parents.includes(node) && (node as any).remove) {
console.debug("node: ", node.getKindName(), (node.compilerNode as ts.NamedDeclaration).name?.getText(), " is not in parents and is removable, cleaning it up. leading trivia", node.getLeadingCommentRanges().map(c => c.getKind() + "->" + c.getText()));
(node as any).remove();
traversal.skip();
}
else {
console.debug("node: ", node.getKindName(), (node.compilerNode as ts.NamedDeclaration).name?.getText(), " is in parents or is not removable, returning it");
}
});

return sourceFile;
}
`

Unfortunatelly the output is:
`
export class PDVModalitiesCardsSearchModalLookupFieldComponent {
// #region Fields from PDVModalitiesCardsSearchResultVM
// #endregion Fields from PDVModalitiesCardsSearchResultVM

private loadAutoComplete(searchString: string) {
return this.http.get<models.PDVModalitiesCardsSearchResultVM[]>(
'api/PDV/PDVModalitiesCardsSearchModal/AutoCompleteSearch'
, { params: { searchString: searchString } }
)
;
}
}
`

the call to remove nodes is leaving the // #region trivia, any idea why? Am I missing something? Can you help me please?
Thanks and best regards
POFerro

PS: Thanks and congrats for the lib it's amazing at making ast manipulation easy :)
PS2: Sorry for the bad formating of source code, I can't seem to get it right :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant