From 241059d558707bbff4a11ed4f446458f31c67ea6 Mon Sep 17 00:00:00 2001 From: matzkoh Date: Sun, 7 Jul 2019 04:01:14 +0900 Subject: [PATCH] BREAKING CHANGE: replace sort logic with sort-package-json and prettier is in peer dependencies now. --- README.md | 50 ++++++-------------------- lib/index.js | 17 +++++---- lib/printer.js | 90 ---------------------------------------------- package-lock.json | 22 +++++++++++- package.json | 34 +++++++++++++----- prettier.config.js | 8 ----- test/index.js | 70 +++++++++++++++++++++++------------- 7 files changed, 110 insertions(+), 181 deletions(-) delete mode 100644 lib/printer.js diff --git a/README.md b/README.md index 8c9c4bb..f673659 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,18 @@ -# Prettier plugin for package.json - [![CircleCI](https://circleci.com/gh/matzkoh/prettier-plugin-packagejson.svg?style=shield)](https://circleci.com/gh/matzkoh/prettier-plugin-packagejson) [![Renovate](https://badges.renovateapi.com/github/matzkoh/prettier-plugin-packagejson)](https://renovatebot.com/) +[![npm](https://img.shields.io/npm/v/prettier-plugin-packagejson.svg)](https://www.npmjs.com/package/prettier-plugin-packagejson) -## Installation - -```bash -$ npm i -D prettier prettier-plugin-packagejson -``` - -`overrides` is required because native json parser takes precedence over this plugin. +# prettier-plugin-packagejson -```json -{ - "semi": false, - "singleQuote": true, - "trailingComma": "all", +This is [Prettier] plugin to make the keys of package.json well sorted order. - "overrides": [ - { - "files": "package.json", - "options": { - "parser": "package-json" - } - } - ] -} -``` +Using [sort-package-json]. -## Rules +[Prettier]: https://github.com/prettier/prettier +[sort-package-json]: https://github.com/keithamus/sort-package-json -- Primary (ordered to top) - 1. name - 2. description - 3. version - 4. author - 5. license -- Others - - Sorted alphabetically -- Sort children - - dependencies - - devDependencies - - peerDependencies - - optionalDependencies - - keywords +## Installation -ToDo: Add more tweaks +```bash +$ npm i -D prettier prettier-plugin-packagejson +``` diff --git a/lib/index.js b/lib/index.js index 6aa961e..d30ae7c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,16 +1,15 @@ const { parsers } = require('prettier/parser-babylon') +const sortPackageJson = require('sort-package-json') const parser = parsers['json-stringify'] -const printer = require('./printer') exports.parsers = { - 'package-json': { + 'json-stringify': { ...parser, - astFormat: 'package-json', - }, -} - -exports.printers = { - 'package-json': { - ...printer, + preprocess(text, options) { + if (parser.preprocess) { + text = parser.preprocess(text, options) + } + return sortPackageJson(text) + }, }, } diff --git a/lib/printer.js b/lib/printer.js deleted file mode 100644 index 7475b69..0000000 --- a/lib/printer.js +++ /dev/null @@ -1,90 +0,0 @@ -const prettier = require('prettier') -const { concat, hardline, indent, join } = prettier.doc.builders - -const primary = ['name', 'description', 'version', 'author', 'license'] -const objects = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] -const arrays = ['keywords'] - -function sortProperties(ast, isRoot) { - const props = {} - ast.properties.forEach(p => { - props[p.key.value] = p - }) - - const keys = Object.keys(props).sort() - const sortedKeys = isRoot - ? [...primary.filter(k => keys.includes(k)), ...keys.filter(k => !primary.includes(k))] - : keys - - ast.properties = sortedKeys.map(k => props[k]) - - objects - .map(k => props[k]) - .filter(Boolean) - .forEach(p => sortProperties(p.value)) - - arrays - .map(k => props[k]) - .filter(Boolean) - .forEach(p => sortElements(p.value)) -} - -function sortElements(ast) { - ast.elements.sort((a, b) => (a.value < b.value ? -1 : b.value < a.value)) -} - -exports.preprocess = ast => { - sortProperties(ast, true) - - return { - type: 'JsonRoot', - ast, - } -} - -exports.print = (path, options, print) => { - const node = path.getValue() - - switch (node.type) { - case 'JsonRoot': - return concat([path.call(print, 'ast'), hardline]) - - case 'ObjectProperty': - return concat([path.call(print, 'key'), ': ', path.call(print, 'value')]) - - case 'ObjectExpression': - return node.properties.length === 0 - ? '{}' - : concat([ - '{', - indent(concat([hardline, join(concat([',', hardline]), path.map(print, 'properties'))])), - hardline, - '}', - ]) - - case 'ArrayExpression': - return node.elements.length === 0 - ? '[]' - : concat([ - '[', - indent(concat([hardline, join(concat([',', hardline]), path.map(print, 'elements'))])), - hardline, - ']', - ]) - - case 'UnaryExpression': - return concat([node.operator === '+' ? '' : node.operator, path.call(print, 'argument')]) - - case 'NullLiteral': - case 'BooleanLiteral': - case 'StringLiteral': - case 'NumericLiteral': - return JSON.stringify(node.value) - - case 'Identifier': - return JSON.stringify(node.name) - - default: - throw new Error('unknown type: ' + JSON.stringify(node.type)) - } -} diff --git a/package-lock.json b/package-lock.json index f3435fe..9d23ff2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,10 +4,30 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "detect-indent": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-5.0.0.tgz", + "integrity": "sha1-OHHMCmoALow+Wzz38zYmRnXwa50=" + }, "prettier": { "version": "1.18.2", "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz", - "integrity": "sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==" + "integrity": "sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==", + "dev": true + }, + "sort-object-keys": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/sort-object-keys/-/sort-object-keys-1.1.2.tgz", + "integrity": "sha1-06bEjcKsl+a8lDZ2luA/bQnTeVI=" + }, + "sort-package-json": { + "version": "1.22.1", + "resolved": "https://registry.npmjs.org/sort-package-json/-/sort-package-json-1.22.1.tgz", + "integrity": "sha512-uVINQraFQvnlzNHFnQOT4MYy0qonIEzKwhrI2yrTiQjNo5QF4h3ffrnCk7a95QAwoK/RdkO/w8W9tJIcaOWC7g==", + "requires": { + "detect-indent": "^5.0.0", + "sort-object-keys": "^1.1.2" + } } } } diff --git a/package.json b/package.json index 011aa2a..d8a2215 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,39 @@ { "name": "prettier-plugin-packagejson", - "description": "Prettier package.json plugin to make the order of properties nice.", "version": "0.0.0-development", - "license": "MIT", - "dependencies": { - "prettier": "1.18.2" - }, - "files": [ - "lib" - ], + "description": "Prettier package.json plugin to make the order of properties nice.", "keywords": [ "package.json", "plugin", "prettier" ], + "homepage": "https://github.com/matzkoh/prettier-plugin-packagejson#readme", + "bugs": { + "url": "https://github.com/matzkoh/prettier-plugin-packagejson/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/matzkoh/prettier-plugin-packagejson.git" + }, + "license": "MIT", + "files": [ + "lib" + ], "main": "lib", + "directories": { + "lib": "lib", + "test": "test" + }, "scripts": { "test": "node test" + }, + "dependencies": { + "sort-package-json": "1.22.1" + }, + "devDependencies": { + "prettier": "1.18.2" + }, + "peerDependencies": { + "prettier": ">= 1.13" } } diff --git a/prettier.config.js b/prettier.config.js index 9b6ae86..ef1ef16 100644 --- a/prettier.config.js +++ b/prettier.config.js @@ -4,12 +4,4 @@ module.exports = { singleQuote: true, trailingComma: 'all', plugins: ['.'], - overrides: [ - { - files: 'package.json', - options: { - parser: 'package-json', - }, - }, - ], } diff --git a/test/index.js b/test/index.js index 13b0d0c..3aaecd5 100644 --- a/test/index.js +++ b/test/index.js @@ -1,41 +1,66 @@ const prettier = require('prettier') -const code = `{ +const input = `{ "license": "", - "author": "", "version": "", + "devDependencies": { + "B": "*", + "A": "*" + }, "description": "", - "name": "", - "keywords": [ - "C", - "B", - "A", - ], "dependencies": { "B": "*", "A": "*" }, - "devDependencies": { + "scripts": { + "test": "", + "posttest": "", + "start": "", + "pretest": "", + "build": "", + "postbuild": "", + "prebuild": "", + "lint": "" + }, + "peerDependencies": { "B": "*", "A": "*" }, + "author": "", + "name": "", "optionalDependencies": { "B": "*", "A": "*" }, - "peerDependencies": { - "B": "*", - "A": "*" - } + "keywords": [ + "C", + "B", + "A" + ] } ` const expected = `{ "name": "", - "description": "", "version": "", - "author": "", + "description": "", + "keywords": [ + "A", + "B", + "C" + ], "license": "", + "author": "", + "scripts": { + "build": "", + "lint": "", + "postbuild": "", + "prebuild": "", + "start": "", + "pretest": "", + "test": "", + "posttest": "" + }, "dependencies": { "A": "*", "B": "*" @@ -44,26 +69,21 @@ const expected = `{ "A": "*", "B": "*" }, - "keywords": [ - "A", - "B", - "C" - ], - "optionalDependencies": { + "peerDependencies": { "A": "*", "B": "*" }, - "peerDependencies": { + "optionalDependencies": { "A": "*", "B": "*" } } ` -const formatted = prettier.format(code, { - parser: 'package-json', +const output = prettier.format(input, { + filepath: 'package.json', plugins: ['.'], }) -console.assert(formatted === expected) +console.assert(output === expected) console.log('\033[32mpassed!\033[39m')