Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Jul 23, 2023
1 parent 33c27ae commit 46dba5c
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 476 deletions.
21 changes: 19 additions & 2 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const {basePrettierConfig} = require('virmator/base-configs/base-prettierrc.js');

/**
* @typedef {import('prettier-plugin-multiline-arrays').MultilineArrayOptions} MultilineOptions
Expand All @@ -7,7 +6,25 @@ const {basePrettierConfig} = require('virmator/base-configs/base-prettierrc.js')
* @type {PrettierOptions & MultilineOptions}
*/
const prettierConfig = {
...basePrettierConfig,
arrowParens: 'always',
bracketSameLine: false,
bracketSpacing: false,
endOfLine: 'lf',
htmlWhitespaceSensitivity: 'ignore',
jsonRecursiveSort: true,
multilineArraysWrapThreshold: 1,
pluginSearchDirs: false,
printWidth: 100,
singleQuote: true,
tabWidth: 4,
trailingComma: 'all',
plugins: [
'prettier-plugin-toml',
'prettier-plugin-packagejson',
'prettier-plugin-multiline-arrays',
'prettier-plugin-organize-imports',
'prettier-plugin-jsdoc',
]
};

module.exports = prettierConfig;
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ The precedence of forcing wrapping goes as follows:
Tested to be compatible with the following plugins. It is likely compatible with many others as well. This plugin must be placed in the order specified below.

