{"id":13631173,"url":"https://github.com/cst/cst","last_synced_at":"2025-04-17T18:32:13.679Z","repository":{"id":33624095,"uuid":"37276358","full_name":"cst/cst","owner":"cst","description":":herb: JavaScript Concrete Syntax Tree ","archived":false,"fork":false,"pushed_at":"2022-12-06T19:46:59.000Z","size":1082,"stargazers_count":458,"open_issues_count":39,"forks_count":21,"subscribers_count":25,"default_branch":"master","last_synced_at":"2024-08-01T22:47:58.654Z","etag":null,"topics":["ast","babylon","cst"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-11T17:44:47.000Z","updated_at":"2024-07-28T19:03:14.000Z","dependencies_parsed_at":"2023-01-15T01:45:49.600Z","dependency_job_id":null,"html_url":"https://github.com/cst/cst","commit_stats":null,"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cst%2Fcst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cst%2Fcst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cst%2Fcst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cst%2Fcst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cst","download_url":"https://codeload.github.com/cst/cst/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223762878,"owners_count":17198384,"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","babylon","cst"],"created_at":"2024-08-01T22:02:13.752Z","updated_at":"2024-11-08T22:31:11.883Z","avatar_url":"https://github.com/cst.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/cst/cst.svg?branch=master)](https://travis-ci.org/cst/cst)\n\n# JavaScript CST implementation\n\n## CST\n\nCheck out code [samples](https://github.com/cst/cst/wiki/How-to-add-a-property-to-an-object) and rest of the wiki for more.\n\n`CST` means Concrete Syntax Tree. Unlike an `AST` (Abstract Syntax Tree), a `CST` contains all the information\nfrom the JavaScript source file: whitespace, punctuators, comments. This information is extremely useful for\ncode style checkers and other code linters. `CST` is also useful for cases when you need to apply modifications\nto existing JavaScript files while preserving the initial file formatting.\n\nThis `CST` implementation is designed to be `100%` compatible with JS `AST` (https://github.com/estree/estree).\n\nMain principles:\n\n* CST contains all the information from a parsed file (including whitespace and comments).\n* Compatible with AST (https://github.com/estree/estree).\n* Requires tokens to modify CST structure.\n* The tree is always valid (it protects itself against breaking changes).\n* CST can be rendered to valid JS at any time.\n\nLet's see an example:\n\n```js\nx = 0;\nif (x) x++;\n```\n\nThe CST for this example:\n\n![](https://raw.githubusercontent.com/cst/cst/master/docs/cst-example.png)\n\n* Blue text — CST Tokens.\n* White text in blue blocks — CST Nodes (their structure is equal to an AST).\n* Blue lines — CST Structure.\n* Red lined — AST Links.\n\n## Classes\n\n### Element\n\n`Element` is the base class for `Node` and `Token`.\n\n```js\ndeclare class Element {\n\n  // traversal for children\n  childElements: Array\u003cElement\u003e;\n  firstChild: ?Element;\n  lastChild: ?Element;\n\n  // traversal for parent\n  parentElement: ?Element;\n\n  // traversing between siblings\n  nextSibling: ?Element;\n  previousSibling: ?Element;\n\n  // traversing to first/last tokens (not only direct tokens)\n  getFirstToken(): ?Token;\n  getLastToken(): ?Token;\n\n  // traversing to next/previous tokens (not only siblings)\n  getNextToken(): ?Token;\n  getPreviousToken(): ?Token;\n\n  // Code properties\n  type: string;\n  isToken: boolean;\n  isNode: boolean;\n  isExpression: boolean;\n  isStatement: boolean;\n  isWhitespace: boolean;\n  isFragment: boolean;\n  isModuleDeclaration: boolean;\n  isModuleSpecifier: boolean;\n\n  // Code methods\n  getSourceCode(): string;\n  getSourceCodeLength(): number;\n\n  // Mutation methods\n\n  // appends child to the end of the `Element`\n  appendChild(newElement: Element): void;\n  // prepends child to the end of the `Element`\n  prependChild(newElement: Element): void;\n  // inserts child before `referenceChild`\n  insertChildBefore(newElement: Element, referenceChild: Element): void;\n  // replaces specified child interval (from `firstChildRef` to lastChildRef`) with specified child.\n  replaceChildren(newElement: Element, firstRefChild: Element, lastRefChild: Element): void;\n\n  // Location methods\n  getRange(): Range;\n  getLoc(): Location;\n}\n\ndeclare class Token extends Element {\n  // token value\n  value: string;\n}\n\ntype Range = [\n    start: number;\n    end: number;\n];\n\ntype Position = {\n  line: number,\n  column: number\n};\n\ntype Location = {\n  start: Position,\n  end: Position\n};\n```\n\n### Node\n\n`Node` extends `Element`. The Nodes are the \"AST part of a CST\". If you drop everything but Nodes from a `CST`, you will\nget a pure `AST` from the Node structure. So it is fair to say that Nodes provide the `AST` logic for a `CST`. Currently\nonly Nodes can contain children.\n\nThe Node property `isNode` always returns `true`.\n\n### Token\n\n`Token` extends `Element`. The purpose of a `CST` is to have tokens in the tree. By only manipulating tokens,\nwe can change code formatting without any effect on the behaviour.\n\nThe Token property `isToken` always returns `true`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcst%2Fcst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcst%2Fcst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcst%2Fcst/lists"}