{"id":13527975,"url":"https://github.com/rdio/jsfmt","last_synced_at":"2025-05-15T02:08:33.499Z","repository":{"id":14660110,"uuid":"17378541","full_name":"rdio/jsfmt","owner":"rdio","description":"For formatting, searching, and rewriting JavaScript.","archived":false,"fork":false,"pushed_at":"2020-04-04T12:38:35.000Z","size":235,"stargazers_count":1690,"open_issues_count":35,"forks_count":54,"subscribers_count":77,"default_branch":"master","last_synced_at":"2025-05-14T15:56:07.489Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://rdio.github.io/jsfmt/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rdio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.MD","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-03T20:12:52.000Z","updated_at":"2025-04-28T12:00:41.000Z","dependencies_parsed_at":"2022-09-26T20:11:50.394Z","dependency_job_id":null,"html_url":"https://github.com/rdio/jsfmt","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rdio%2Fjsfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rdio%2Fjsfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rdio%2Fjsfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rdio%2Fjsfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rdio","download_url":"https://codeload.github.com/rdio/jsfmt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259384,"owners_count":22040820,"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":[],"created_at":"2024-08-01T06:02:09.033Z","updated_at":"2025-05-15T02:08:33.441Z","avatar_url":"https://github.com/rdio.png","language":"JavaScript","readme":"jsfmt\n===\n\n[![NPM version](https://badge.fury.io/js/jsfmt.svg)](http://badge.fury.io/js/jsfmt)\n[![Build Status](https://travis-ci.org/rdio/jsfmt.svg?branch=master)](https://travis-ci.org/rdio/jsfmt)\n[![Dependency Status](https://david-dm.org/rdio/jsfmt.svg)](https://david-dm.org/rdio/jsfmt)\n[![Coverage Status](https://coveralls.io/repos/rdio/jsfmt/badge.svg)](https://coveralls.io/r/rdio/jsfmt)\n\nFor formatting, searching, and rewriting JavaScript. Analogous to [`gofmt`](http://golang.org/cmd/gofmt/).\n\nInstallation\n---\n\n`npm install -g jsfmt`\n\nUsage\n---\n\n```\n$ jsfmt --help\nUsage:\n  jsfmt [--no-format] [--save-ast] [--diff|--list|--write] [--validate] [--rewrite PATTERN|--search PATTERN] [--json|--ast] [\u003cfile\u003e...]\n  jsfmt (--version | --help)\n\nOptions:\n  -h --help                      Show this help text\n  --version                      Show jsfmt version\n  -d --diff                      Show diff against original file\n  -l --list                      List the files which differ from jsfmt output\n  -v --validate                  Validate the input file(s)\n  --no-format                    Do not format the input file(s)\n  -w --write                     Overwrite the original file with jsfmt output\n  -j --json                      Tell jsfmt that the file being parsed is json\n  -a --ast                       Tell jsfmt that the file being parsed is in JSON AST\n  --save-ast                     Output the resulting js in JSON AST format\n  -r=PATTERN --rewrite PATTERN   Rewrite rule (e.g., 'a.slice(b, len(a) -\u003e a.slice(b)')\n  -s=PATTERN --search PATTERN    Search rule (e.g., 'a.slice')\n```\n\nIf no path is given it will read from `stdin`. A directory path will recurse over all *.js files in the directory.\n\nNote that the AST options (`--ast` and `--save-ast`) are experimental and may be removed.\n\nFormatting\n---\n\nFor formatting `jsfmt` uses [esformatter](https://github.com/millermedeiros/esformatter).\n\n### .jsfmtrc\n\nAny of the [esformatter](https://github.com/millermedeiros/esformatter) formatting\noptions can be overwritten via a `.jsfmtrc` file. The file is parsed using\n[rc](https://github.com/dominictarr/rc), which accepts either a `json` or `ini` formatted file.\n\nA `.jsfmtrc` will be read if it exists in any of the following directories:\n* a local .jsfmtrc or the first found looking in ./ ../ ../../ ../../../ etc.\n* $HOME/.jsfmtrc\n* $HOME/.jsfmt/config\n* $HOME/.config/jsfmt\n* $HOME/.config/jsfmt/config\n* /etc/jsfmtrc\n* /etc/jsfmt/config\n\n`jsfmt` will also attempt to pickup and use the configured `indent`\nvariable from your `.jshintrc` configuration file, if present.\n\nRewriting\n---\n\nThe `--rewrite` flag allows rewriting portions of the JavaScript's AST before formatting. This is especially handy for intelligent renaming and handling API changes from a library. The rewrite rule must be a string of the form:\n\n    pattern -\u003e replacement\n\nBoth `pattern` and `replacement` must be valid JavaScript. In `pattern`, single-character lowercase identifiers serve as wildcards matching arbitrary expressions; those expressions will be substituted for the same identifiers in the `replacement`.\n\n### Example\n\nRewrite occurences of `_.reduce` to use native reduce:\n\n    jsfmt --rewrite \"_.reduce(a, b, c) -\u003e a.reduce(b, c)\" reduce.js\n\nSearching\n---\n\nThe `--search` flag allows searching through a JavaScript's AST. The search rule is very similar to the rewrite rule but just outputs expressions that match the given search expression. The search expression must be valid JavaScript.\n\n### Example\n\nFind occurences of `_.reduce`:\n\n    jsfmt --search \"_.reduce(a, b, c)\" reduce.js\n\nValidating\n---\n\nThe `--validate` flag will print any errors found by esprima while parsing the JavaScript.\n\n### Example\n\n    jsfmt --validate bad.js\n\nAPI\n---\n\n### Formatting\n\n```javascript\njsfmt.format(\u003cjavascript_string\u003e, \u003cconfig_object\u003e) // Returns formatted JavaScript\n```\n\n```javascript\njsfmt.formatJSON(\u003cJSON_string\u003e, \u003cconfig_object\u003e) // Returns formatted JSON\n```\n\n```javascript\nvar config = jsfmt.getConfig(); // Loads the jsfmt config from the appropriate rc file or default config object\n```\n\n#### Example\n\n```javascript\nvar jsfmt = require('jsfmt');\nvar fs = require('fs');\n\nvar js = fs.readFileSync('unformatted.js');\nvar config = jsfmt.getConfig();\n\njs = jsfmt.format(js, config);\n```\n\n### Rewriting\n\n```javascript\njsfmt.rewrite(\u003cjavascript_string\u003e, \u003crewrite_rule\u003e) // Returns rewritten JavaScript\n```\n\n#### Example\n\n```javascript\nvar jsfmt = require('jsfmt');\nvar fs = require('fs');\n\nvar js = fs.readFileSync('each.js');\n\njs = jsfmt.rewrite(js, \"_.each(a, b) -\u003e a.forEach(b)\");\n```\n\n### Searching\n\n```javascript\njsfmt.search(\u003cjavascript_string\u003e, \u003csearch_expression\u003e) // Returns array of matches\n```\n\n#### Example\n\n```javascript\nvar jsfmt = require('jsfmt');\nvar fs = require('fs');\n\nvar js = fs.readFileSync('component.js');\n\njsfmt.search(js, \"R.Component.create(a, { dependencies: z })\").forEach(function(matches, wildcards) {\n  console.log(wildcards.z);\n});\n```\n\n### Validating\n\n```javascript\njsfmt.validate(\u003cjavascript_string\u003e) // Returns errors found while parsing JavaScript\n```\n\n```javascript\njsfmt.validateJSON(\u003cJSON_string\u003e) // Returns errors found while parsing JSON\n```\n\n#### Example\n\n```javascript\nvar jsfmt = require('jsfmt');\nvar fs = require('fs');\n\nvar js = fs.readFileSync('each.js');\nvar errors = jsfmt.validate(js);\n\nfor (var i = 0; i \u003c errors.length; i++) {\n  console.error(errors[i]);\n}\n```\n\nPlugins\n-------\n\nSince `jsfmt` uses `esformatter` under the covers for formatting your code you can utilize any `esformatter` plugins with `jsfmt`. Please see https://github.com/millermedeiros/esformatter/#plugins for more information.\n\n### JSX\n\nThere exists a plugin [esformatter-jsx](https://github.com/royriojas/esformatter-jsx) which provides support for formatting JSX with `esformatter`. Please see https://github.com/royriojas/esformatter-jsx/wiki/Usage-with-jsfmt for more information on setting up with `jsfmt`.\n\nLinks\n---\n\n- vim-jsfmt.vim - https://github.com/mephux/vim-jsfmt - \"Format javascript source on save.\"\n- Atom Package - https://atom.io/packages/atom-jsfmt - \"Automatically run jsfmt every time you save a JavaScript source file.\"\n- Grunt Task - https://github.com/james2doyle/grunt-jsfmt - \"A task for the jsfmt library.\"\n- Emacs Plugin - https://github.com/brettlangdon/jsfmt.el - \"Run jsfmt from within emacs\"\n- Gulp Task - https://github.com/blai/gulp-jsfmt - \"A gulp task for jsfmt.\"  \n- Sublime Text plugin - https://github.com/ionutvmi/sublime-jsfmt - \"On-demand and automatic jsfmt from Sublime Text 2 and 3\"\n\nChangelog\n---\n\n### v0.4.0\n\n- Added two new command-line args for AST formatting. Note that these are experimental and may be removed.\n- Removed `--config` option in favor of .jsfmtrc and better docs around rc.\n- Updated esformatter and using new esformatter plugin for automatic brace insertion.\n- Updated style guide to include esformatter changes.\n- Fixes and cleanup for shebang.\n- Support for variable arguments using ES6 rest syntax.\n- General rewrite cleanup.\n- Changing exit code to `-1` on missing arg failure.\n- Updates to `rc` and other dependencies.\n\n### v0.3.2\n\n- Adding support for `UnaryExpression`\n- Fixing bug where rewrite types were not being set properly\n\n### v0.3.1\n\n- Fixed bug when searching for expressions within BlockStatement or Program body\n- Added JSON support\n\n### v0.3.0\n\n- Added CONTRIBUTING\n- Added tests\n- Added Gruntfile for development\n- Added CI support\n- Added style guide\n- Added default formatting config\n- Exposed `jsfmt.getConfig` api method for loading jsfmt config\n- Exposed `jsfmt.format(js[, options])` api method for formatting\n- Added `--validate` option and exposed `jsfmt.validate` api method\n- Pinned dependencies\n\n### v0.2.0\n\n- Add [rc](https://github.com/dominictarr/rc) and `--config config.json` support for formatting configuration\n- Making `--format` the default action\n- Fix support for shebang at the top of js files, e.g. `#!/usr/bin/env node`\n- Fix jsfmt diff mode where whitespace was unaccounted for due to `-b` git diff option\n\n### v0.1.1\n\n- Initial release\n\nLicense\n---\nApache License, Version 2.0. Copyright 2014 Rdio, Inc.\n","funding_links":[],"categories":["JavaScript","QA Tools","QA Tools [🔝](#readme)","QA 工具"],"sub_categories":["Runner","运行器","运行器e2e测试"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frdio%2Fjsfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frdio%2Fjsfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frdio%2Fjsfmt/lists"}