prettier-plugin-multiline-arrays-2 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +3 -0
  2. package/LICENSE-CC0 +121 -0
  3. package/LICENSE-MIT +21 -0
  4. package/README.md +91 -0
  5. package/dist/augments/array.d.ts +3 -0
  6. package/dist/augments/array.js +21 -0
  7. package/dist/augments/array.test.d.ts +1 -0
  8. package/dist/augments/array.test.js +34 -0
  9. package/dist/augments/doc.d.ts +5 -0
  10. package/dist/augments/doc.js +30 -0
  11. package/dist/augments/string.d.ts +1 -0
  12. package/dist/augments/string.js +6 -0
  13. package/dist/index.d.ts +11 -0
  14. package/dist/index.js +46 -0
  15. package/dist/options.d.ts +32 -0
  16. package/dist/options.js +71 -0
  17. package/dist/options.test.d.ts +1 -0
  18. package/dist/options.test.js +48 -0
  19. package/dist/plugin-marker.d.ts +1 -0
  20. package/dist/plugin-marker.js +2 -0
  21. package/dist/preprocessing.d.ts +2 -0
  22. package/dist/preprocessing.js +94 -0
  23. package/dist/printer/child-docs.d.ts +18 -0
  24. package/dist/printer/child-docs.js +89 -0
  25. package/dist/printer/comment-triggers.d.ts +25 -0
  26. package/dist/printer/comment-triggers.js +327 -0
  27. package/dist/printer/comments.d.ts +2 -0
  28. package/dist/printer/comments.js +34 -0
  29. package/dist/printer/insert-new-lines.d.ts +15 -0
  30. package/dist/printer/insert-new-lines.js +580 -0
  31. package/dist/printer/insert-new-lines.test.d.ts +1 -0
  32. package/dist/printer/insert-new-lines.test.js +36 -0
  33. package/dist/printer/leading-new-line.d.ts +7 -0
  34. package/dist/printer/leading-new-line.js +31 -0
  35. package/dist/printer/multiline-array-printer.d.ts +4 -0
  36. package/dist/printer/multiline-array-printer.js +42 -0
  37. package/dist/printer/original-printer.d.ts +3 -0
  38. package/dist/printer/original-printer.js +10 -0
  39. package/dist/printer/supported-node-types.d.ts +9 -0
  40. package/dist/printer/supported-node-types.js +16 -0
  41. package/dist/printer/trailing-comma.d.ts +6 -0
  42. package/dist/printer/trailing-comma.js +28 -0
  43. package/dist/readme-examples/formatted-with-comments.example.d.ts +3 -0
  44. package/dist/readme-examples/formatted-with-comments.example.js +16 -0
  45. package/dist/readme-examples/formatted.example.d.ts +2 -0
  46. package/dist/readme-examples/formatted.example.js +12 -0
  47. package/dist/readme-examples/not-formatted.example.d.ts +2 -0
  48. package/dist/readme-examples/not-formatted.example.js +6 -0
  49. package/dist/readme-examples/prettier-options.example.d.ts +4 -0
  50. package/dist/readme-examples/prettier-options.example.js +5 -0
  51. package/dist/test/babel-ts.test.d.ts +1 -0
  52. package/dist/test/babel-ts.test.js +10 -0
  53. package/dist/test/javascript.test.d.ts +1 -0
  54. package/dist/test/javascript.test.js +702 -0
  55. package/dist/test/json.test.d.ts +1 -0
  56. package/dist/test/json.test.js +403 -0
  57. package/dist/test/json5.test.d.ts +1 -0
  58. package/dist/test/json5.test.js +210 -0
  59. package/dist/test/prettier-config.d.ts +2 -0
  60. package/dist/test/prettier-config.js +12 -0
  61. package/dist/test/prettier-versions.mock.script.d.ts +1 -0
  62. package/dist/test/prettier-versions.mock.script.js +123 -0
  63. package/dist/test/run-tests.mock.d.ts +17 -0
  64. package/dist/test/run-tests.mock.js +95 -0
  65. package/dist/test/typescript-tests.mock.d.ts +2 -0
  66. package/dist/test/typescript-tests.mock.js +1575 -0
  67. package/dist/test/typescript.test.d.ts +1 -0
  68. package/dist/test/typescript.test.js +10 -0
  69. package/package.json +117 -0
