Skip to content

Commit

Permalink
Merge pull request #28 from tthornton3-chwy/prettier-v3-support
Browse files Browse the repository at this point in the history
Prettier v3 support (v2)
  • Loading branch information
electrovir committed Oct 22, 2023
2 parents a80b328 + ace10ad commit 2571156
Show file tree
Hide file tree
Showing 15 changed files with 1,861 additions and 2,028 deletions.
2 changes: 1 addition & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const baseOptions = require('./.virmator/mocharc-base');
const baseOptions = require('./configs/mocha.config.js');

/** @type {import('mocha').MochaOptions} */
const mochaConfig = {
Expand Down
21 changes: 18 additions & 3 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
const {basePrettierConfig} = require('virmator/base-configs/base-prettierrc.js');

/**
* @typedef {import('prettier-plugin-multiline-arrays').MultilineArrayOptions} MultilineOptions
*
* @typedef {import('prettier').Options} PrettierOptions
* @type {PrettierOptions & MultilineOptions}
*/
const prettierConfig = {
...basePrettierConfig,
arrowParens: 'always',
bracketSameLine: false,
bracketSpacing: false,
endOfLine: 'lf',
htmlWhitespaceSensitivity: 'ignore',
jsonRecursiveSort: true,
printWidth: 100,
singleQuote: true,
tabWidth: 4,
trailingComma: 'all',
plugins: [
'prettier-plugin-toml',
'prettier-plugin-sort-json',
'prettier-plugin-packagejson',
'prettier-plugin-multiline-arrays',
'prettier-plugin-organize-imports',
'prettier-plugin-jsdoc',
],
};

module.exports = prettierConfig;
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Tested to be compatible with the following plugins. It is likely compatible with
4. this plugin must be placed here
5. `prettier-plugin-organize-imports`
6. `prettier-plugin-jsdoc`
7. `prettier-plugin-interpolated-html-tags`
7. ~~`prettier-plugin-interpolated-html-tags`~~ (needs to be updated to v3)

## Dev

Expand Down
3 changes: 3 additions & 0 deletions cspell.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ module.exports = {
...baseConfig.words,
'farmerpaul',
'Robinfr',
'espree',
'meriyah',
'nodenext',
],
};
3,634 changes: 1,704 additions & 1,930 deletions package-lock.json

Large diffs are not rendered by default.

