From 91382499bb736fcca994f0e30b4bcc34c146724f Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Sun, 9 Oct 2022 14:22:08 -0400 Subject: [PATCH] fix: handle missing prettier --- lib/markdown.ts | 28 +++++++++++-------- lib/rule-list.ts | 17 +++++------ lib/rule-notices.ts | 1 + package.json | 3 +- test/lib/__snapshots__/generator-test.ts.snap | 5 ---- test/lib/generator-test.ts | 2 +- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/markdown.ts b/lib/markdown.ts index b6b38c16..2e384f45 100644 --- a/lib/markdown.ts +++ b/lib/markdown.ts @@ -1,13 +1,20 @@ // General helpers for dealing with markdown files / content. -import prettier from 'prettier'; // eslint-disable-line node/no-extraneous-import -- prettier is included by eslint-plugin-square - export async function format(str: string, filePath: string): Promise { - const options = await prettier.resolveConfig(filePath); - return prettier.format(str, { - ...options, - parser: 'markdown', - }); + try { + const { default: prettier } = await import('prettier'); + const options = await prettier.resolveConfig(filePath); + return prettier + .format(str, { + ...options, + parser: 'markdown', + }) + .replace(/\s+$/, ''); // Trim the ending newline that prettier may add. + } catch { + // Skip prettier formatting if not installed. + /* istanbul ignore next -- TODO: Figure out how to test when prettier is not installed. */ + return str; + } } /** @@ -32,10 +39,9 @@ export async function replaceOrCreateHeader( lines.splice(0, 1); } - return ( - (await format(newHeader, pathToDoc)) + - lines.slice(markerLineIndex + 1).join('\n') - ); + return `${await format(newHeader, pathToDoc)}\n${lines + .slice(markerLineIndex + 1) + .join('\n')}`; } /** diff --git a/lib/rule-list.ts b/lib/rule-list.ts index ce1ba006..6d5431b6 100644 --- a/lib/rule-list.ts +++ b/lib/rule-list.ts @@ -159,16 +159,17 @@ export async function updateRulesList( const columns = getColumns(details, plugin, configsToRules); // New legend. - const legend = await format( - generateLegend(columns, urlConfigs), - pathToReadme - ); + const legend = generateLegend(columns, urlConfigs); // New rule list. - const list = await format( - generateRulesListMarkdown(columns, details, configsToRules, pluginPrefix), - pathToReadme + const list = generateRulesListMarkdown( + columns, + details, + configsToRules, + pluginPrefix ); - return `${preList}${BEGIN_RULE_LIST_MARKER}\n\n${legend}\n${list}\n${END_RULE_LIST_MARKER}${postList}`; + const newContent = await format(`${legend}\n\n${list}`, pathToReadme); + + return `${preList}${BEGIN_RULE_LIST_MARKER}\n\n${newContent}\n\n${END_RULE_LIST_MARKER}${postList}`; } diff --git a/lib/rule-notices.ts b/lib/rule-notices.ts index a0c3e0f2..ac063c90 100644 --- a/lib/rule-notices.ts +++ b/lib/rule-notices.ts @@ -201,6 +201,7 @@ export function generateRuleHeaderLines( pluginPrefix, urlConfigs ), + '', END_RULE_HEADER_MARKER, ].join('\n'); } diff --git a/package.json b/package.json index ebbda3ad..ad9621a2 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,8 @@ "typescript": "^4.8.4" }, "peerDependencies": { - "eslint": ">= 7" + "eslint": ">= 7", + "prettier": ">= 2" }, "engines": { "node": "^14.18.0 || ^16.0.0 || >=18.0.0" diff --git a/test/lib/__snapshots__/generator-test.ts.snap b/test/lib/__snapshots__/generator-test.ts.snap index 240923f0..f1ea6f6b 100644 --- a/test/lib/__snapshots__/generator-test.ts.snap +++ b/test/lib/__snapshots__/generator-test.ts.snap @@ -3,7 +3,6 @@ exports[`generator #generate CJS (non-ESM) generates the documentation 1`] = ` " - | Rule | Description | | ------------------------------ | ------------- | | [no-foo](docs/rules/no-foo.md) | disallow foo. | @@ -25,7 +24,6 @@ exports[`generator #generate Missing plugin package.json throws an error 1`] = ` exports[`generator #generate No configs found omits the config column 1`] = ` " - | Rule | Description | | ------------------------------ | ------------- | | [no-foo](docs/rules/no-foo.md) | disallow foo. | @@ -52,7 +50,6 @@ Foo. ## Rules - | Rule | Description | | ------------------------------ | ---------------------- | | [no-foo](docs/rules/no-foo.md) | Description of no-foo. | @@ -344,7 +341,6 @@ exports[`generator #generate no rules with description generates the documentati "## Rules - | Rule | | ------------------------------ | | [no-foo](docs/rules/no-foo.md) | @@ -364,7 +360,6 @@ exports[`generator #generate one rule missing description generates the document "## Rules - | Rule | Description | | ------------------------------ | ----------------------- | | [no-bar](docs/rules/no-bar.md) | | diff --git a/test/lib/generator-test.ts b/test/lib/generator-test.ts index df06b1bf..b5f58be3 100644 --- a/test/lib/generator-test.ts +++ b/test/lib/generator-test.ts @@ -5,7 +5,7 @@ import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { readFileSync } from 'node:fs'; import { jest } from '@jest/globals'; -import prettier from 'prettier'; // eslint-disable-line node/no-extraneous-import -- prettier is included by eslint-plugin-square +import prettier from 'prettier'; import * as sinon from 'sinon'; const __dirname = dirname(fileURLToPath(import.meta.url));