1. `prettier-plugin-toml`
2. `prettier-plugin-sort-json`
2. `prettier-plugin-sort-json` (needs to be updated to v3)
3. `prettier-plugin-packagejson`
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
781 changes: 382 additions & 399 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"test:types": "tsc --noEmit"
},
"dependencies": {
"@augment-vir/common": "^15.3.0",
"@augment-vir/common": "^15.5.0",
"proxy-vir": "^0.0.1"
},
"devDependencies": {
"@augment-vir/chai": "^15.3.0",
"@augment-vir/chai": "^15.5.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.5",
Expand All @@ -55,24 +55,22 @@
"chai": "^4.3.7",
"cross-env": "^7.0.3",
"cspell": "^6.31.2",
"esbuild": "^0.18.13",
"esbuild": "^0.18.16",
"istanbul-smart-text-reporter": "^1.1.2",
"markdown-code-example-inserter": "^0.3.1",
"mocha": "^10.2.0",
"mocha-spec-reporter-with-file-names": "^0.0.3",
"npm-check-updates": "^16.10.16",
"prettier": "^3.0.0",
"prettier-plugin-interpolated-html-tags": "^0.0.4",
"prettier-plugin-jsdoc": "^0.4.2",
"prettier-plugin-jsdoc": "^1.0.1",
"prettier-plugin-multiline-arrays": "*",
"prettier-plugin-organize-imports": "^3.2.3",
"prettier-plugin-packagejson": "^2.4.5",
"prettier-plugin-sort-json": "^1.0.0",
"prettier-plugin-toml": "^0.3.1",
"test-as-package": "^0.0.4",
"ts-node": "^10.9.1",
"typescript": "5.1.6",
"virmator": "^7.2.1"
"virmator": "^7.2.5"
},
"overrides": {
"semver": "^7.5.2"
Expand Down
49 changes: 31 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ import {MultilineArrayOptions, defaultMultilineArrayOptions, optionHelp} from '.
import {wrapParser} from './preprocessing';
import {multilineArrayPrinter} from './printer/multiline-array-printer';


// exports in case others want to utilize these
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(
async function loadPlugin(): Promise<Plugin> {


const parsers: Record<string, Parser<any>> = mapObjectValues(
{
typescript: tsParsers.typescript,
babel: babelParsers.babel,
Expand All @@ -41,16 +35,16 @@ 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];
Expand All @@ -64,19 +58,38 @@ export const options: Record<keyof MultilineArrayOptions, SupportOption> = getOb
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;

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

/** Not actually exported. Just for type checking purposes. */
const plugin: Plugin = {
const plugin: Plugin = {
options,
printers,
defaultOptions,
parsers,
languages,

};
return plugin;
}

/**
*
* this doesn't work because it return a promise, but how else can we access the languages?
*/
export default loadPlugin();
3 changes: 3 additions & 0 deletions src/preprocessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export function wrapParser(originalParser: Parser, parserName: string) {
initialTarget: originalParser,
});

console.log('attaching the preprocessor', originalParser);
function multilineArraysPluginPreprocess(text: string, options: ParserOptions) {

console.log('firing the preprocessor');
const pluginsWithRelevantParsers = findPluginsByParserName(parserName, options);
pluginsWithRelevantParsers.forEach((plugin) => {
const currentParser = plugin.parsers?.[parserName];
Expand Down
8 changes: 4 additions & 4 deletions src/printer/insert-new-lines-into-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export function insertLinesIntoArguments(
codePath = 'indent';
}
// case 2. sibling is concat
else if (isDocCommand(openingSibling) && openingSibling.type === 'concat') {
findingSiblingChildren = openingSibling.parts;
codePath = 'concat';
}
// else if (isDocCommand(openingSibling) && openingSibling.type === 'concat') {
// findingSiblingChildren = openingSibling.parts;
// codePath = 'concat';
// }
// case 3. sibling is group
else if (openingSibling.type === 'group') {
const originalBreakValue = openingSibling.break;
Expand Down
69 changes: 35 additions & 34 deletions src/printer/insert-new-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ function insertLinesIntoArray(
console.info({firstIndentContentsChild: startingLine});
}
if (!isDocCommand(startingLine) || startingLine.type !== 'line') {
if (isDocCommand(startingLine) && startingLine.type === 'concat') {
undoAllMutations();
return false;
} else {
// // todo: I think this needs to check for arrays now instead of concat
// if (isDocCommand(startingLine) && startingLine.type === 'concat') {
// undoAllMutations();
// return false;
// } else {
throw new Error(`${found} first indent child was not a line.`);
}
// }
}
indentContents[0] = '';
undoMutations.push(() => {
Expand Down Expand Up @@ -330,35 +331,35 @@ function insertLinesIntoArray(
parentToMutate[siblingIndex] = commaSibling;
});
}
} else if (isDocCommand(currentDoc) && currentDoc.type === 'concat') {
const firstPart = currentDoc.parts[0];
const secondPart = currentDoc.parts[1];
if (debug) {
console.info('got concat child doc');
console.info(currentDoc.parts, {firstPart, secondPart});
console.info(
isDocCommand(firstPart),
isDocCommand(secondPart),
(firstPart as any).type === 'line',
(firstPart as any).hard,
(secondPart as any).type === 'break-parent',
);
}
if (
isDocCommand(firstPart) &&
isDocCommand(secondPart) &&
firstPart.type === 'line' &&
firstPart.hard &&
secondPart.type === 'break-parent'
) {
if (debug) {
console.info('concat child was indeed a line break');
}
forceFinalLineBreakExists = true;
return false;
} else if (debug) {
console.info('concat child doc was not a line break');
}
// } else if (isDocCommand(currentDoc) && currentDoc.type === 'concat') {
// const firstPart = currentDoc.parts[0];
// const secondPart = currentDoc.parts[1];
// if (debug) {
// console.info('got concat child doc');
// console.info(currentDoc.parts, {firstPart, secondPart});
// console.info(
// isDocCommand(firstPart),
// isDocCommand(secondPart),
// (firstPart as any).type === 'line',
// (firstPart as any).hard,
// (secondPart as any).type === 'break-parent',
// );
// }
// if (
// isDocCommand(firstPart) &&
// isDocCommand(secondPart) &&
// firstPart.type === 'line' &&
// firstPart.hard &&
// secondPart.type === 'break-parent'
// ) {
// if (debug) {
// console.info('concat child was indeed a line break');
// }
// forceFinalLineBreakExists = true;
// return false;
// } else if (debug) {
// console.info('concat child doc was not a line break');
// }
}
return true;
},
Expand Down
14 changes: 6 additions & 8 deletions src/test/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {stripColor} from '../augments/string';
import {MultilineArrayOptions} from '../options';
import {repoConfig} from './prettier-config';

function runPrettierFormat(
async function runPrettierFormat(
code: string,
extension: string,
options: Partial<MultilineArrayOptions> = {},
parser: string | undefined,
): string {
): Promise<string> {
if (extension.startsWith('.')) {
extension = extension.slice(1);
}
Expand All @@ -32,7 +32,7 @@ function runPrettierFormat(
plugins,
};

return prettierFormat(code, prettierOptions);
return await prettierFormat(code, prettierOptions);
}

export type MultilineArrayTest = {
Expand All @@ -58,11 +58,11 @@ function removeIndent(input: string): string {

export function runTests(extension: string, tests: MultilineArrayTest[], parser: string) {
tests.forEach((test) => {
function testCallback() {
async function testCallback() {
try {
const inputCode = removeIndent(test.code);
const expected = removeIndent(test.expect ?? test.code);
const formatted = runPrettierFormat(inputCode, extension, test.options, parser);
const formatted = await runPrettierFormat(inputCode, extension, test.options, parser);
assert.strictEqual(formatted, expected);
if (formatted !== expected) {
allPassed = false;
Expand All @@ -83,9 +83,7 @@ export function runTests(extension: string, tests: MultilineArrayTest[], parser:

if (test.force) {
forced = true;
it.only(test.it, () => {
testCallback();
});
it.only(test.it, testCallback);
} else if (test.exclude) {
it.skip(test.it, testCallback);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/test/typescript-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export const typescriptTests: MultilineArrayTest[] = [
options: {
multilineArraysWrapThreshold: 10,
},
force: true,
},
{
it: 'an array without wrapping should only take up one line',
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"module": "CommonJS",
"module": "node16",
"moduleResolution": "Node",
"noImplicitAny": true,
"noImplicitOverride": true,
Expand All @@ -18,7 +18,7 @@
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext",
"target": "ES2021",
"useUnknownInCatchVariables": true
},
"exclude": [
Expand Down

0 comments on commit 46dba5c

Please sign in to comment.