-
Notifications
You must be signed in to change notification settings - Fork 5
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 do I get all the tokens for a statement? #4
Comments
I've not used the To actually investigate that sort of piece of code, I'd look at using the walk or visitor APIs to pick the type of element you're looking for first - e.g.
This snippet still doesn't handle the full complexity of python imports, so e.g. renamed imports would need something different, but this might set you on the right path, depending on what you're trying to do. |
That snippet definitely helps. Thanks for posting it. Do you by any chance have any suggestions on how to go about extracting a renamed import from This is what I tried. import { createVisitor, parse, walk } from 'python-ast';
interface Import {
items: string[];
source: string;
}
const analyze = (code: string) => {
const ast = parse(code);
const visitor = createVisitor({
visitImport_name(importNode) {
const importSourceNode = importNode.dotted_as_names();
console.log('visitImport_name', importSourceNode?.text);
},
visitImport_stmt(importNode) {
const importSourceNode = importNode.import_name();
importSourceNode
?.dotted_as_names()
.children?.forEach((child) => console.log('child', child.text));
importSourceNode
?.dotted_as_names()
.children?.forEach((child) => console.log('child', child.text));
console.log('visitImport_stmt', importSourceNode?.text);
console.log(
'visitImport_stmt',
importSourceNode
?.dotted_as_names()
.dotted_as_name()
.map((i) => i.text)
);
}
});
visitor.visit(ast);
const imports: Import[] = [];
walk(
{
enterImport_from(importNode) {
const importSourceNode = importNode.dotted_name();
const importedItems = importNode.import_as_names()?.import_as_name();
imports.push({
source: importSourceNode?.text || '',
items: importedItems?.map((item) => item.text) || []
});
}
},
ast
);
return imports;
};
analyze('import datetime as dt\n'); This was the output.
|
I can't do
node.text.split(' ')
. "Since tokens on hidden channels (e.g. whitespace or comments) are not added to the parse trees, they will not appear in the output of this method." So, I getfromdatetimeimportdatetime,date
when traversingfrom datetime import datetime, date
, for example.I'd want to do something like
node.getTokens()
, butgetTokens(x)
takes attype
as a parameter that I haven't seen documentation for.I tried just plugging in incremental integers for
ttype
, but that didn't give me any tokens.The text was updated successfully, but these errors were encountered: