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,42 @@
1
+ import { assertWrap } from "@augment-vir/assert";
2
+ import estreePluginModule from "prettier/plugins/estree";
3
+ import { envDebugKey, fillInOptions, } from "../options.js";
4
+ import { printWithMultilineArrays } from "./insert-new-lines.js";
5
+ /**
6
+ * The estree plugin is bundled with Prettier and exposes the base printers we
7
+ * want to wrap. Its types don't currently export `printers`, so we access it
8
+ * via the module's exported value.
9
+ */
10
+ const estreePlugin = estreePluginModule;
11
+ const debug = !!process.env[envDebugKey];
12
+ export function createMultilineArrayPrinter(basePrinter) {
13
+ return {
14
+ ...basePrinter,
15
+ /**
16
+ * The 4th parameter `args` is used by Prettier internally. We must
17
+ * declare it to preserve the function's arity (Function.length), which
18
+ * Prettier checks to determine behavior. Without this 4th parameter,
19
+ * Prettier's internal printing logic behaves differently, causing
20
+ * incorrect output for non-array code (like function calls).
21
+ */
22
+ print(path, options, print, args) {
23
+ if (debug) {
24
+ console.info("[multiline-arrays] multilineArrayPrinter.print for node:", path.getNode()?.type);
25
+ }
26
+ const originalOutput = basePrinter.print(path, options, print, args);
27
+ if (options.filepath?.endsWith("package.json") &&
28
+ options.plugins.some((plugin) => typeof plugin === "object" &&
29
+ plugin.name?.includes("prettier-plugin-packagejson"))) {
30
+ return originalOutput;
31
+ }
32
+ const multilineOptions = fillInOptions(options);
33
+ return printWithMultilineArrays({
34
+ originalFormattedOutput: originalOutput,
35
+ path,
36
+ inputOptions: multilineOptions,
37
+ debug,
38
+ });
39
+ },
40
+ };
41
+ }
42
+ export const multilineArrayPrinter = createMultilineArrayPrinter(assertWrap.isDefined(estreePlugin.printers.estree, "No ESTree printer found."));
@@ -0,0 +1,3 @@
1
+ import type { Printer } from "prettier";
2
+ export declare function setOriginalPrinter(input: Printer): void;
3
+ export declare function getOriginalPrinter(): Printer;
@@ -0,0 +1,10 @@
1
+ let originalPrinter;
2
+ export function setOriginalPrinter(input) {
3
+ originalPrinter = input;
4
+ }
5
+ export function getOriginalPrinter() {
6
+ if (!originalPrinter) {
7
+ throw new Error("originalPrinter hasn't been defined yet!");
8
+ }
9
+ return originalPrinter;
10
+ }
@@ -0,0 +1,9 @@
1
+ import type { ArrayExpression, ArrayPattern, BaseNode, Node } from "estree";
2
+ type TSTupleType = BaseNode & {
3
+ type: "TSTupleType";
4
+ elementTypes: (BaseNode | null)[];
5
+ };
6
+ export type ArrayLikeNode = ArrayExpression | ArrayPattern | TSTupleType;
7
+ export declare function isArrayLikeNode(node: Node): boolean;
8
+ export declare function getArrayLikeElements(node: ArrayLikeNode): (BaseNode | null)[];
9
+ export {};
@@ -0,0 +1,16 @@
1
+ const arrayLikeNodeTypes = [
2
+ "ArrayExpression",
3
+ "ArrayPattern",
4
+ // this expression type isn't accounted for in the types, but I saw it used in another plugin
5
+ "TupleExpression",
6
+ "TSTupleType",
7
+ ];
8
+ export function isArrayLikeNode(node) {
9
+ return arrayLikeNodeTypes.includes(node.type);
10
+ }
11
+ export function getArrayLikeElements(node) {
12
+ if (node.type === "TSTupleType") {
13
+ return node.elementTypes;
14
+ }
15
+ return node.elements;
16
+ }
@@ -0,0 +1,6 @@
1
+ import type { BaseNode } from "estree";
2
+ export declare function containsTrailingComma({ nodeLocation, children, originalLines, }: Readonly<{
3
+ nodeLocation: BaseNode["loc"];
4
+ children: (BaseNode | null)[];
5
+ originalLines: string[];
6
+ }>): boolean;
@@ -0,0 +1,28 @@
1
+ import { extractTextBetweenRanges } from "../augments/array.js";
2
+ export function containsTrailingComma({ nodeLocation, children, originalLines, }) {
3
+ const lastElement = children[children.length - 1];
4
+ if (lastElement) {
5
+ const startLocation = lastElement.loc?.end;
6
+ if (!startLocation) {
7
+ return false;
8
+ }
9
+ const endLocation = nodeLocation?.end;
10
+ if (!endLocation) {
11
+ return false;
12
+ }
13
+ const textPastLastElement = extractTextBetweenRanges(originalLines, {
14
+ start: {
15
+ column: startLocation.column - 1,
16
+ line: startLocation.line - 1,
17
+ },
18
+ end: {
19
+ column: endLocation.column - 1,
20
+ line: endLocation.line - 1,
21
+ },
22
+ });
23
+ if (textPastLastElement.includes(",")) {
24
+ return true;
25
+ }
26
+ }
27
+ return false;
28
+ }
@@ -0,0 +1,3 @@
1
+ export declare const linePatternArray: string[];
2
+ export declare const highThresholdArray: string[];
3
+ export declare const superHighThresholdArray: string[];
@@ -0,0 +1,16 @@
1
+ // prettier-multiline-arrays-next-line-pattern: 2 1
2
+ export const linePatternArray = [
3
+ 'a', 'b',
4
+ 'c',
5
+ 'd', 'e',
6
+ ];
7
+ // Even if this example had a leading new line or a trailing comma, it won't wrap because the
8
+ // comment overrides that behavior.
9
+ // prettier-multiline-arrays-next-threshold: 10
10
+ export const highThresholdArray = ['a', 'b', 'c', 'd', 'e'];
11
+ // this array doesn't fully wrap even though it exceeded the column width because the
12
+ // "next-line-pattern" comment overrides Prettier's column width wrapping
13
+ // prettier-multiline-arrays-next-line-pattern: 100
14
+ export const superHighThresholdArray = [
15
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
16
+ ];
@@ -0,0 +1,2 @@
1
+ export declare const myArray: string[];
2
+ export declare const myCustomArray: string[];
@@ -0,0 +1,12 @@
1
+ export const myArray = [
2
+ 'a',
3
+ 'b',
4
+ 'c',
5
+ ]; // note the trailing comma which forces a wrap
6
+ export const myCustomArray = [
7
+ 'a',
8
+ 'b',
9
+ 'c',
10
+ 'd',
11
+ 'e',
12
+ ]; // note the leading new line which forces a wrap
@@ -0,0 +1,2 @@
1
+ export declare const myArray: string[];
2
+ export declare const myCustomArray: string[];
@@ -0,0 +1,6 @@
1
+ // prettier-ignore
2
+ export const myArray = ['a', 'b', 'c',]; // note the trailing comma which forces a wrap
3
+ // prettier-ignore
4
+ export const myCustomArray = [
5
+ 'a', 'b', 'c', 'd', 'e'
6
+ ]; // note the leading new line which forces a wrap
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ plugins: string[];
3
+ };
4
+ export default _default;
@@ -0,0 +1,5 @@
1
+ export default {
2
+ plugins: [
3
+ 'prettier-plugin-multiline-arrays',
4
+ ],
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { describe } from '@augment-vir/test';
2
+ import { runTests } from './run-tests.mock.js';
3
+ import { typescriptTests } from './typescript-tests.mock.js';
4
+ describe('babel-ts multiline array formatting', () => {
5
+ runTests({
6
+ extension: '.ts',
7
+ tests: typescriptTests,
8
+ parser: 'babel-ts',
9
+ });
10
+ });
@@ -0,0 +1 @@
1
+ export {};