Skip to content

Commit

Permalink
fix(prettier-plugin-pkg): better or stricter types support
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Sep 24, 2019
1 parent 8609047 commit f9328a3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
6 changes: 3 additions & 3 deletions packages/pkg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { engines } from './rules/engines'
import { files } from './rules/files'
import { scripts } from './rules/scripts'
import { sort } from './rules/sort'
import { StringMapperProperty, StringMapperPropertyValue } from './types'
import { ObjectExpression, ObjectProperty } from './types'

const PKG_REG = /[\\/]?package\.json$/

const {
json: { parse },
} = parsers

const format = (properties: StringMapperProperty[]) => {
const format = (properties: ObjectProperty[]) => {
let props = sort(properties)
props = engines(props)
props = files(props)
Expand All @@ -29,7 +29,7 @@ export default {
parse(...args) {
const [, , options] = args
const { filepath } = options
const ast: StringMapperPropertyValue = parse(...args)
const ast: ObjectExpression = parse(...args)

if (PKG_REG.test(filepath)) {
ast.properties = format(ast.properties)
Expand Down
16 changes: 5 additions & 11 deletions packages/pkg/src/rules/engines.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { StringMapperProperty } from '../types'
import { ObjectProperty, StringMapperExpression } from '../types'

const process = (props: StringMapperProperty[]) => {
const process = (props: ObjectProperty[]) => {
const enginesIndex = props.findIndex(prop => prop.key.value === 'engines')

if (enginesIndex >= 0) {
const [engines] = props.splice(enginesIndex, 1)
const { value } = engines
const { properties } = value

properties.sort((a, b) =>
const engines = props[enginesIndex]
const value = engines.value as StringMapperExpression
value.properties.sort((a, b) =>
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
)

value.properties = properties

props.splice(enginesIndex, 0, engines)
}

return props
Expand Down
10 changes: 4 additions & 6 deletions packages/pkg/src/rules/files.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { StringLiteral } from '@babel/types'

import { StringArrayProperty, StringMapperProperty } from '../types'
import { ObjectProperty, StringArrayExpression } from '../types'

const process = (props: StringMapperProperty[]) => {
const process = (props: ObjectProperty[]) => {
const filesIndex = props.findIndex(prop => prop.key.value === 'files')

if (filesIndex >= 0) {
const [filesNode] = props.splice(filesIndex, 1)

let readme: StringLiteral
let license: StringLiteral
// FIXME: should not use unknown casting
let { elements } = (filesNode.value as unknown) as StringArrayProperty

elements = elements
const elements = (filesNode.value as StringArrayExpression).elements
.filter(node => {
const value = node.value.toLowerCase()

Expand All @@ -40,7 +38,7 @@ const process = (props: StringMapperProperty[]) => {
elements.push(license)
}

;((filesNode.value as unknown) as StringArrayProperty).elements = elements
Object.assign(filesNode, { elements })

props.splice(filesIndex, 0, filesNode)
}
Expand Down
9 changes: 4 additions & 5 deletions packages/pkg/src/rules/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { StringMapperProperty } from '../types'
import { ObjectProperty, StringMapperExpression } from '../types'

const process = (props: StringMapperProperty[]) => {
const process = (props: ObjectProperty[]) => {
const scriptsIndex = props.findIndex(prop => prop.key.value === 'scripts')

if (scriptsIndex >= 0) {
const scripts = props[scriptsIndex]
scripts.value.properties.sort((a, b) =>
const value = scripts.value as StringMapperExpression
value.properties.sort((a, b) =>
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0,
)
// eslint-disable-next-line no-param-reassign
props[scriptsIndex] = scripts
}

return props
Expand Down
6 changes: 3 additions & 3 deletions packages/pkg/src/rules/sort.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StringMapperProperty } from '../types'
import { ObjectProperty } from '../types'

// reference: https://docs.npmjs.com/files/package.json#people-fields-author-contributors
const primary = [
Expand Down Expand Up @@ -55,8 +55,8 @@ const primary = [
'sideEffects',
]

export const sort = (props: StringMapperProperty[]) => {
const others: StringMapperProperty[] = []
export const sort = (props: ObjectProperty[]) => {
const others: ObjectProperty[] = []
const known = props.filter(prop => {
if (primary.includes(prop.key.value)) {
return true
Expand Down
43 changes: 38 additions & 5 deletions packages/pkg/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
import { ObjectExpression, ObjectProperty, StringLiteral } from '@babel/types'
import {
ArrayExpression as _ArrayExpression,
ObjectExpression as _ObjectExpression,
ObjectProperty as _ObjectProperty,
StringLiteral,
} from '@babel/types'

export interface StringMapperProperty extends ObjectProperty {
export interface StringMapperProperty extends _ObjectProperty {
key: {
value: string
}
value: StringMapperPropertyValue
value: StringMapperExpression
}

export interface StringMapperPropertyValue extends ObjectExpression {
export interface StringMapperExpression extends _ObjectExpression {
properties: StringMapperProperty[]
}

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

export interface StringArrayExpression extends _ArrayExpression {
elements: StringLiteral[]
}

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

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

export interface ArrayExpression extends _ArrayExpression {
elements: Array<
| ArrayExpression
| ObjectExpression
| StringArrayExpression
| StringMapperExpression
>
}

0 comments on commit f9328a3

Please sign in to comment.