Skip to content

Commit

Permalink
optimize transform function using compose util (#18)
Browse files Browse the repository at this point in the history
* build: move to bunchee

* chore: add changeset

* feat: compose fns of parser

* chore: add changeset

* test: revert relative path

* chore: upgrade pnpm

* ci: upgrade action
  • Loading branch information
nnecec authored Jul 8, 2024
1 parent c5f21ee commit dfadab4
Show file tree
Hide file tree
Showing 14 changed files with 6,312 additions and 4,970 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-zebras-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'prettier-plugin-zh': patch
'core': patch
---

compose fns of parser
6 changes: 6 additions & 0 deletions .changeset/unlucky-years-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"prettier-plugin-zh": minor
"core": minor
---

use bunchee for bundle
15 changes: 7 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ jobs:

steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Use Node.js 16
uses: actions/setup-node@v1
with:
node-version: 16.x
- name: Setup pnpm
uses: pnpm/action-setup@v4

- name: Use pnpm 8
uses: pnpm/action-setup@v2
- name: Use Node.js LTS
uses: actions/setup-node@v4
with:
version: 8
node-version: latest
cache: 'pnpm'

- name: Install Dependencies
run: pnpm install --frozen-lockfile
Expand Down
14 changes: 6 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x
- name: Setup pnpm
uses: pnpm/action-setup@v4

- name: Setup pnpm 8.x
uses: pnpm/action-setup@v2
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
version: 8
node-version: latest

- name: Install Dependencies
run: pnpm install --frozen-lockfile
Expand Down
22 changes: 11 additions & 11 deletions apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
},
"dependencies": {
"next": "~13.4.7",
"nextra": "^2.13.2",
"nextra-theme-docs": "^2.13.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"nextra": "^2.13.4",
"nextra-theme-docs": "^2.13.4",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@next/eslint-plugin-next": "~13.4.7",
"@types/node": "^20.11.5",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"autoprefixer": "^10.4.17",
"postcss": "^8.4.33",
"tailwindcss": "^3.4.1",
"typescript": "^5.3.3"
"@types/node": "^20.14.9",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.39",
"tailwindcss": "^3.4.4",
"typescript": "^5.5.3"
}
}
6 changes: 0 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,4 @@ export default [
},
},
},
{
files: ['**/*.ts', '**/*.tsx'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
},
]
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
"version": "changeset version"
},
"dependencies": {
"@changesets/cli": "^2.27.1"
"@changesets/cli": "^2.27.7"
},
"devDependencies": {
"@next/eslint-plugin-next": "~13.4.7",
"@nnecec/eslint-config": "^0.8.10",
"@nnecec/prettier-config": "^0.4.0",
"@nnecec/tsconfig": "^0.2.2",
"@turbo/gen": "^1.11.3",
"eslint": "^8.56.0",
"prettier": "^3.2.4",
"@nnecec/eslint-config": "^0.10.0",
"@nnecec/prettier-config": "^0.4.2",
"@nnecec/tsconfig": "^0.2.3",
"@turbo/gen": "^2.0.6",
"eslint": "^9.6.0",
"prettier": "^3.3.2",
"prettier-plugin-zh": "workspace:*",
"turbo": "^1.11.3"
"turbo": "^2.0.6"
},
"packageManager": "pnpm@8.6.0"
"packageManager": "pnpm@9.5.0+sha512.140036830124618d624a2187b50d04289d5a087f326c9edfc0ccd733d76c4f52c3a313d4fc148794a2a9d81553016004e6742e8cf850670268a7387fc220c903"
}
18 changes: 8 additions & 10 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@
"author": "nnecec <[email protected]>",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./package.json": "./package.json"
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"main": "./dist/index.js",
"files": [
"./dist"
],
"scripts": {
"build": "tsup src/index.ts --format esm,cjs --dts",
"build": "bunchee",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
"test": "echo \"Error: no test specified\" && exit 0"
"dev": "bunchee -w"
},
"devDependencies": {
"@nnecec/tsconfig": "^0.2.2",
"tsup": "^8.0.1"
"@nnecec/tsconfig": "^0.2.3",
"bunchee": "^5.2.2"
},
"publishConfig": {
"access": "restricted"
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-zh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test": "echo \"Error: no test specified\" && exit 0"
},
"devDependencies": {
"eslint": "^8.56.0"
"eslint": "^9.6.0"
},
"peerDependencies": {
"eslint": ">=8.0.0"
Expand Down
23 changes: 11 additions & 12 deletions packages/prettier-plugin-zh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,29 @@
"author": "nnecec <[email protected]>",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./package.json": "./package.json"
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"main": "./dist/index.js",
"main": "./dist/index.cjs",
"files": [
"./dist"
],
"scripts": {
"build": "tsup src/index.ts --format esm,cjs --dts",
"build": "bunchee",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
"test": "echo \"Error: no test specified\" && exit 0"
"dev": "bunchee -w"
},
"dependencies": {
"prettier": "^3.2.4"
"prettier": "^3.3.2"
},
"devDependencies": {
"core": "workspace:*",
"tsup": "^8.0.1"
"bunchee": "^5.2.2",
"core": "workspace:*"
},
"peerDependencies": {
"prettier": ">=3.0.0"
"prettier": ">=3"
},
"publishConfig": {
"access": "public"
Expand Down
23 changes: 9 additions & 14 deletions packages/prettier-plugin-zh/src/transforms/transform-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@ import {
import type { MarkdownAST } from '../types'
import type { Transform } from './types'

import { traverseChildren } from '../utils'
import { compose, traverseChildren } from '../utils'

export const transformMarkdown: Transform<MarkdownAST> = (ast, options) => {
traverseChildren<MarkdownAST>(ast, ({ child, nextSibling, prevSibling }) => {
switch (child.type) {
case 'text': {
if (options.spaceAroundAlphabet === true) child.value = spaceAroundAlphabet(child.value)
if (options.spaceAroundNumber === true) child.value = spaceAroundNumber(child.value)

if (options.noDuplicatePunctuation === true) {
child.value = noDuplicatePunctuation(child.value)
}

if (options.noSpaceBetweenNumberUnit && options.noSpaceBetweenNumberUnit.length > 0) {
child.value = noSpaceBetweenNumberUnit(child.value, options.noSpaceBetweenNumberUnit)
}
if (options.noSpaceAroundFullwidth === true) {
child.value = noSpaceAroundFullwidth(child.value)
}
const fns = [
options.spaceAroundAlphabet === true && spaceAroundAlphabet,
options.spaceAroundNumber === true && spaceAroundNumber,
options.noDuplicatePunctuation === true && noDuplicatePunctuation,
options.noSpaceBetweenNumberUnit && options.noSpaceBetweenNumberUnit.length > 0 && noSpaceBetweenNumberUnit,
options.noSpaceAroundFullwidth === true && noSpaceAroundFullwidth,
].filter(fn => !!fn)
child.value = compose(...fns)(child.value, options.noSpaceBetweenNumberUnit)
break
}
case 'inlineCode': {
Expand Down
12 changes: 12 additions & 0 deletions packages/prettier-plugin-zh/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ export function traverseChildren<T extends WithChildren>(ast: T, fn: (context: T
traverseChildren(child, fn)
}
}

export function compose(...fns: ((...args: any[]) => any)[]) {
if (fns.length === 0) {
return (arg: any) => arg
}
if (fns.length === 1) {
return fns[0]!
}
return fns.reduce((a, b) => {
return (...args) => a(b(...args))
})
}
Loading

0 comments on commit dfadab4

Please sign in to comment.