Skip to content

Commit

Permalink
fix(prettier-plugin-pkg): support yarn(flat) and vscode out of box
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Sep 26, 2019
1 parent 00205c0 commit 47d3557
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ script:
- yarn build
- yarn lint
- yarn test
# - yarn type-coverage
- yarn type-coverage

after_success:
- yarn global add codecov codacy-coverage
Expand Down
1 change: 1 addition & 0 deletions codechecks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ checks:
- path: 'packages/*lib/esm.js'
- name: type-coverage-watcher
options:
atLeast: 100
detail: true
ignoreCatch: true
strict: true
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
"devDependencies": {
"@1stg/babel-preset": "^0.7.0",
"@1stg/commitlint-config": "^0.1.0",
"@1stg/eslint-config": "^0.12.12",
"@1stg/eslint-config": "^0.12.13",
"@1stg/husky-config": "^0.3.0",
"@1stg/lint-staged": "^0.8.0",
"@1stg/prettier-config": "^0.3.1",
"@1stg/remark-config": "^0.2.1",
"@1stg/rollup-config": "^0.10.4",
"@1stg/prettier-config": "^0.3.2",
"@1stg/remark-config": "^0.2.2",
"@1stg/rollup-config": "^0.10.5",
"@1stg/tsconfig": "^0.6.0",
"@1stg/tslint-config": "^0.4.0",
"@1stg/tslint-config": "^0.4.1",
"@babel/core": "^7.6.2",
"@babel/types": "^7.6.1",
"@commitlint/cli": "^8.2.0",
Expand All @@ -50,7 +50,6 @@
"rollup": "^1.21.4",
"ts-jest": "^24.1.0",
"tslint": "^5.20.0",
"tslint-config-eslint": "^0.1.0",
"type-coverage": "^2.3.0",
"typescript": "^3.6.3"
},
Expand Down
26 changes: 22 additions & 4 deletions packages/pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ An opinionated `package.json` formatter plugin for [Prettier](https://prettier.i

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing, taking various rules into account.

This plugin adds support for `package.json` files used within NPM modules.
This plugin adds support for `package.json` files used within NPM modules, [npm][], [yarn][] and [VSCode][] is supportted out of box.

## Requirements

Expand All @@ -33,7 +33,7 @@ Once installed, [Prettier plugins](https://prettier.io/docs/en/plugins.html) sho
npx prettier --write package.json

# yarn
yarn prettier --write package.jso
yarn prettier --write package.json
```

## Rules
Expand Down Expand Up @@ -61,13 +61,18 @@ Top-level keys are sorted according to a style commonly seen in the packages of
// meta
"name",
"version",
"flat",
"displayName",
"description",
"categories",
"repository",
"homepage",
"bugs",
"author",
"publisher",
"contributors",
"license",
"preview",
"private",
"workspaces",

Expand All @@ -83,7 +88,6 @@ Top-level keys are sorted according to a style commonly seen in the packages of
"module",
"esnext",
"es2015",
"esm",
"fesm5",
"fesm2015",
"browser",
Expand All @@ -108,7 +112,17 @@ Top-level keys are sorted according to a style commonly seen in the packages of
"devDependencies",
"publishConfig",
"resolutions",
"sideEffects"
"sideEffects",

// vscode spec
"icon",
"galleryBanner",
"activationEvents",
"contributes",
"markdown",
"qna",
"extensionPack",
"extensionDependencies"
]
```

Expand All @@ -124,3 +138,7 @@ Forthcoming rules include:
## Meta

[LICENSE (Mozilla Public License)](./LICENSE)

[npm]: https://docs.npmjs.com/files/package.json
[yarn]: https://yarnpkg.com/docs/package-j
[vscode]: https://code.visualstudio.com/api/references/extension-manifest
7 changes: 3 additions & 4 deletions packages/pkg/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Plugin } from 'prettier'
import { parsers } from 'prettier/parser-babylon'

import { engines } from './rules/engines'
import { files } from './rules/files'
import { scripts } from './rules/scripts'
import { object } from './rules/object'
import { sort } from './rules/sort'
import { ObjectExpression, ObjectProperty } from './types'

Expand All @@ -15,9 +14,9 @@ const {

const format = (properties: ObjectProperty[]) => {
let props = sort(properties)
props = engines(props)
props = object(props, 'engines')
props = object(props, 'scripts')
props = files(props)
props = scripts(props)
return props
}

Expand Down
17 changes: 0 additions & 17 deletions packages/pkg/src/rules/engines.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/pkg/src/rules/files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StringLiteral } from '@babel/types'

import { ObjectProperty, StringArrayExpression } from '../types'
import { sortStringArray } from '../utils'

const process = (props: ObjectProperty[]) => {
const filesIndex = props.findIndex(prop => prop.key.value === 'files')
Expand Down Expand Up @@ -28,7 +29,7 @@ const process = (props: ObjectProperty[]) => {

return true
})
.sort((a, b) => (a.value > b.value ? 1 : a.value < b.value ? -1 : 0))
.sort(sortStringArray)

if (readme) {
elements.push(readme)
Expand Down
16 changes: 16 additions & 0 deletions packages/pkg/src/rules/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ObjectExpression, ObjectProperty } from '../types'
import { sortObject } from '../utils'

export const process = (props: ObjectProperty[], key: string) => {
const keyIndex = props.findIndex(prop => prop.key.value === key)

if (keyIndex >= 0) {
const object = props[keyIndex]
const value = object.value as ObjectExpression
value.properties.sort(sortObject)
}

return props
}

export { process as object }
17 changes: 0 additions & 17 deletions packages/pkg/src/rules/scripts.ts

This file was deleted.

28 changes: 23 additions & 5 deletions packages/pkg/src/rules/sort.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { ObjectProperty } from '../types'
import { sortObject } from '../utils'

// reference: https://docs.npmjs.com/files/package.json#people-fields-author-contributors
/**
* reference:
* npm - https://docs.npmjs.com/files/package.json
* yarn - https://yarnpkg.com/docs/package-json
* vscode - https://code.visualstudio.com/api/references/extension-manifest
*/
const primary = [
// meta
'name',
'version',
'flat',
'displayName',
'description',
'categories',
'repository',
'homepage',
'bugs',
'author',
'publisher',
'contributors',
'license',
'preview',
'private',
'workspaces',

Expand All @@ -27,7 +38,6 @@ const primary = [
'module',
'esnext',
'es2015',
'esm',
'fesm5',
'fesm2015',
'browser',
Expand All @@ -53,6 +63,16 @@ const primary = [
'publishConfig',
'resolutions',
'sideEffects',

// vscode spec
'icon',
'galleryBanner',
'activationEvents',
'contributes',
'markdown',
'qna',
'extensionPack',
'extensionDependencies',
]

export const sort = (props: ObjectProperty[]) => {
Expand All @@ -70,9 +90,7 @@ export const sort = (props: ObjectProperty[]) => {
const bIndex = primary.indexOf(b.key.value)
return aIndex > bIndex ? 1 : aIndex < bIndex ? -1 : 0
})
others.sort((a, b) =>
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
)
others.sort(sortObject)

return known.concat(others)
}
41 changes: 16 additions & 25 deletions packages/pkg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,39 @@ import {
StringLiteral,
} from '@babel/types'

export interface StringMapperProperty extends _ObjectProperty {
export interface ObjectProperty extends _ObjectProperty {
key: {
value: string
}
value: StringMapperExpression
value: ArrayExpression | ObjectExpression
}

export interface ObjectExpression extends _ObjectExpression {
properties: ObjectProperty[]
}

export interface StringMapperExpression extends _ObjectExpression {
properties: StringMapperProperty[]
export interface ArrayExpression extends _ArrayExpression {
elements: Array<ArrayExpression | ObjectExpression | StringLiteral>
}

export interface StringArrayProperty extends _ObjectProperty {
export interface StringMapProperty extends ObjectProperty {
key: {
value: string
}
value: StringArrayExpression
value: StringMapExpression
}

export interface StringArrayExpression extends _ArrayExpression {
elements: StringLiteral[]
export interface StringMapExpression extends ObjectExpression {
properties: StringMapProperty[]
}

export interface ObjectProperty extends _ObjectProperty {
export interface StringArrayProperty extends ObjectProperty {
key: {
value: string
}
value:
| ArrayExpression
| ObjectExpression
| StringArrayExpression
| StringMapperExpression
}

export interface ObjectExpression extends _ObjectExpression {
properties: ObjectProperty[]
value: StringArrayExpression
}

export interface ArrayExpression extends _ArrayExpression {
elements: Array<
| ArrayExpression
| ObjectExpression
| StringArrayExpression
| StringMapperExpression
>
export interface StringArrayExpression extends ArrayExpression {
elements: StringLiteral[]
}
9 changes: 9 additions & 0 deletions packages/pkg/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StringLiteral } from '@babel/types'

import { ObjectProperty } from './types'

export const sortObject = (a: ObjectProperty, b: ObjectProperty) =>
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0

export const sortStringArray = (a: StringLiteral, b: StringLiteral) =>
a.value > b.value ? 1 : a.value < b.value ? -1 : 0
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["@1stg/tslint-config", "tslint-config-eslint"],
"extends": "@1stg/tslint-config/eslint",
"linterOptions": {
"exclude": ["node_modules/**", "**/lib/**"]
}
Expand Down

0 comments on commit 47d3557

Please sign in to comment.