From c60b87172ed55e8105060ec46feffbaee22096d7 Mon Sep 17 00:00:00 2001 From: Nelson Martell Date: Sun, 22 Oct 2023 19:23:20 -0500 Subject: [PATCH 1/3] feat(presets): add multiline --- README.md | 20 +++++- package-lock.json | 133 +++++++++++++++++++++++++++++++++++++++ package.json | 6 +- src/presets/index.ts | 9 ++- src/presets/multiline.ts | 47 ++++++++++++++ 5 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 src/presets/multiline.ts diff --git a/README.md b/README.md index 9777cc3..7de42b9 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Depending on how are you creating your own config file, you may choose to instal - `devDependencies` (`-D`, `--save-dev`). - `peerDependencies` (`--save-peer`). -- `dependecies`. +- `dependencies`. @@ -90,6 +90,18 @@ Compatibility with Shell Script for Bash or sh. npm add -D prettier-plugin-sh ``` +### Multiline preset (`sh`) + +Multiline elements for some languages. + +#### Required plugins: + +- [prettier-plugin-multiline-arrays](https://github.com/electrovir/prettier-plugin-multiline-arrays) + +```sh +npm add -D prettier-plugin-multiline-arrays +``` + ## Usage ### Simple usage @@ -150,7 +162,11 @@ import { getMergedOverrideFor, } from '@elegantech/prettier-multi-config/presets'; -const selectedPresets = ['base', 'php', 'sh']; +const selectedPresets = [ + 'base', + 'php', + 'sh', +]; /** @type {import("prettier").Config} */ const config = { diff --git a/package-lock.json b/package-lock.json index 24db368..7ea70bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,6 +32,7 @@ "is-ci": "^3.0.1", "lint-staged": "^15.0.0", "prettier": "^3.0.3", + "prettier-plugin-multiline-arrays": "^3.0.0", "prettier-plugin-organize-imports": "^3.2.3", "prettier-plugin-pkg": "^0.18.0", "prettier-plugin-sh": "^0.13.1", @@ -46,6 +47,7 @@ }, "optionalDependencies": { "@prettier/plugin-php": ">= 0.20.1", + "prettier-plugin-multiline-arrays": "^3.0.0", "prettier-plugin-organize-imports": "^3.2.3", "prettier-plugin-pkg": ">= 0.18.0", "prettier-plugin-sh": ">= 0.13.1" @@ -93,6 +95,28 @@ "node": ">=6.0.0" } }, + "node_modules/@augment-vir/common": { + "version": "21.3.6", + "resolved": "https://registry.npmjs.org/@augment-vir/common/-/common-21.3.6.tgz", + "integrity": "sha512-4mXYHGBrLk2M7AVjXxT5qp+gCbGS77/xqRZLZDlZiTxRdrjGcRfHVWUOYjdu2GC8aqQ34qbtiSNSnl+fCY3byA==", + "dev": true, + "dependencies": { + "browser-or-node": "^2.1.1", + "type-fest": "^4.4.0" + } + }, + "node_modules/@augment-vir/common/node_modules/type-fest": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-4.5.0.tgz", + "integrity": "sha512-diLQivFzddJl4ylL3jxSkEc39Tpw7o1QeEHIPxVwryDK2lpB7Nqhzhuo6v5/Ls08Z0yPSAhsyAWlv1/H0ciNmw==", + "dev": true, + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@babel/code-frame": { "version": "7.22.13", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.13.tgz", @@ -2822,6 +2846,12 @@ "node": ">=8" } }, + "node_modules/browser-or-node": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/browser-or-node/-/browser-or-node-2.1.1.tgz", + "integrity": "sha512-8CVjaLJGuSKMVTxJ2DpBl5XnlNDiT4cQFeuCJJrvJmts9YrTZDizTX7PjC2s6W4x+MBGZeEY6dGMrF04/6Hgqg==", + "dev": true + }, "node_modules/browserslist": { "version": "4.21.10", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.10.tgz", @@ -10686,6 +10716,19 @@ "resolved": "examples/shareable-typescript", "link": true }, + "node_modules/prettier-plugin-multiline-arrays": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/prettier-plugin-multiline-arrays/-/prettier-plugin-multiline-arrays-3.0.0.tgz", + "integrity": "sha512-qEfctOpCfnTztyLRoVkKvYpOLeTiG1Sq29Guk9zMs5YeZeoRZXO6JDdWj9OJyswJHmIrOEuPCmpdS7yxkyIatw==", + "dev": true, + "dependencies": { + "@augment-vir/common": "^21.3.6", + "proxy-vir": "^0.0.1" + }, + "peerDependencies": { + "prettier": "^3.0.0" + } + }, "node_modules/prettier-plugin-organize-imports": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/prettier-plugin-organize-imports/-/prettier-plugin-organize-imports-3.2.3.tgz", @@ -10790,6 +10833,36 @@ "integrity": "sha512-vtK/94akxsTMhe0/cbfpR+syPuszcuwhqVjJq26CuNDgFGj682oRBXOP5MJpv2r7JtE8MsiepGIqvvOTBwn2vA==", "dev": true }, + "node_modules/proxy-vir": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/proxy-vir/-/proxy-vir-0.0.1.tgz", + "integrity": "sha512-K3sv2I9kigOqt17hMhvWjSiwx90hpxxD3rN+meVQhrGKoxhVbt82ulkm3i1KlenVIMS1KFskt/vltMlGHVQrMw==", + "dev": true, + "dependencies": { + "@augment-vir/common": "^13.3.0" + } + }, + "node_modules/proxy-vir/node_modules/@augment-vir/common": { + "version": "13.5.0", + "resolved": "https://registry.npmjs.org/@augment-vir/common/-/common-13.5.0.tgz", + "integrity": "sha512-kOnAOShZTaOJ8glYyi2K3hfiYV5zQyjpfc4jmHm5lySAFNMpOjKA7pZxpl8n3RCIHAt+nFP64vuxtSI6jSWOZw==", + "dev": true, + "dependencies": { + "type-fest": "^3.8.0" + } + }, + "node_modules/proxy-vir/node_modules/type-fest": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-3.13.1.tgz", + "integrity": "sha512-tLq3bSNx+xSpwvAJnzrK0Ep5CLNWjvFTOp71URMaAEWBfRb9nnJiBoUe0tF8bI4ZFO3omgBR6NvnbzVUT3Ly4g==", + "dev": true, + "engines": { + "node": ">=14.16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", @@ -13408,6 +13481,24 @@ "@jridgewell/trace-mapping": "^0.3.9" } }, + "@augment-vir/common": { + "version": "21.3.6", + "resolved": "https://registry.npmjs.org/@augment-vir/common/-/common-21.3.6.tgz", + "integrity": "sha512-4mXYHGBrLk2M7AVjXxT5qp+gCbGS77/xqRZLZDlZiTxRdrjGcRfHVWUOYjdu2GC8aqQ34qbtiSNSnl+fCY3byA==", + "dev": true, + "requires": { + "browser-or-node": "^2.1.1", + "type-fest": "^4.4.0" + }, + "dependencies": { + "type-fest": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-4.5.0.tgz", + "integrity": "sha512-diLQivFzddJl4ylL3jxSkEc39Tpw7o1QeEHIPxVwryDK2lpB7Nqhzhuo6v5/Ls08Z0yPSAhsyAWlv1/H0ciNmw==", + "dev": true + } + } + }, "@babel/code-frame": { "version": "7.22.13", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.13.tgz", @@ -15260,6 +15351,12 @@ "fill-range": "^7.0.1" } }, + "browser-or-node": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/browser-or-node/-/browser-or-node-2.1.1.tgz", + "integrity": "sha512-8CVjaLJGuSKMVTxJ2DpBl5XnlNDiT4cQFeuCJJrvJmts9YrTZDizTX7PjC2s6W4x+MBGZeEY6dGMrF04/6Hgqg==", + "dev": true + }, "browserslist": { "version": "4.21.10", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.10.tgz", @@ -20656,6 +20753,16 @@ "unbuild": "*" } }, + "prettier-plugin-multiline-arrays": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/prettier-plugin-multiline-arrays/-/prettier-plugin-multiline-arrays-3.0.0.tgz", + "integrity": "sha512-qEfctOpCfnTztyLRoVkKvYpOLeTiG1Sq29Guk9zMs5YeZeoRZXO6JDdWj9OJyswJHmIrOEuPCmpdS7yxkyIatw==", + "dev": true, + "requires": { + "@augment-vir/common": "^21.3.6", + "proxy-vir": "^0.0.1" + } + }, "prettier-plugin-organize-imports": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/prettier-plugin-organize-imports/-/prettier-plugin-organize-imports-3.2.3.tgz", @@ -20717,6 +20824,32 @@ "integrity": "sha512-vtK/94akxsTMhe0/cbfpR+syPuszcuwhqVjJq26CuNDgFGj682oRBXOP5MJpv2r7JtE8MsiepGIqvvOTBwn2vA==", "dev": true }, + "proxy-vir": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/proxy-vir/-/proxy-vir-0.0.1.tgz", + "integrity": "sha512-K3sv2I9kigOqt17hMhvWjSiwx90hpxxD3rN+meVQhrGKoxhVbt82ulkm3i1KlenVIMS1KFskt/vltMlGHVQrMw==", + "dev": true, + "requires": { + "@augment-vir/common": "^13.3.0" + }, + "dependencies": { + "@augment-vir/common": { + "version": "13.5.0", + "resolved": "https://registry.npmjs.org/@augment-vir/common/-/common-13.5.0.tgz", + "integrity": "sha512-kOnAOShZTaOJ8glYyi2K3hfiYV5zQyjpfc4jmHm5lySAFNMpOjKA7pZxpl8n3RCIHAt+nFP64vuxtSI6jSWOZw==", + "dev": true, + "requires": { + "type-fest": "^3.8.0" + } + }, + "type-fest": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-3.13.1.tgz", + "integrity": "sha512-tLq3bSNx+xSpwvAJnzrK0Ep5CLNWjvFTOp71URMaAEWBfRb9nnJiBoUe0tF8bI4ZFO3omgBR6NvnbzVUT3Ly4g==", + "dev": true + } + } + }, "punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", diff --git a/package.json b/package.json index 97286ae..cd51fa1 100644 --- a/package.json +++ b/package.json @@ -50,17 +50,17 @@ ], "scripts": { "build": "unbuild", - "build:internal": "unbuild internal", "build:watch": "watch 'npm run build' src", "fix:eslint": "eslint . --fix", "fix:style": "prettier --write .", + "internal:build": "unbuild internal", "internal:promote-prettier": "node scripts/promote-prettier-config.mjs", "lint:all": "concurrently -t --success=all --timings \"npm:lint:*(!all)\"", "lint:eslint": "eslint .", "lint:prettier-config": "npx prettier --check --no-error-on-unmatched-pattern --log-level=silent test", "lint:style": "prettier --check .", "lint:ts": "tsc --noEmit -p tsconfig.main.json && tsc --noEmit -p tsconfig.configs.json && tsc --noEmit -p tsconfig.internal.json && tsc --noEmit -p tsconfig.test.json", - "postbuild:internal": "npm run internal:promote-prettier", + "postinternal:build": "npm run internal:promote-prettier", "postinternal:promote-prettier": "prettier --write ./.prettierrc.json", "prepack": "npm run build", "prepare": "is-ci || husky install", @@ -76,6 +76,7 @@ }, "optionalDependencies": { "@prettier/plugin-php": ">= 0.20.1", + "prettier-plugin-multiline-arrays": "^3.0.0", "prettier-plugin-organize-imports": "^3.2.3", "prettier-plugin-pkg": ">= 0.18.0", "prettier-plugin-sh": ">= 0.13.1" @@ -98,6 +99,7 @@ "is-ci": "^3.0.1", "lint-staged": "^15.0.0", "prettier": "^3.0.3", + "prettier-plugin-multiline-arrays": "^3.0.0", "prettier-plugin-organize-imports": "^3.2.3", "prettier-plugin-pkg": "^0.18.0", "prettier-plugin-sh": "^0.13.1", diff --git a/src/presets/index.ts b/src/presets/index.ts index c8274f5..07c05a4 100644 --- a/src/presets/index.ts +++ b/src/presets/index.ts @@ -2,13 +2,19 @@ import { IOptionsWithoutPlugins, IPrettierOverride, PresetPlugins } from '~/cont import { IMergeOverrideMultipleOptions, isNotEmpty, mergeOverrideMultiple } from '~/helpers'; import { preset as base } from './base'; +import { preset as multiline } from './multiline'; import { preset as php } from './php'; import { preset as sh } from './sh'; /** * List of available presets. */ -export const AVAILABLE_PRESETS = ['base', 'php', 'sh'] as const; +export const AVAILABLE_PRESETS = [ + 'base', + 'php', + 'sh', + 'multiline', +] as const; export type PresetName = (typeof AVAILABLE_PRESETS)[number]; @@ -31,6 +37,7 @@ export const PRESETS_MAP = { base, php, sh, + multiline, } as const; export type PresetMap = typeof PRESETS_MAP; diff --git a/src/presets/multiline.ts b/src/presets/multiline.ts new file mode 100644 index 0000000..79d87ed --- /dev/null +++ b/src/presets/multiline.ts @@ -0,0 +1,47 @@ +import { IOptionsWithoutPlugins, IPrettierPreset } from '~/contracts'; + +/** + * @link https://github.com/electrovir/prettier-plugin-multiline-arrays?tab=readme-ov-file#options + */ +export interface IMultilineOptions extends IOptionsWithoutPlugins { + /** + * This should be set to a single number which controls when arrays wrap. + * If an array has more elements than the number specified here, it will be forced to wrap. + * This option defaults to -1, which indicates that no automatic wrapping will take place. + * + * @default -1 + */ + multilineArraysWrapThreshold?: number; + + /** + * This should be set to a string which contains a space separated list of numbers. These numbers allow + * fine grained control over how many elements appear in each line. The pattern will repeat if an array + * has more elements than the pattern. + * + * @default '1' + */ + multilineArraysLinePattern?: string; +} + +// TODO: Include multiline array plugin when compatible with Prettier 3 +// (https://github.com/electrovir/prettier-plugin-multiline-arrays/issues/26) + +/** + * Preset for multiline elements. + * + * @link https://github.com/electrovir/prettier-plugin-multiline-arrays + */ +export const preset: IPrettierPreset = { + globalOptions: { + multilineArraysWrapThreshold: 1, + }, + + plugins: [ + // + 'prettier-plugin-multiline-arrays', + ], + + overrideConfigs: [ + // TODO: Add configs for language + ], +}; From 4e91e439fcbc34becc1182d6049b5617e3bcb1e5 Mon Sep 17 00:00:00 2001 From: Nelson Martell Date: Sun, 22 Oct 2023 19:25:41 -0500 Subject: [PATCH 2/3] chore: update prettier config --- .prettierrc.json | 12 +++++++++--- internal/configs/prettierrc.ts | 13 +++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index b9dce1e..8955d82 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -8,21 +8,27 @@ "printWidth": 120, "endOfLine": "lf", "organizeImportsSkipDestructiveCodeActions": true, + "multilineArraysWrapThreshold": 1, "plugins": [ "prettier-plugin-pkg", "prettier-plugin-organize-imports", - "prettier-plugin-sh" + "prettier-plugin-sh", + "prettier-plugin-multiline-arrays" ], "overrides": [ { - "files": ["*.sh"], + "files": [ + "*.sh" + ], "options": { "indent": 4, "tabWidth": 4 } }, { - "files": [".prettierrc.json"], + "files": [ + ".prettierrc.json" + ], "options": { "printWidth": 80 } diff --git a/internal/configs/prettierrc.ts b/internal/configs/prettierrc.ts index 407e829..ce07795 100644 --- a/internal/configs/prettierrc.ts +++ b/internal/configs/prettierrc.ts @@ -6,13 +6,22 @@ import { getMergedGlobalOptionsFor, getMergedOverrideFor, getPluginsFor } from ' * The default config to be used as default. */ const config: Config = { - ...getMergedGlobalOptionsFor(['base', 'sh']), + ...getMergedGlobalOptionsFor([ + 'base', + 'multiline', + 'sh', + ]), - plugins: getPluginsFor(['base', 'sh']), + plugins: getPluginsFor([ + 'base', + 'sh', + 'multiline', + ]), overrides: [ // ...getMergedOverrideFor('base'), + ...getMergedOverrideFor('multiline'), ...getMergedOverrideFor('sh'), { files: ['.prettierrc.json'], From 4769e68ce2e74c046c3bed2d6a3c99df8b44fb9d Mon Sep 17 00:00:00 2001 From: Nelson Martell Date: Sun, 22 Oct 2023 19:34:52 -0500 Subject: [PATCH 3/3] style: autofix style for new prettier setup --- .eslintrc.cjs | 47 ++++++++++++++++++---- .lintstagedrc.mjs | 10 ++++- examples/shareable-typescript/src/index.ts | 6 ++- jsconfig.json | 5 ++- src/helpers/is-not-empty.spec.ts | 2 + src/helpers/merge-override.ts | 10 ++++- tsconfig.base.json | 7 +++- tsconfig.configs.json | 10 ++++- tsconfig.internal.json | 11 ++++- tsconfig.main.json | 6 ++- tsconfig.test.json | 10 ++++- 11 files changed, 103 insertions(+), 21 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 86879bf..e248ba5 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -88,13 +88,23 @@ const defaultTypescriptRules = { selector: 'variable', types: ['boolean'], format: ['PascalCase'], - prefix: ['is', 'should', 'has', 'can', 'did', 'will'], + prefix: [ + 'is', + 'should', + 'has', + 'can', + 'did', + 'will', + ], }, { // Enforce that all variables are either in camelCase or UPPER_CASE selector: 'variable', - format: ['camelCase', 'UPPER_CASE'], + format: [ + 'camelCase', + 'UPPER_CASE', + ], }, { // Ignore destructured names @@ -126,11 +136,21 @@ const defaultTypescriptRules = { module.exports = { root: true, - ignorePatterns: ['node_modules', 'build', 'dist', '!.*', 'examples'], + ignorePatterns: [ + 'node_modules', + 'build', + 'dist', + '!.*', + 'examples', + ], overrides: [ { - files: ['*.js', '*.mjs', '*.cjs'], + files: [ + '*.js', + '*.mjs', + '*.cjs', + ], parserOptions: { ecmaVersion: 'latest', }, @@ -151,12 +171,21 @@ module.exports = { rules: { ...defaultRules, // Helps import the correct file extension - 'import/extensions': ['error', 'always', { ignorePackages: true }], + 'import/extensions': [ + 'error', + 'always', + { ignorePackages: true }, + ], 'node/no-unsupported-features/es-syntax': ['off'], }, }, { - files: ['*.ts', '*.mts', '*.cts', '*.tsx'], + files: [ + '*.ts', + '*.mts', + '*.cts', + '*.tsx', + ], extends: [ 'eslint:recommended', 'plugin:unicorn/recommended', @@ -164,7 +193,11 @@ module.exports = { 'prettier', ], parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint', 'import', 'unicorn'], + plugins: [ + '@typescript-eslint', + 'import', + 'unicorn', + ], parserOptions: { EXPERIMENTAL_useProjectService: true, // project: true, diff --git a/.lintstagedrc.mjs b/.lintstagedrc.mjs index d82b1bf..44af66b 100644 --- a/.lintstagedrc.mjs +++ b/.lintstagedrc.mjs @@ -8,10 +8,16 @@ const prettierCmd = 'prettier --ignore-unknown --write'; export default { // Javascript sources - '*.{js,cjs,mjs}': [eslintCmd, prettierCmd], + '*.{js,cjs,mjs}': [ + eslintCmd, + prettierCmd, + ], // Typescript sources - '*.{ts,cts,mts}': [eslintCmd, prettierCmd], + '*.{ts,cts,mts}': [ + eslintCmd, + prettierCmd, + ], // Other files '*!(.{js,cjs,mjs,ts,cts,mts})': [prettierCmd], diff --git a/examples/shareable-typescript/src/index.ts b/examples/shareable-typescript/src/index.ts index 1ebe6d1..8e69f7a 100644 --- a/examples/shareable-typescript/src/index.ts +++ b/examples/shareable-typescript/src/index.ts @@ -7,7 +7,11 @@ import { import { Config } from 'prettier'; -const selectedPresets: PresetName[] = ['base', 'php', 'sh']; +const selectedPresets: PresetName[] = [ + 'base', + 'php', + 'sh', +]; const config: Config = { // Add global configurations for selected presets diff --git a/jsconfig.json b/jsconfig.json index 901e7ab..7595c24 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -5,5 +5,8 @@ "target": "ESNext", "strict": true }, - "exclude": ["node_modules", "**/node_modules/*"] + "exclude": [ + "node_modules", + "**/node_modules/*" + ] } diff --git a/src/helpers/is-not-empty.spec.ts b/src/helpers/is-not-empty.spec.ts index 4b7a664..05ea5b3 100644 --- a/src/helpers/is-not-empty.spec.ts +++ b/src/helpers/is-not-empty.spec.ts @@ -2,6 +2,8 @@ import { describe, expect, test } from 'vitest'; import { isNotEmpty } from './is-not-empty'; +// prettier-multiline-arrays-set-threshold: 3 + describe('isNotEmpty(💬)', () => { test.each([ ['number', 0], diff --git a/src/helpers/merge-override.ts b/src/helpers/merge-override.ts index 4e1eaa8..82fd79e 100644 --- a/src/helpers/merge-override.ts +++ b/src/helpers/merge-override.ts @@ -44,7 +44,10 @@ export const mergeOverride =