{"id":15413070,"url":"https://github.com/daybrush/ast-parser","last_synced_at":"2025-04-19T09:43:56.109Z","repository":{"id":40643799,"uuid":"168687768","full_name":"daybrush/ast-parser","owner":"daybrush","description":"ast parser","archived":false,"fork":false,"pushed_at":"2022-04-14T05:57:21.000Z","size":132,"stargazers_count":8,"open_issues_count":3,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-30T08:53:07.590Z","etag":null,"topics":["ast","babel","javascript","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daybrush.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-01T11:27:51.000Z","updated_at":"2024-09-12T12:19:01.000Z","dependencies_parsed_at":"2022-09-18T15:13:11.667Z","dependency_job_id":null,"html_url":"https://github.com/daybrush/ast-parser","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daybrush%2Fast-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daybrush%2Fast-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daybrush%2Fast-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daybrush%2Fast-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daybrush","download_url":"https://codeload.github.com/daybrush/ast-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249666758,"owners_count":21308154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","babel","javascript","typescript"],"created_at":"2024-10-01T16:55:21.084Z","updated_at":"2025-04-19T09:43:56.081Z","avatar_url":"https://github.com/daybrush.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ast-parser\n\n```sh\n$ npm i ast-parser -D\n```\n\n```js\nconst {parse: babelParse} = require(\"@babel/parser\");\nconst {parse: astParse, find, findInfo} = require(\"ast-parser\");\n\nfunction getNode(code) {\n  return babelParse(code, {\n    sourceType: \"module\",\n    plugins: [\"classProperties\", \"typescript\"],\n  });\n}\n\nexpect(astParse(getNode(code)).nodeType).toBe(\"File\");\nexpect(astParse(getNode(code).program).nodeType).toBe(\"Program\");\n\nconst info = astParse(getNode(`\n  const a = {a: 1, b: 2};\n`)).program.body[0];\nconst info2 = info.declarations[0];\n\nexpect(info.nodeType).toBe(\"VariableDeclaration\");\nexpect(info.kind).toBe(\"const\");\nexpect(info.declarations.length).toBe(1);\n\nexpect(info2.id.nodeType).toBe(\"Identifier\");\nexpect(info2.id.name).toBe(\"a\");\nexpect(info2.id.string).toBe(\"a\");\nexpect(info2.init.nodeType).toBe(\"ObjectExpression\");\nexpect(info2.init.string).toBe(\"{a: 1, b: 2}\");\nexpect(info.string).toBe(\"const a = {a: 1, b: 2}\");\n\nexpect(\n  astParse(getNode(`\n    const a = {a: 1, b: 2};\n  `)).program.body[0]\n).).toBe(\"VariableDeclaration\");\n```\n\n## find ObjectExpression, ObjectProperty\n```js\nconst info = find(\"ObjectExpression\", getNode(`\n  const a = {a: 1, b: 2};\n`));\nconst info2 = findInfo(\"ObjectProperty\", info);\n\nexpect(info.string).toBe(\"{a: 1, b: 2}\");\nexpect(info2.length).toBe(2);\nexpect(info2[0].string).toBe(\"a: 1\");\nexpect(info2[0].key.string).toBe(\"a\");\nexpect(info2[0].value.string).toBe(\"1\");\nexpect(info2[1].string).toBe(\"b: 2\");\nexpect(info2[1].key.string).toBe(\"b\");\nexpect(info2[1].value.string).toBe(\"2\");\n```\n\n## find ClassProperty\n```js\nconst info = find(\"ClassDeclaration\", `\n  class A extends B implements C {\n    a = \"0\";\n    b: number = \"1\";\n    private c: string = \"2\";\n    public static d: () =\u003e void = () =\u003e ({});\n    public static abstract e: () =\u003e void;\n    constructor(a: string, private b?: number) {\n\n    }\n    public method1(): number {\n\n    }\n  }\n`);\nconst info2 = findInfo(\"ClassProperty\", info);\nconst info3 = findInfo(\"ClassMethod\", info);\n\nexpect(info.string).toBe(\"class A extends B implements C\");\nexpect(info2.length).toBe(5);\nexpect(info3.length).toBe(2);\nexpect(info2[0].string).toBe(\"a\");\nexpect(info2[1].string).toBe(\"b: number\");\nexpect(info2[2].string).toBe(\"private c: string\");\nexpect(info2[3].string).toBe(\"public static d: () =\u003e void\");\nexpect(info2[4].string).toBe(\"public static abstract e: () =\u003e void\");\nexpect(info3[0].string).toBe(\"constructor(a: string, private b?: number)\");\nexpect(info3[1].string).toBe(\"public method1(): number\");\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaybrush%2Fast-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaybrush%2Fast-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaybrush%2Fast-parser/lists"}