41 changes: 20 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-multiline-arrays",
"version": "2.0.0",
"version": "3.0.0",
"description": "Prettier plugin to force all arrays to be multiline.",
"keywords": [
"array",
Expand Down Expand Up @@ -41,38 +41,37 @@
"test:types": "tsc --noEmit"
},
"dependencies": {
"@augment-vir/common": "^15.3.0",
"@augment-vir/common": "^15.6.1",
"proxy-vir": "^0.0.1"
},
"devDependencies": {
"@augment-vir/chai": "^15.3.0",
"@augment-vir/chai": "^15.6.1",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.5",
"@types/esprima": "^4.0.3",
"@types/mocha": "^10.0.1",
"@types/prettier": "^2.7.3",
"chai": "^4.3.7",
"@types/chai": "^4.3.8",
"@types/esprima": "^4.0.4",
"@types/mocha": "^10.0.2",
"@types/prettier": "^3.0.0",
"chai": "^4.3.10",
"cross-env": "^7.0.3",
"cspell": "^6.31.1",
"esbuild": "^0.18.8",
"cspell": "^7.3.8",
"esbuild": "^0.18.20",
"istanbul-smart-text-reporter": "^1.1.2",
"markdown-code-example-inserter": "^0.3.1",
"markdown-code-example-inserter": "^0.3.2",
"mocha": "^10.2.0",
"mocha-spec-reporter-with-file-names": "^0.0.3",
"npm-check-updates": "^16.10.13",
"prettier": "^2.8.8",
"prettier-plugin-interpolated-html-tags": "^0.0.4",
"prettier-plugin-jsdoc": "^0.4.2",
"npm-check-updates": "^16.14.6",
"prettier": "^3.0.0",
"prettier-plugin-jsdoc": "^1.1.1",
"prettier-plugin-multiline-arrays": "*",
"prettier-plugin-organize-imports": "^3.2.2",
"prettier-plugin-packagejson": "^2.4.3",
"prettier-plugin-sort-json": "^1.0.0",
"prettier-plugin-toml": "^0.3.1",
"prettier-plugin-organize-imports": "^3.2.3",
"prettier-plugin-packagejson": "^2.4.6",
"prettier-plugin-sort-json": "^3.1.0",
"prettier-plugin-toml": "^0.3.5",
"test-as-package": "^0.0.4",
"ts-node": "^10.9.1",
"typescript": "5.1.3",
"virmator": "^6.6.5"
"typescript": "^5.2.2",
"virmator": "^7.2.5"
},
"overrides": {
"semver": "^7.5.2"
Expand Down
4 changes: 4 additions & 0 deletions src/augments/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ export function isDocCommand(
): inputDoc is doc.builders.DocCommand {
return !!inputDoc && typeof inputDoc !== 'string' && !Array.isArray(inputDoc);
}

export function isDocArray(inputDoc: Doc | undefined | null): inputDoc is Doc[] {
return Array.isArray(inputDoc);
}
84 changes: 48 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import {
Printer,
RequiredOptions,
StringSupportOption,
SupportLanguage,
SupportOption,
getSupportInfo,
} from 'prettier';
import {parsers as babelParsers} from 'prettier/parser-babel';
import {parsers as tsParsers} from 'prettier/parser-typescript';
import {parsers as babelParsers} from 'prettier/plugins/babel';
import {MultilineArrayOptions, defaultMultilineArrayOptions, optionHelp} from './options';
import {wrapParser} from './preprocessing';
import {multilineArrayPrinter} from './printer/multiline-array-printer';
Expand All @@ -21,18 +19,7 @@ import {multilineArrayPrinter} from './printer/multiline-array-printer';
export * from './options';
export {pluginMarker} from './plugin-marker';

export const languages: SupportLanguage[] = getSupportInfo().languages.filter(({name}) =>
[
'JavaScript',
'TypeScript',
'JSON',
'JSON5',
'JSON with Comments',
'JSON.stringify',
].includes(name),
);

export const parsers: Record<string, Parser<any>> = mapObjectValues(
const parsers: Record<string, Parser<any>> = mapObjectValues(
{
typescript: tsParsers.typescript,
babel: babelParsers.babel,
Expand All @@ -41,42 +28,67 @@ export const parsers: Record<string, Parser<any>> = mapObjectValues(
json5: babelParsers.json5,
},
(languageName, parserEntry) => {
return wrapParser(parserEntry, languageName);
return wrapParser(parserEntry as any, languageName);
},
);

export const printers: Record<string, Printer<any>> = {
const printers: Record<string, Printer<any>> = {
estree: multilineArrayPrinter,
'estree-json': multilineArrayPrinter,
};

export const options: Record<keyof MultilineArrayOptions, SupportOption> = getObjectTypedKeys(
const options: Record<keyof MultilineArrayOptions, SupportOption> = getObjectTypedKeys(
defaultMultilineArrayOptions,
).reduce((accum, key) => {
const defaultValue = defaultMultilineArrayOptions[key];
const supportOption: StringSupportOption | IntSupportOption | BooleanSupportOption = {
name: key,
type: (typeof defaultValue === 'number' ? 'int' : typeof defaultValue) as
| 'string'
| 'boolean'
| 'int',
category: 'multilineArray',
since: '0.0.1',
default: defaultValue as any,
description: optionHelp[key],
};
accum[key] = supportOption;
return accum;
}, {} as Record<keyof MultilineArrayOptions, SupportOption>);
).reduce(
(accum, key) => {
const defaultValue = defaultMultilineArrayOptions[key];
const supportOption: StringSupportOption | IntSupportOption | BooleanSupportOption = {
name: key,
type: (typeof defaultValue === 'number' ? 'int' : typeof defaultValue) as
| 'string'
| 'boolean'
| 'int',
category: 'multilineArray',
since: '0.0.1',
default: defaultValue as any,
description: optionHelp[key],
} as any;
accum[key] = supportOption;
return accum;
},
{} as Record<keyof MultilineArrayOptions, SupportOption>,
);

export const defaultOptions: Partial<RequiredOptions> & Required<MultilineArrayOptions> =
const defaultOptions: Partial<RequiredOptions> & Required<MultilineArrayOptions> =
defaultMultilineArrayOptions;

/** Not actually exported. Just for type checking purposes. */
const languages = [
{
name: 'JavaScript',
parsers: [
'babel',
'acorn',
'espree',
'meriyah',
'babel-flow',
'babel-ts',
'flow',
'typescript',
],
},
{name: 'TypeScript', parsers: ['typescript', 'babel-ts']},
{name: 'JSON.stringify', parsers: ['json-stringify']},
{name: 'JSON', parsers: ['json']},
{name: 'JSON with Comments', parsers: ['json']},
{name: 'JSON5', parsers: ['json5']},
];

const plugin: Plugin = {
options,
printers,
defaultOptions,
parsers,
languages,
};

export {defaultOptions, options, parsers};
8 changes: 7 additions & 1 deletion src/preprocessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function addMultilinePrinter(options: ParserOptions): void {
}

function findPluginsByParserName(parserName: string, options: ParserOptions): Plugin[] {
return options.plugins.filter((plugin): plugin is Plugin => {
return options.plugins?.filter((plugin): plugin is Plugin => {
return (
typeof plugin === 'object' &&
(plugin as any).pluginMarker !== pluginMarker &&
Expand Down Expand Up @@ -83,6 +83,12 @@ export function wrapParser(originalParser: Parser, parserName: string) {
processedText,
{
...options,
...((pluginWithPreprocessor as any)?.name?.includes(
'prettier-plugin-organize-imports',
) &&
options.filepath?.endsWith('.js.flow') && {
filepath: options.filepath.slice(0, -5),
}),
plugins: options.plugins.filter(
(plugin) => (plugin as any).pluginMarker !== pluginMarker,
),
Expand Down
6 changes: 3 additions & 3 deletions src/printer/insert-new-lines-into-arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Doc, doc} from 'prettier';
import {isDocCommand, stringifyDoc} from '../augments/doc';
import {isDocArray, isDocCommand, stringifyDoc} from '../augments/doc';
import {walkDoc} from './child-docs';

const nestingSyntaxOpen = '[{(`' as const;
Expand Down Expand Up @@ -50,8 +50,8 @@ export function insertLinesIntoArguments(
codePath = 'indent';
}
// case 2. sibling is concat
else if (isDocCommand(openingSibling) && openingSibling.type === 'concat') {
findingSiblingChildren = openingSibling.parts;
else if (isDocArray(openingSibling)) {
findingSiblingChildren = openingSibling;
codePath = 'concat';
}
// case 3. sibling is group
Expand Down
37 changes: 20 additions & 17 deletions src/printer/insert-new-lines.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getObjectTypedKeys, PropertyValueType} from '@augment-vir/common';
import {CallExpression, Node} from 'estree';
import {CallExpression} from 'estree';
import {AstPath, Doc, doc, ParserOptions} from 'prettier';
import {isDocCommand, stringifyDoc} from '../augments/doc';
import {isDocArray, isDocCommand, stringifyDoc} from '../augments/doc';
import {MultilineArrayOptions} from '../options';
import {walkDoc} from './child-docs';
import {
Expand Down Expand Up @@ -92,7 +92,7 @@ function insertLinesIntoArray(
console.info({firstIndentContentsChild: startingLine});
}
if (!isDocCommand(startingLine) || startingLine.type !== 'line') {
if (isDocCommand(startingLine) && startingLine.type === 'concat') {
if (isDocArray(startingLine)) {
undoAllMutations();
return false;
} else {
Expand Down Expand Up @@ -330,12 +330,12 @@ function insertLinesIntoArray(
parentToMutate[siblingIndex] = commaSibling;
});
}
} else if (isDocCommand(currentDoc) && currentDoc.type === 'concat') {
const firstPart = currentDoc.parts[0];
const secondPart = currentDoc.parts[1];
} else if (isDocArray(currentDoc)) {
const firstPart = currentDoc[0];
const secondPart = currentDoc[1];
if (debug) {
console.info('got concat child doc');
console.info(currentDoc.parts, {firstPart, secondPart});
console.info(currentDoc, {firstPart, secondPart});
console.info(
isDocCommand(firstPart),
isDocCommand(secondPart),
Expand Down Expand Up @@ -434,17 +434,20 @@ function getLatestSetValue<T extends object>(
): PropertyValueType<T> | undefined {
const relevantSetLineCountsKey: keyof T = getObjectTypedKeys(triggers)
.sort()
.reduce((closestKey, currentKey): keyof T => {
if (Number(currentKey) < currentLine) {
const currentData = triggers[currentKey];

if (currentData && currentData.lineEnd > currentLine) {
return currentKey;
.reduce(
(closestKey, currentKey): keyof T => {
if (Number(currentKey) < currentLine) {
const currentData = triggers[currentKey];

if (currentData && currentData.lineEnd > currentLine) {
return currentKey;
}
}
}

return closestKey;
}, '' as keyof T);
return closestKey;
},
'' as keyof T,
);
const relevantSetLineCount: PropertyValueType<T> | undefined =
triggers[relevantSetLineCountsKey]?.data;

Expand All @@ -463,7 +466,7 @@ export function printWithMultilineArrays(
`Could not find valid root node in ${path.stack.map((entry) => entry.type).join(',')}`,
);
}
const node = path.getValue() as Node | undefined;
const node = path.getNode();

if (
node &&
Expand Down
Loading

0 comments on commit 2571156

Please sign in to comment.