@@ -0,0 +1,123 @@
1
+ import { assert, assertWrap } from "@augment-vir/assert";
2
+ import { awaitedBlockingMap, indent, log, logColors, } from "@augment-vir/common";
3
+ import { readPackageJson, runShellCommand } from "@augment-vir/node";
4
+ import { calculateRelativeDate, createUtcFullDate, getNowInUtcTimezone, toTimestamp, } from "date-vir";
5
+ import { resolve } from "node:path";
6
+ import { assertValidShape, defineShape, recordShape, unknownShape, } from "object-shape-tester";
7
+ import semver from "semver";
8
+ const repoRootDirPath = resolve(import.meta.dirname, "..", "..");
9
+ const repoPackageJson = await readPackageJson(repoRootDirPath);
10
+ const supportedPrettierRange = assertWrap.isTruthy(repoPackageJson.peerDependencies?.prettier, "Failed to find supported Prettier version range from peer dependencies.");
11
+ const currentPrettierVersion = assertWrap.isTruthy(repoPackageJson.devDependencies?.prettier, "Failed to find current Prettier version from dev dependencies.");
12
+ async function fetchPrettierV3MinorVersions() {
13
+ const response = await fetch("https://registry.npmjs.org/prettier");
14
+ if (!response.ok) {
15
+ throw new Error(`Failed to fetch npm metadata: ${response.status}`);
16
+ }
17
+ const responseJson = await response.json();
18
+ assertValidShape(responseJson, defineShape({
19
+ versions: recordShape({
20
+ keys: "",
21
+ values: unknownShape(),
22
+ }),
23
+ time: recordShape({
24
+ keys: "",
25
+ values: "",
26
+ }),
27
+ }), {
28
+ allowExtraKeys: true,
29
+ });
30
+ const oldestAllowedPublishTime = toTimestamp(calculateRelativeDate(getNowInUtcTimezone(), {
31
+ days: -5,
32
+ }));
33
+ const rawVersions = Object.keys(responseJson.versions).filter((version) => {
34
+ const publishTime = responseJson.time[version];
35
+ if (!publishTime) {
36
+ log.warning(`Failed to find publish time for Prettier version: ${version}`);
37
+ return false;
38
+ }
39
+ return (toTimestamp(createUtcFullDate(publishTime)) <=
40
+ oldestAllowedPublishTime);
41
+ });
42
+ const mappedLatestMinorVersions = rawVersions.reduce((latestMinorVersions, version) => {
43
+ if (!semver.satisfies(version, supportedPrettierRange)) {
44
+ return latestMinorVersions;
45
+ }
46
+ const parsedVersion = semver.parse(version);
47
+ if (!parsedVersion) {
48
+ log.warning(`Failed to parse Prettier version with semver: ${version}`);
49
+ return latestMinorVersions;
50
+ }
51
+ const key = `${parsedVersion.major}.${parsedVersion.minor}`;
52
+ const previousVersion = latestMinorVersions[key];
53
+ if (!previousVersion || semver.gt(version, previousVersion)) {
54
+ latestMinorVersions[key] = version;
55
+ }
56
+ return latestMinorVersions;
57
+ }, {});
58
+ return Object.values(mappedLatestMinorVersions).sort(semver.compare);
59
+ }
60
+ async function runPrettierTests() {
61
+ const prettierVersions = await fetchPrettierV3MinorVersions();
62
+ log.info(`Testing with prettier versions:\n${indent(prettierVersions.join("\n"))}`);
63
+ const versionPasses = await awaitedBlockingMap(prettierVersions, async (version) => {
64
+ log.info(`\n\n>>>>>>>>>> Prettier v${version}\n`);
65
+ try {
66
+ log.faint(`Installing Prettier v${version}...`);
67
+ await runShellCommand(`npm i -D --no-save prettier@${version}`, {
68
+ cwd: repoRootDirPath,
69
+ rejectOnError: true,
70
+ });
71
+ const { stdout } = await runShellCommand("npm ls prettier", {
72
+ cwd: repoRootDirPath,
73
+ });
74
+ assert.hasValue(stdout, `└── prettier@${version}`, `Prettier v${version} was not installed.`);
75
+ }
76
+ catch (error) {
77
+ log.faint(error);
78
+ log.error(`Failed to install Prettier v${version}.`);
79
+ return {
80
+ version,
81
+ success: false,
82
+ };
83
+ }
84
+ log.faint(`Running tests for Prettier v${version}...`);
85
+ try {
86
+ await runShellCommand("npm test", {
87
+ cwd: repoRootDirPath,
88
+ rejectOnError: true,
89
+ hookUpToConsole: true,
90
+ });
91
+ return {
92
+ version,
93
+ success: true,
94
+ };
95
+ }
96
+ catch {
97
+ return {
98
+ version,
99
+ success: false,
100
+ };
101
+ }
102
+ });
103
+ log.faint(`Restoring Prettier v${currentPrettierVersion}...\n\n`);
104
+ await runShellCommand(`npm i -D --no-save prettier@${currentPrettierVersion}`, {
105
+ cwd: repoRootDirPath,
106
+ rejectOnError: true,
107
+ });
108
+ versionPasses.forEach(({ success, version }) => {
109
+ log.info(`Prettier v${version}: ${success ? logColors.success : logColors.error}${success ? "pass" : "fail"}${logColors.reset}`);
110
+ });
111
+ const success = versionPasses.every(({ success }) => {
112
+ return success;
113
+ });
114
+ if (success) {
115
+ log.success("Versioned tests passed.");
116
+ process.exit(0);
117
+ }
118
+ else {
119
+ log.error("Versioned tests failed.");
120
+ process.exit(1);
121
+ }
122
+ }
123
+ await runPrettierTests();
@@ -0,0 +1,17 @@
1
+ import { type PartialWithNullable } from '@augment-vir/common';
2
+ import { type Options as PrettierOptions } from 'prettier';
3
+ import { type MultilineArrayOptions } from '../options.js';
4
+ export type MultilineArrayTest = {
5
+ it: string;
6
+ code: string;
7
+ expect?: string | undefined;
8
+ options?: (Partial<MultilineArrayOptions> & PartialWithNullable<PrettierOptions>) | undefined;
9
+ only?: true;
10
+ skip?: true;
11
+ failureMessage?: string;
12
+ };
13
+ export declare function runTests({ extension, tests, parser, }: Readonly<{
14
+ extension: string;
15
+ tests: MultilineArrayTest[];
16
+ parser: string;
17
+ }>): void;
@@ -0,0 +1,95 @@
1
+ import { assert, check } from '@augment-vir/assert';
2
+ import { omitObjectKeys, removeColor, removeNullishValues, } from '@augment-vir/common';
3
+ import { it } from '@augment-vir/test';
4
+ import { format as prettierFormat } from 'prettier';
5
+ import { repoConfig } from './prettier-config.js';
6
+ async function runPrettierFormat({ code, extension, options = {}, parser, }) {
7
+ if (extension.startsWith('.')) {
8
+ extension = extension.slice(1);
9
+ }
10
+ const filepathOptions = check.isString(options.filepath)
11
+ ? {
12
+ filepath: options.filepath,
13
+ }
14
+ : 'filepath' in options
15
+ ? {}
16
+ : {
17
+ filepath: `blah.${extension}`,
18
+ };
19
+ const prettierOptions = {
20
+ ...repoConfig,
21
+ ...removeNullishValues(omitObjectKeys(options, [
22
+ 'filepath',
23
+ ])),
24
+ ...filepathOptions,
25
+ ...(parser
26
+ ? {
27
+ parser,
28
+ }
29
+ : {}),
30
+ };
31
+ return await prettierFormat(code, prettierOptions);
32
+ }
33
+ let forced = false;
34
+ let allPassed = true;
35
+ function removeIndent(input) {
36
+ return (input
37
+ .replace(/^\s*\n\s*/, '')
38
+ .replace(/\n {12}/g, '\n')
39
+ // this is only used for tests
40
+ .replace(/\n[ \t]+$/, '\n'));
41
+ }
42
+ export function runTests({ extension, tests, parser, }) {
43
+ tests.forEach((test) => {
44
+ async function testCallback() {
45
+ try {
46
+ const inputCode = removeIndent(test.code);
47
+ const expected = removeIndent(test.expect ?? test.code);
48
+ const formatted = await runPrettierFormat({
49
+ code: inputCode,
50
+ extension,
51
+ options: test.options,
52
+ parser,
53
+ });
54
+ assert.strictEquals(formatted, expected);
55
+ if (formatted !== expected) {
56
+ allPassed = false;
57
+ }
58
+ }
59
+ catch (error) {
60
+ allPassed = false;
61
+ if (test.failureMessage && error instanceof Error) {
62
+ const strippedMessage = removeColor(error.message);
63
+ if (test.failureMessage !== strippedMessage) {
64
+ console.info({
65
+ strippedMessage,
66
+ });
67
+ }
68
+ assert.strictEquals(removeColor(strippedMessage), test.failureMessage);
69
+ }
70
+ else {
71
+ throw error;
72
+ }
73
+ }
74
+ }
75
+ if (test.only) {
76
+ forced = true;
77
+ // eslint-disable-next-line sonarjs/no-exclusive-tests
78
+ it.only(test.it, testCallback);
79
+ }
80
+ else if (test.skip) {
81
+ it.skip(test.it, testCallback);
82
+ }
83
+ else {
84
+ it(test.it, testCallback);
85
+ }
86
+ });
87
+ if (forced) {
88
+ // eslint-disable-next-line sonarjs/no-exclusive-tests
89
+ it.only('forced tests should not remain in the code', () => {
90
+ if (allPassed) {
91
+ assert.strictEquals(forced, false, 'Only tests are not allowed.');
92
+ }
93
+ });
94
+ }
95
+ }
@@ -0,0 +1,2 @@
1
+ import { type MultilineArrayTest } from './run-tests.mock.js';
2
+ export declare const typescriptTests: MultilineArrayTest[];