{"id":15011359,"url":"https://github.com/shawnbot/sast","last_synced_at":"2025-04-09T19:08:40.096Z","repository":{"id":45329846,"uuid":"107446098","full_name":"shawnbot/sast","owner":"shawnbot","description":"Parse CSS, Sass, SCSS, and Less into a unist syntax tree","archived":false,"fork":false,"pushed_at":"2023-01-03T21:47:47.000Z","size":585,"stargazers_count":50,"open_issues_count":14,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T19:08:34.365Z","etag":null,"topics":["ast","css","lesscss","parser","sass","scss","syntax-tree","unist"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shawnbot.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":"2017-10-18T18:16:57.000Z","updated_at":"2024-10-10T20:39:07.000Z","dependencies_parsed_at":"2023-02-01T10:46:22.314Z","dependency_job_id":null,"html_url":"https://github.com/shawnbot/sast","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawnbot%2Fsast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawnbot%2Fsast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawnbot%2Fsast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawnbot%2Fsast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shawnbot","download_url":"https://codeload.github.com/shawnbot/sast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094993,"owners_count":21046770,"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","css","lesscss","parser","sass","scss","syntax-tree","unist"],"created_at":"2024-09-24T19:40:48.945Z","updated_at":"2025-04-09T19:08:40.050Z","avatar_url":"https://github.com/shawnbot.png","language":"JavaScript","readme":"# sast\nThis is a thing that parses CSS, Sass, and SCSS into a [unist]-compatible\nabstract syntax tree (AST), which makes it possible to then search and\nmanipulate with all of the wonderful [unist utility modules][utilities]. Most\nof the heavy lifting is done by [gonzales].\n\n## Installation\nInstall it with npm:\n\n```\nnpm install --save sast\n```\n\n## Usage\nYou can `import` or `require()` the module and access [the API](#api) as its\nmethods, like this:\n\n```js\n// CommonJS, older versions of Node\nconst sast = require('sast')\n\n// ES6/ES2016/Babel/etc.\nimport sast from 'sast'\n\nconst tree = sast.parse('a { color: $red; }', {syntax: 'scss'})\nconsole.dir(tree, {depth: null})\n```\n\nor you can import just the API methods you need, like so:\n\n```js\n// CommonJS\nconst {parse} = require('sast')\n// ES6\nimport {parse} from 'sast'\n\nconst tree = parse('a { color: $red; }', {syntax: 'scss'})\n```\n\n## API\n\n### `sast.parse(source [, options])` \u003ca name=\"parse\"\u003e\u003c/a\u003e\nSynchronously parse the CSS, Sass, or SCSS source text (a string) into an\nabstract source tree (AST). The default syntax is CSS (`{syntax: 'css'}`);\nother acceptable values are `sass`, `scss`, and `less`. See the [gonzales\ndocs](https://github.com/tonyganch/gonzales-pe#parameters-1) for more info. To\nparse files by path, use [`parseFile()`](#parse-file).\n\n### `sast.stringify(node)` \u003ca name=\"stringify\"\u003e\u003c/a\u003e\nFormat the resulting AST back into a string, presumably after manipulating it.\n\n### `sast.jsonify(node)` \u003ca name=\"jsonify\"\u003e\u003c/a\u003e\nCoerce the given AST node into JSON data, according to the following rules:\n\n1. Numbers are numbers: `1` -\u003e `1`, not `\"1\"`.\n1. Lists become arrays: `(a, 1)` -\u003e `[\"a\", 1]`\n1. [Maps][Sass maps] become objects: `(x: 1)` -\u003e `{x: 1}`\n1. Lists and maps can be nested!\n1. Everything else is [stringified](#stringify), and _should_ be preserved in\n   its original form:\n   * Sass/SCSS variables should preserve their leading `$`.\n   * Hex colors should preserve their leading `#`.\n   * `rgb()`, `rgba()`, `hsl()`, `hsla()`, and any other functions should\n     preserve their parentheses.\n   * Parentheses that are not parsed as lists or maps should be preserved.\n\n### `sast.parseFile(filename [, parseOptions={} [, readOptions='utf8'])` \u003ca name=\"parse-file\"\u003e\u003c/a\u003e\nRead a file and parse its contents, returning a Promise. If no\n`parseOptions.syntax` is provided, or its value is `auto`, the filename's\nextension will be used as the `syntax` option passed to [`parse()`](#parse).\n\n```js\nconst {parseFile} = require('sast')\nparseFile('path/to/some.scss')\n  .then(tree =\u003e console.dir(tree, {depth: null}))\n  .catch(error =\u003e console.error('Parse error:', error))\n```\n\n## CLI\nThe `sast` [npm package] comes with two command line utilities:\n\n### `sast-parse`\nParses a file or stdin as a given syntax, applies one or more simplifying\ntransformations, then outputs the resulting syntax tree in a variety of\nformats:\n\n* JSON: the raw syntax tree in object form, which can be passed to other CLIs.\n* [YAML]: an easier-to-read alternative to JSON, also suitable for piping to\n  other CLIs.\n* Tree: a text representation of the syntax tree provided by\n  [unist-util-inspect](https://github.com/syntax-tree/unist-util-inspect).\n* Text: the [stringified](#stringify) syntax tree, which is hopefully valid for\n  the given syntax.\n\nRun `sast-parse --help` for available options.\n\n### `sast-data`\nParses one or more SCSS (the only supported syntax at this time) files, and\ntransforms all top-level variable declarations into key-value pairs. The result\nis a JSON object in which each key is a variable name, and the value is the\n[jsonified](#jsonify) variable value.\n\nThis is useful for generating [design tokens] from existing SCSS variables if\nyou don't have the ability to go in the other direction.\n\nRun `sast-data --help` for available options and more information.\n\n## Node types\nMost [node types] are defined by [gonzalez], the underlying parser. After\ntransforming each of the syntax tree nodes into [unist nodes], the following\nnodes are introduced:\n\n### Maps\nAny `parentheses` node whose first `operator` child is a `:` is interpreted as\na [Sass map] and recast as a `map` node. The `children` are preserved as-is,\nand key/value pairs separated by `:` and delimited by `,` are placed in the\n`values` property as an array of objects with `key` and `value` properties,\neach of which is a plain old node list. Some examples:\n\n* `(x: 1)` will be [jsonified](#jsonify) as `{x: 1}`\n* `(x: a, y: 2)` will be interpreted as `{x: \"a\", y: 2}`\n\n### Lists\nAny `parentheses` node whose first `operator` child is a `,` is interpreted as\na list (array) and recast as a `list` node. The `children` are perserved as-is,\nand children that aren't `space` nodes are split into subgroups by each `,`\noperator and converted into `value` nodes with one or more children, then\nplaced in the `values` property of the `list` node. Some examples:\n\n* `(1, x)` will be [jsonified](#jsonify) as `[1, \"x\"]`\n* `(a, (b, c))` will be intepreted as `[\"a\", [\"b\", \"c\"]]`\n\n\n[gonzales]: https://github.com/tonyganch/gonzales-pe\n[node types]: https://github.com/tonyganch/gonzales-pe/blob/dev/docs/node-types.md\n[sass map]: https://www.sitepoint.com/using-sass-maps/\n[unist]: https://github.com/syntax-tree/unist\n[unist nodes]: https://github.com/syntax-tree/unist#unist-nodes\n[utilities]: https://github.com/syntax-tree/unist#list-of-utilities\n[npm package]: https://npmjs.com/package/sast\n[YAML]: https://en.wikipedia.org/wiki/YAML\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshawnbot%2Fsast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshawnbot%2Fsast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshawnbot%2Fsast/lists"}