From 002c2a70f09590c9c85ddd4e652e6a1c5d0f1c1a Mon Sep 17 00:00:00 2001 From: Rob Wise Date: Sat, 4 Mar 2017 11:49:52 -0500 Subject: [PATCH] feat($prettier-eslint): Integrate prettier-eslint As discussed in: - https://github.com/jlongster/prettier-atom/issues/57 - https://github.com/kentcdodds/prettier-eslint-atom/issues/28 - https://github.com/prettier/prettier/issues/758 This commit completely refactors the repo. We now allow using prettier-eslint as an option so that you do not need to install a separate plugin (prettier-eslint-atom). Under the hood, almost all of the code has been refactored and covered by Jest integration tests. We also implement Babel and many of the devops/build configurations that Kent Dodds authored in the prettier-eslint-atom repo. Resolves #57 --- .all-contributorsrc | 135 + .babelrc | 4 + .eslintignore | 5 + .eslintrc.yml | 23 + .flowconfig | 9 + .gitattributes | 2 + .gitignore | 4 + .travis.yml | 10 + CODE_OF_CONDUCT.md | 74 + CONTRIBUTING.md | 67 + LICENSE.md | 2 +- README.md | 95 +- decls/index.js | 42 + dist/executePrettier.js | 72 + dist/format.js | 26 + dist/formatOnSave.js | 29 + dist/helpers.js | 132 + dist/main.js | 43 + flow-typed/npm/all-contributors-cli_vx.x.x.js | 207 + flow-typed/npm/atom-linter_vx.x.x.js | 46 + flow-typed/npm/babel-cli_vx.x.x.js | 108 + flow-typed/npm/babel-eslint_vx.x.x.js | 73 + flow-typed/npm/babel-jest_vx.x.x.js | 32 + ...lugin-transform-flow-strip-types_vx.x.x.js | 32 + flow-typed/npm/babel-preset-env_vx.x.x.js | 74 + flow-typed/npm/babel-preset-stage-2_vx.x.x.js | 32 + flow-typed/npm/commitizen_vx.x.x.js | 235 + .../npm/cz-conventional-changelog_vx.x.x.js | 38 + .../npm/eslint-config-airbnb-base_vx.x.x.js | 101 + .../npm/eslint-plugin-flowtype_vx.x.x.js | 319 ++ flow-typed/npm/eslint-plugin-import_vx.x.x.js | 326 ++ flow-typed/npm/eslint-plugin-jest_vx.x.x.js | 60 + flow-typed/npm/eslint_vx.x.x.js | 2293 +++++++++ flow-typed/npm/find-root_vx.x.x.js | 38 + flow-typed/npm/flow-bin_v0.x.x.js | 6 + flow-typed/npm/flow-typed_vx.x.x.js | 158 + flow-typed/npm/husky_vx.x.x.js | 53 + flow-typed/npm/jest-cli_vx.x.x.js | 305 ++ flow-typed/npm/jest_v19.x.x.js | 449 ++ flow-typed/npm/loophole_vx.x.x.js | 32 + flow-typed/npm/minimatch_vx.x.x.js | 32 + flow-typed/npm/nps-utils_vx.x.x.js | 32 + flow-typed/npm/nps_vx.x.x.js | 130 + flow-typed/npm/opt-cli_vx.x.x.js | 53 + flow-typed/npm/prettier-eslint-cli_vx.x.x.js | 60 + flow-typed/npm/prettier-eslint_vx.x.x.js | 46 + flow-typed/npm/prettier_vx.x.x.js | 136 + flow-typed/npm/rimraf_vx.x.x.js | 39 + lib/format.js | 22 - lib/formatOnSave.js | 21 - lib/helpers.js | 15 - package-scripts.yml | 61 + package.json | 288 +- plugin.gif | Bin 0 -> 62342 bytes src/__snapshots__/helpers.test.js.snap | 13 + {lib => src}/executePrettier.js | 43 +- src/executePrettier.test.js | 110 + src/format.js | 18 + src/format.test.js | 48 + src/formatOnSave.js | 24 + src/formatOnSave.test.js | 80 + src/helpers.js | 93 + src/helpers.test.js | 198 + {lib => src}/main.js | 17 +- tests/fixtures/.eslintignore | 1 + tests/fixtures/doesNotMatchEslintignore.js | 0 tests/fixtures/matchesEslintignore.js | 0 tests/mocks/textEditor.js | 25 + yarn.lock | 4400 +++++++++++++++++ 69 files changed, 11574 insertions(+), 222 deletions(-) create mode 100644 .all-contributorsrc create mode 100644 .babelrc create mode 100644 .eslintignore create mode 100644 .eslintrc.yml create mode 100644 .flowconfig create mode 100644 .gitattributes create mode 100644 .travis.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 decls/index.js create mode 100644 dist/executePrettier.js create mode 100644 dist/format.js create mode 100644 dist/formatOnSave.js create mode 100644 dist/helpers.js create mode 100644 dist/main.js create mode 100644 flow-typed/npm/all-contributors-cli_vx.x.x.js create mode 100644 flow-typed/npm/atom-linter_vx.x.x.js create mode 100644 flow-typed/npm/babel-cli_vx.x.x.js create mode 100644 flow-typed/npm/babel-eslint_vx.x.x.js create mode 100644 flow-typed/npm/babel-jest_vx.x.x.js create mode 100644 flow-typed/npm/babel-plugin-transform-flow-strip-types_vx.x.x.js create mode 100644 flow-typed/npm/babel-preset-env_vx.x.x.js create mode 100644 flow-typed/npm/babel-preset-stage-2_vx.x.x.js create mode 100644 flow-typed/npm/commitizen_vx.x.x.js create mode 100644 flow-typed/npm/cz-conventional-changelog_vx.x.x.js create mode 100644 flow-typed/npm/eslint-config-airbnb-base_vx.x.x.js create mode 100644 flow-typed/npm/eslint-plugin-flowtype_vx.x.x.js create mode 100644 flow-typed/npm/eslint-plugin-import_vx.x.x.js create mode 100644 flow-typed/npm/eslint-plugin-jest_vx.x.x.js create mode 100644 flow-typed/npm/eslint_vx.x.x.js create mode 100644 flow-typed/npm/find-root_vx.x.x.js create mode 100644 flow-typed/npm/flow-bin_v0.x.x.js create mode 100644 flow-typed/npm/flow-typed_vx.x.x.js create mode 100644 flow-typed/npm/husky_vx.x.x.js create mode 100644 flow-typed/npm/jest-cli_vx.x.x.js create mode 100644 flow-typed/npm/jest_v19.x.x.js create mode 100644 flow-typed/npm/loophole_vx.x.x.js create mode 100644 flow-typed/npm/minimatch_vx.x.x.js create mode 100644 flow-typed/npm/nps-utils_vx.x.x.js create mode 100644 flow-typed/npm/nps_vx.x.x.js create mode 100644 flow-typed/npm/opt-cli_vx.x.x.js create mode 100644 flow-typed/npm/prettier-eslint-cli_vx.x.x.js create mode 100644 flow-typed/npm/prettier-eslint_vx.x.x.js create mode 100644 flow-typed/npm/prettier_vx.x.x.js create mode 100644 flow-typed/npm/rimraf_vx.x.x.js delete mode 100644 lib/format.js delete mode 100644 lib/formatOnSave.js delete mode 100644 lib/helpers.js create mode 100644 package-scripts.yml create mode 100644 plugin.gif create mode 100644 src/__snapshots__/helpers.test.js.snap rename {lib => src}/executePrettier.js (52%) create mode 100644 src/executePrettier.test.js create mode 100644 src/format.js create mode 100644 src/format.test.js create mode 100644 src/formatOnSave.js create mode 100644 src/formatOnSave.test.js create mode 100644 src/helpers.js create mode 100644 src/helpers.test.js rename {lib => src}/main.js (71%) create mode 100644 tests/fixtures/.eslintignore create mode 100644 tests/fixtures/doesNotMatchEslintignore.js create mode 100644 tests/fixtures/matchesEslintignore.js create mode 100644 tests/mocks/textEditor.js create mode 100644 yarn.lock diff --git a/.all-contributorsrc b/.all-contributorsrc new file mode 100644 index 00000000..4c38b77a --- /dev/null +++ b/.all-contributorsrc @@ -0,0 +1,135 @@ +{ + "projectName": "prettier-atom", + "projectOwner": "robwise", + "files": [ + "README.md" + ], + "imageSize": 100, + "commit": false, + "contributors": [ + { + "login": "jlongster", + "name": "James Long", + "avatar_url": "https://github.com/avatars/u/17031?v=3", + "profile": "http://jlongster.com", + "contributions": [ + "question", + "code", + "doc", + "plugin", + "review" + ] + }, + { + "login": "robwise", + "name": "Rob Wise", + "avatar_url": "https://github.com/avatars/u/6173488?v=3", + "profile": "https://robwise.github.io", + "contributions": [ + "code", + "doc" + ] + }, + { + "login": "kentcdodds", + "name": "Kent C. Dodds", + "avatar_url": "https://github.com/avatars/u/1500684?v=3", + "profile": "https://kentcdodds.com", + "contributions": [ + "code", + "doc", + "infra" + ] + }, + { + "login": "cloud-walker", + "name": "Luca Barone", + "avatar_url": "https://github.com/avatars/u/1144075?v=3", + "profile": "https://github.com/cloud-walker", + "contributions": [] + }, + { + "login": "arnarthor", + "name": "Arnar Þór Sveinsson", + "avatar_url": "https://github.com/avatars/u/4514159?v=3", + "profile": "https://github.com/arnarthor", + "contributions": [ + "code" + ] + }, + { + "login": "skevy", + "name": "Adam Miskiewicz", + "avatar_url": "https://github.com/avatars/u/131916?v=3", + "profile": "http://www.adammiskiewicz.com/", + "contributions": [ + "code" + ] + }, + { + "login": "oriSomething", + "name": "Ori Livni", + "avatar_url": "https://github.com/avatars/u/2685242?v=3", + "profile": "http://www.orilivni.com", + "contributions": [ + "code" + ] + }, + { + "login": "transcranial", + "name": "Leon Chen", + "avatar_url": "https://github.com/avatars/u/6182852?v=3", + "profile": "https://transcranial.github.io", + "contributions": [ + "code" + ] + }, + { + "login": "vjeux", + "name": "Christopher Chedeau", + "avatar_url": "https://github.com/avatars/u/197597?v=3", + "profile": "http://blog.vjeux.com/", + "contributions": [ + "question", + "code", + "plugin" + ] + }, + { + "login": "1st8", + "name": "Christoph Geschwind", + "avatar_url": "https://github.com/avatars/u/646693?v=3", + "profile": "http://christoph-geschwind.de", + "contributions": [ + "code" + ] + }, + { + "login": "ahutchings", + "name": "Andrew Hutchings", + "avatar_url": "https://github.com/avatars/u/35026?v=3", + "profile": "https://andrewhutchings.com", + "contributions": [ + "code" + ] + }, + { + "login": "schnerd", + "name": "David Schnurr", + "avatar_url": "https://github.com/avatars/u/875591?v=3", + "profile": "http://davidschnurr.com", + "contributions": [ + "code" + ] + }, + { + "login": "ryancole", + "name": "Ryan Cole", + "avatar_url": "https://github.com/avatars/u/484801?v=3", + "profile": "http://rycole.com/", + "contributions": [ + "code" + ] + } + ] +} diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000..35432ba7 --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": [["env", { "electron": 1.4 }], "stage-2"], + "plugins": ["transform-flow-strip-types"] +} diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..de9cf435 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +node_modules +.nyc_output +coverage +dist +flow-typed/npm diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 00000000..e7d57c8c --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,23 @@ +extends: + - airbnb-base + - plugin:flowtype/recommended + - plugin:jest/recommended + +parser: babel-eslint + +plugins: + - flowtype + - jest + +globals: + atom: true + +env: + node: true + jest: true + +rules: + no-confusing-arrow: 0 # not that confusing + max-len: + - 2 + - 110 diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 00000000..9068289b --- /dev/null +++ b/.flowconfig @@ -0,0 +1,9 @@ +[ignore] +dist + +[include] + +[libs] +decls/ + +[options] diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..391f0a4e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +*.js text eol=lf diff --git a/.gitignore b/.gitignore index 93f13619..1695b2ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ node_modules npm-debug.log +.nyc_output +coverage +.opt-in +.opt-out diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..3eaee8a2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +cache: + directories: + - node_modules +notifications: + email: false +node_js: + - '6' +script: + - yarn start validate diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..e7242187 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at kent+coc@doddsfamily.us. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..1df25a25 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,67 @@ +# Contributing + +Thanks for being willing to contribute! + +**Working on your first Pull Request?** You can learn how from this *free* series +[How to Contribute to an Open Source Project on GitHub][egghead] + +## Project setup + +1. Fork and clone the repo +2. `$ yarn install` to install dependencies +3. `$ yarn start validate` to validate you've got it working +4. Create a branch for your PR + +This project uses [`nps`][nps] and you can run `yarn start` to see what scripts are available. + +## Add yourself as a contributor + +This project follows the [all contributors][all-contributors] specification. To add yourself to the table of +contributors on the README.md, please use the automated script as part of your PR: + +```console +yarn start addContributor +``` + +Follow the prompt. If you've already added yourself to the list and are making a new type of contribution, you can run +it again and select the added contribution type. + +## Committing and Pushing changes + +This project uses [`semantic-release`][semantic-release] to do automatic releases and generate a changelog based on the +commit history. So we follow [a convention][convention] for commit messages. Please follow this convention for your +commit messages. + +You can use `commitizen` to help you to follow [the convention][convention] + +Once you are ready to commit the changes, please use the below commands + +1. `git add ` +2. `$ yarn start commit` + +... and follow the instruction of the interactive prompt. + +### opt into git hooks + +There are git hooks set up with this project that are automatically installed when you install dependencies. They're +really handy, but are turned off by default (so as to not hinder new contributors). You can opt into these by creating +a file called `.opt-in` at the root of the project and putting this inside: + +``` +commit-msg +pre-commit +``` + +## Help needed + +Please checkout the [ROADMAP.md][ROADMAP] and raise an issue to discuss +any of the items in the want to do or might do list. + +Also, please watch the repo and respond to questions/bug reports/feature requests! Thanks! + +[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github +[semantic-release]: https://npmjs.com/package/semantic-release +[convention]: https://github.com/conventional-changelog/conventional-changelog-angular/blob/ed32559941719a130bb0327f886d6a32a8cbc2ba/convention.md +[all-contributors]: https://github.com/kentcdodds/all-contributors +[ROADMAP]: ./ROADMAP.md +[nps]: https://www.npmjs.com/package/nps diff --git a/LICENSE.md b/LICENSE.md index b352b9c4..e83eaad0 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) Stephen Kubovic +Copyright (c) Rob Wise Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/README.md b/README.md index 38b7f1a9..aab282e5 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,94 @@ -# Prettier formatter for Atom +# Prettier for Atom -Atom package to format your Javascript using [Prettier](https://github.com/jlongster/prettier). +Atom package to format your JavaScript using Prettier. Comes with powerful optional ESlint integration. -### Usage + + + -#### Keybindings +[![Version][version-badge]][package] +[![Downloads][downloads-badge]][package] +[![MIT License][license-badge]][LICENSE] -Use `ctrl-alt-f` to format the current Javascript file. If a text selection is made, only the selected text will be formatted. +[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors) +[![PRs Welcome][prs-badge]][prs] +[![Code of Conduct][coc-badge]][coc] -#### Menu +[![Watch on GitHub][github-watch-badge]][github-watch] +[![Star on GitHub][github-star-badge]][github-star] +[![Tweet][twitter-badge]][twitter] -*Packages > prettier > Format* +![Demo](./plugin.gif) + +## Installation + +``` +apm install prettier-atom +``` + +Or, Settings → Install → Search for `prettier-atom` + +## Usage + +Use two ways: + +- Invoke manually using the keyboard shortcut (if no selection, whole file is formatted): ctrl + alt + f +- Automatically format on save (requires enabling in Package → `prettier-atom` → Format) + +If you use ESlint, check the "ESLint Integration" checkbox and poof, everything should work (we use Kent Dodds's [`prettier-eslint`][prettier-eslint] plugin under the hood). We will recursively search up the file tree for your package.json and eslint settings and use them when formatting. + +More detailed descriptions of each option can be found in the Atom settings for this plugin. + +Please open a pull request or file an issue if you notice bugs or something doesn't work as it should! + +## Inspiration + +This repository was created by [James Long][james-long] to go along with his Prettier project. [Kent Dodds][kent-dodds] forked that project and created prettier-eslint and an accompanying atom plugin. Because it became clear that the projects were very similar and thus maintenance made easier by combining the two, prettier-eslint-atom was merged back into prettier-atom. + +## Contributors + +Thanks goes to these people ([emoji key][emojis]): + + +| [
James Long](http://jlongster.com)
💬 [💻](https://github.com/robwise/prettier-atom/commits?author=jlongster) [📖](https://github.com/robwise/prettier-atom/commits?author=jlongster) 🔌 👀 | [
Rob Wise](https://robwise.github.io)
[💻](https://github.com/robwise/prettier-atom/commits?author=robwise) [📖](https://github.com/robwise/prettier-atom/commits?author=robwise) | [
Kent C. Dodds](https://kentcdodds.com)
[💻](https://github.com/robwise/prettier-atom/commits?author=kentcdodds) [📖](https://github.com/robwise/prettier-atom/commits?author=kentcdodds) 🚇 | [
Luca Barone](https://github.com/cloud-walker)
| [
Arnar Þór Sveinsson](https://github.com/arnarthor)
[💻](https://github.com/robwise/prettier-atom/commits?author=arnarthor) | [
Adam Miskiewicz](http://www.adammiskiewicz.com/)
[💻](https://github.com/robwise/prettier-atom/commits?author=skevy) | [
Ori Livni](http://www.orilivni.com)
[💻](https://github.com/robwise/prettier-atom/commits?author=oriSomething) | +| :---: | :---: | :---: | :---: | :---: | :---: | :---: | +| [
Leon Chen](https://transcranial.github.io)
[💻](https://github.com/robwise/prettier-atom/commits?author=transcranial) | [
Christopher Chedeau](http://blog.vjeux.com/)
💬 [💻](https://github.com/robwise/prettier-atom/commits?author=vjeux) 🔌 | [
Christoph Geschwind](http://christoph-geschwind.de)
[💻](https://github.com/robwise/prettier-atom/commits?author=1st8) | [
Andrew Hutchings](https://andrewhutchings.com)
[💻](https://github.com/robwise/prettier-atom/commits?author=ahutchings) | [
David Schnurr](http://davidschnurr.com)
[💻](https://github.com/robwise/prettier-atom/commits?author=schnerd) | [
Ryan Cole](http://rycole.com/)
[💻](https://github.com/robwise/prettier-atom/commits?author=ryancole) | + + +This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome! + +## LICENSE + +[MIT](./LICENSE.md) + +[npm]: https://www.npmjs.com/ +[node]: https://nodejs.org +[build-badge]: https://img.shields.io/travis/jlongster/prettier-atom.svg?style=flat-square +[build]: https://travis-ci.org/jlongster/prettier-atom +[coverage-badge]: https://img.shields.io/codecov/c/github/jlongster/prettier-atom.svg?style=flat-square +[coverage]: https://codecov.io/github/jlongster/prettier-atom +[dependencyci-badge]: https://dependencyci.com/github/jlongster/prettier-atom/badge?style=flat-square +[dependencyci]: https://dependencyci.com/github/jlongster/prettier-atom +[version-badge]: https://img.shields.io/apm/v/prettier-atom.svg?style=flat-square +[package]: https://atom.io/packages/prettier-atom +[downloads-badge]: https://img.shields.io/apm/dm/prettier-atom.svg?style=flat-square +[license-badge]: https://img.shields.io/apm/l/prettier-atom.svg?style=flat-square +[license]: https://github.com/jlongster/prettier-atom/blob/master/LICENSE +[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square +[prs]: http://makeapullrequest.com +[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square +[coc]: https://github.com/jlongster/prettier-atom/blob/master/other/CODE_OF_CONDUCT.md +[roadmap-badge]: https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square +[roadmap]: https://github.com/jlongster/prettier-atom/blob/master/other/ROADMAP.md +[github-watch-badge]: https://img.shields.io/github/watchers/jlongster/prettier-atom.svg?style=social +[github-watch]: https://github.com/jlongster/prettier-atom/watchers +[github-star-badge]: https://img.shields.io/github/stars/jlongster/prettier-atom.svg?style=social +[github-star]: https://github.com/jlongster/prettier-atom/stargazers +[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20prettier-atom!%20https://github.com/robwawise/prettier-atom%20%F0%9F%91%8D +[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/jlongster/prettier-atom.svg?style=social +[emojis]: https://github.com/jlongster/all-contributors#emoji-key +[all-contributors]: https://github.com/jlongster/all-contributors +[prettier]: https://github.com/jlongster/prettier +[prettier-eslint]: https://github.com/jlongster/prettier-atom +[kent-dodds]: https://github.com/kentcdodds +[james-long]: https://github.com/jlongster diff --git a/decls/index.js b/decls/index.js new file mode 100644 index 00000000..406c6ed1 --- /dev/null +++ b/decls/index.js @@ -0,0 +1,42 @@ +// types for working with Atom stuff +declare type FilePath = string; +declare type Point = {row: number, column: number}; +declare type Range = {start: Point, end: Point}; +declare type Ranges = Array; +// eslint-disable-next-line no-undef +declare type Globs = Array; +// eslint-disable-next-line no-undef +declare type TextEditor = { + getGrammar: () => {scopeName: string}, + getBuffer: () => {getRange: () => Range}, + getCursorScreenPosition: () => Point, + getLastCursor: () => {getScopeDescriptor: () => string}, + getSelectedText: () => string, + getSelectedBufferRanges: () => Ranges, + // getTextInRange: () => string, + getTextInBufferRange: () => string, + setCursorScreenPosition: (point: Point) => Point, + setTextInBufferRange: (bufferRange: Range, text: string) => Range, + buffer: {file: {path: FilePath}}, + backwardsScanInBufferRange: ( + regex: RegExp, + Range: Range, + iter: ( + { + match: string, + matchText: string, + range: Range, + stop: Function, + replace: (replacement: string) => void, + } + ) => void + ) => void, +}; +declare var atom: { + config: { + get: (key: string) => any, + }, + notifications: { + addError: (message: string, options?: {detail?: string, dismissable?: boolean}) => void, + }, +}; diff --git a/dist/executePrettier.js b/dist/executePrettier.js new file mode 100644 index 00000000..3644dd07 --- /dev/null +++ b/dist/executePrettier.js @@ -0,0 +1,72 @@ +'use strict'; + +var prettierEslint = require('prettier-eslint'); +var prettier = require('prettier'); + +var _require = require('loophole'), + allowUnsafeNewFunction = _require.allowUnsafeNewFunction; + +var _require2 = require('./helpers'), + getPrettierOptions = _require2.getPrettierOptions, + getCurrentFilePath = _require2.getCurrentFilePath, + shouldDisplayErrors = _require2.shouldDisplayErrors, + shouldUseEslint = _require2.shouldUseEslint; + +var EMBEDDED_JS_REGEX = /]*>([\s\S]*?)(?=<\/script>)/gi; + +var displayError = function displayError(error) { + var message = 'prettier-atom: ' + error.toString(); + var detail = error.stack.toString(); + + atom.notifications.addError(message, { detail: detail, dismissable: true }); +}; + +var handleError = function handleError(error) { + if (shouldDisplayErrors()) displayError(error); + return false; +}; + +var executePrettier = function executePrettier(editor, text) { + try { + if (shouldUseEslint()) { + return allowUnsafeNewFunction(function () { + return prettierEslint({ text: text, filePath: getCurrentFilePath(editor) }); + }); + } + return prettier.format(text, getPrettierOptions(editor)); + } catch (error) { + return handleError(error); + } +}; + +var executePrettierOnBufferRange = function executePrettierOnBufferRange(editor, bufferRange) { + var cursorPositionPriorToFormat = editor.getCursorScreenPosition(); + var textToTransform = editor.getTextInBufferRange(bufferRange); + var transformed = executePrettier(editor, textToTransform); + + if (!transformed) return; + + editor.setTextInBufferRange(bufferRange, transformed); + editor.setCursorScreenPosition(cursorPositionPriorToFormat); +}; + +var executePrettierOnEmbeddedScripts = function executePrettierOnEmbeddedScripts(editor) { + return editor.backwardsScanInBufferRange(EMBEDDED_JS_REGEX, editor.getBuffer().getRange(), function (iter) { + // Create new range with start row advanced by 1, + // since we cannot use look-behind on variable-length starting + //