├── .gitignore ├── yarn.lock ├── README.md ├── .github ├── dependabot.yml └── workflows │ └── node.js.yml ├── package.json ├── index.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | prettier@^3.0.0: 6 | version "3.0.3" 7 | resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.3.tgz#432a51f7ba422d1469096c0fdc28e235db8f9643" 8 | integrity sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg== 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # editorconfig-to-prettier 2 | 3 | **This repository's code has been moved into the Prettier repo, please go there with any issues or pull requests. see https://github.com/prettier/prettier/pull/15394 for more details** 4 | 5 | Converts an [editorconfig]-parsed object to a [prettier] configuration. See [this issue] for more details. 6 | 7 | [editorconfig]: https://github.com/editorconfig/editorconfig-core-js 8 | [prettier]: https://github.com/prettier/prettier 9 | [this issue]: https://github.com/josephfrazier/editorconfig-to-prettier/issues/6 10 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "npm" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "editorconfig-to-prettier", 3 | "version": "1.0.0", 4 | "description": "Converts an `editorconfig`-parsed object to a `prettier` configuration", 5 | "main": "index.js", 6 | "files": [], 7 | "scripts": { 8 | "lint": "prettier --list-different *.js", 9 | "fix": "prettier --write *.js", 10 | "prepublishOnly": "npm test", 11 | "pretest": "npm run lint", 12 | "test": "node test" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/josephfrazier/editorconfig-to-prettier.git" 17 | }, 18 | "author": "Joseph Frazier <1212jtraceur@gmail.com>", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/josephfrazier/editorconfig-to-prettier/issues" 22 | }, 23 | "homepage": "https://github.com/josephfrazier/editorconfig-to-prettier#readme", 24 | "devDependencies": { 25 | "prettier": "^3.0.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs 3 | 4 | name: Node.js CI 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [16.x, 18.x, 19.x] 20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 21 | 22 | steps: 23 | - uses: actions/checkout@v3 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v3 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | cache: 'npm' 29 | - run: yarn --frozen-lockfile 30 | - run: yarn run test 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = editorConfigToPrettier; 2 | 3 | function removeUnset(editorConfig) { 4 | const result = {}; 5 | const keys = Object.keys(editorConfig); 6 | for (let i = 0; i < keys.length; i++) { 7 | const key = keys[i]; 8 | if (editorConfig[key] === "unset") { 9 | continue; 10 | } 11 | result[key] = editorConfig[key]; 12 | } 13 | return result; 14 | } 15 | 16 | function editorConfigToPrettier(editorConfig) { 17 | if (!editorConfig) { 18 | return null; 19 | } 20 | 21 | editorConfig = removeUnset(editorConfig); 22 | 23 | if (Object.keys(editorConfig).length === 0) { 24 | return null; 25 | } 26 | 27 | const result = {}; 28 | 29 | if (editorConfig.indent_style) { 30 | result.useTabs = editorConfig.indent_style === "tab"; 31 | } 32 | 33 | if (editorConfig.indent_size === "tab") { 34 | result.useTabs = true; 35 | } 36 | 37 | if (result.useTabs && editorConfig.tab_width) { 38 | result.tabWidth = editorConfig.tab_width; 39 | } else if ( 40 | editorConfig.indent_style === "space" && 41 | editorConfig.indent_size && 42 | editorConfig.indent_size !== "tab" 43 | ) { 44 | result.tabWidth = editorConfig.indent_size; 45 | } else if (editorConfig.tab_width !== undefined) { 46 | result.tabWidth = editorConfig.tab_width; 47 | } 48 | 49 | if (editorConfig.max_line_length) { 50 | if (editorConfig.max_line_length === "off") { 51 | result.printWidth = Number.POSITIVE_INFINITY; 52 | } else { 53 | result.printWidth = editorConfig.max_line_length; 54 | } 55 | } 56 | 57 | if (editorConfig.quote_type === "single") { 58 | result.singleQuote = true; 59 | } else if (editorConfig.quote_type === "double") { 60 | result.singleQuote = false; 61 | } 62 | 63 | if (["cr", "crlf", "lf"].indexOf(editorConfig.end_of_line) !== -1) { 64 | result.endOfLine = editorConfig.end_of_line; 65 | } 66 | 67 | if ( 68 | editorConfig.insert_final_newline === false || 69 | editorConfig.insert_final_newline === true 70 | ) { 71 | result.insertFinalNewline = editorConfig.insert_final_newline; 72 | } 73 | 74 | return result; 75 | } 76 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"); 2 | 3 | const editorconfigToPrettier = require("./"); 4 | 5 | assert.deepStrictEqual( 6 | editorconfigToPrettier({ 7 | indent_style: "tab", 8 | tab_width: 8, 9 | indent_size: 2, 10 | max_line_length: 100, 11 | }), 12 | { 13 | useTabs: true, 14 | tabWidth: 8, 15 | printWidth: 100, 16 | }, 17 | ); 18 | 19 | assert.deepStrictEqual( 20 | editorconfigToPrettier({ 21 | indent_style: "space", 22 | tab_width: 8, 23 | indent_size: 2, 24 | max_line_length: 100, 25 | }), 26 | { 27 | useTabs: false, 28 | tabWidth: 2, 29 | printWidth: 100, 30 | }, 31 | ); 32 | 33 | assert.deepStrictEqual( 34 | editorconfigToPrettier({ 35 | indent_style: "space", 36 | tab_width: 8, 37 | indent_size: 8, 38 | max_line_length: 100, 39 | }), 40 | { 41 | useTabs: false, 42 | tabWidth: 8, 43 | printWidth: 100, 44 | }, 45 | ); 46 | 47 | assert.deepStrictEqual( 48 | editorconfigToPrettier({ 49 | tab_width: 4, 50 | indent_size: "tab", 51 | }), 52 | { 53 | tabWidth: 4, 54 | useTabs: true, 55 | }, 56 | ); 57 | 58 | assert.deepStrictEqual( 59 | editorconfigToPrettier({ 60 | indent_size: "tab", 61 | }), 62 | { 63 | useTabs: true, 64 | }, 65 | ); 66 | 67 | assert.deepStrictEqual( 68 | editorconfigToPrettier({ 69 | tab_width: 0, 70 | indent_size: 0, 71 | }), 72 | { 73 | tabWidth: 0, 74 | }, 75 | ); 76 | 77 | assert.deepStrictEqual( 78 | editorconfigToPrettier({ 79 | quote_type: "single", 80 | }), 81 | { 82 | singleQuote: true, 83 | }, 84 | ); 85 | 86 | assert.deepStrictEqual( 87 | editorconfigToPrettier({ 88 | quote_type: "double", 89 | }), 90 | { 91 | singleQuote: false, 92 | }, 93 | ); 94 | 95 | assert.deepStrictEqual( 96 | editorconfigToPrettier({ 97 | quote_type: "double", 98 | max_line_length: "off", 99 | }), 100 | { 101 | printWidth: Number.POSITIVE_INFINITY, 102 | singleQuote: false, 103 | }, 104 | ); 105 | 106 | assert.deepStrictEqual( 107 | editorconfigToPrettier({ 108 | end_of_line: "cr", 109 | }), 110 | { 111 | endOfLine: "cr", 112 | }, 113 | ); 114 | 115 | assert.deepStrictEqual( 116 | editorconfigToPrettier({ 117 | end_of_line: "crlf", 118 | }), 119 | { 120 | endOfLine: "crlf", 121 | }, 122 | ); 123 | 124 | assert.deepStrictEqual( 125 | editorconfigToPrettier({ 126 | end_of_line: "lf", 127 | }), 128 | { 129 | endOfLine: "lf", 130 | }, 131 | ); 132 | 133 | assert.deepStrictEqual( 134 | editorconfigToPrettier({ 135 | endOfLine: 123, 136 | }), 137 | {}, 138 | ); 139 | 140 | assert.deepStrictEqual( 141 | editorconfigToPrettier({ 142 | indent_style: "space", 143 | indent_size: 2, 144 | max_line_length: "unset", 145 | }), 146 | { 147 | useTabs: false, 148 | tabWidth: 2, 149 | }, 150 | ); 151 | 152 | assert.deepStrictEqual( 153 | editorconfigToPrettier({ insert_final_newline: false }), 154 | { insertFinalNewline: false }, 155 | ); 156 | 157 | assert.deepStrictEqual(editorconfigToPrettier({ insert_final_newline: true }), { 158 | insertFinalNewline: true, 159 | }); 160 | 161 | assert.deepStrictEqual(editorconfigToPrettier({}), null); 162 | assert.deepStrictEqual(editorconfigToPrettier(null), null); 163 | --------------------------------------------------------------------------------