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.
- package/LICENSE +3 -0
- package/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +91 -0
- package/dist/augments/array.d.ts +3 -0
- package/dist/augments/array.js +21 -0
- package/dist/augments/array.test.d.ts +1 -0
- package/dist/augments/array.test.js +34 -0
- package/dist/augments/doc.d.ts +5 -0
- package/dist/augments/doc.js +30 -0
- package/dist/augments/string.d.ts +1 -0
- package/dist/augments/string.js +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +46 -0
- package/dist/options.d.ts +32 -0
- package/dist/options.js +71 -0
- package/dist/options.test.d.ts +1 -0
- package/dist/options.test.js +48 -0
- package/dist/plugin-marker.d.ts +1 -0
- package/dist/plugin-marker.js +2 -0
- package/dist/preprocessing.d.ts +2 -0
- package/dist/preprocessing.js +94 -0
- package/dist/printer/child-docs.d.ts +18 -0
- package/dist/printer/child-docs.js +89 -0
- package/dist/printer/comment-triggers.d.ts +25 -0
- package/dist/printer/comment-triggers.js +327 -0
- package/dist/printer/comments.d.ts +2 -0
- package/dist/printer/comments.js +34 -0
- package/dist/printer/insert-new-lines.d.ts +15 -0
- package/dist/printer/insert-new-lines.js +580 -0
- package/dist/printer/insert-new-lines.test.d.ts +1 -0
- package/dist/printer/insert-new-lines.test.js +36 -0
- package/dist/printer/leading-new-line.d.ts +7 -0
- package/dist/printer/leading-new-line.js +31 -0
- package/dist/printer/multiline-array-printer.d.ts +4 -0
- package/dist/printer/multiline-array-printer.js +42 -0
- package/dist/printer/original-printer.d.ts +3 -0
- package/dist/printer/original-printer.js +10 -0
- package/dist/printer/supported-node-types.d.ts +9 -0
- package/dist/printer/supported-node-types.js +16 -0
- package/dist/printer/trailing-comma.d.ts +6 -0
- package/dist/printer/trailing-comma.js +28 -0
- package/dist/readme-examples/formatted-with-comments.example.d.ts +3 -0
- package/dist/readme-examples/formatted-with-comments.example.js +16 -0
- package/dist/readme-examples/formatted.example.d.ts +2 -0
- package/dist/readme-examples/formatted.example.js +12 -0
- package/dist/readme-examples/not-formatted.example.d.ts +2 -0
- package/dist/readme-examples/not-formatted.example.js +6 -0
- package/dist/readme-examples/prettier-options.example.d.ts +4 -0
- package/dist/readme-examples/prettier-options.example.js +5 -0
- package/dist/test/babel-ts.test.d.ts +1 -0
- package/dist/test/babel-ts.test.js +10 -0
- package/dist/test/javascript.test.d.ts +1 -0
- package/dist/test/javascript.test.js +702 -0
- package/dist/test/json.test.d.ts +1 -0
- package/dist/test/json.test.js +403 -0
- package/dist/test/json5.test.d.ts +1 -0
- package/dist/test/json5.test.js +210 -0
- package/dist/test/prettier-config.d.ts +2 -0
- package/dist/test/prettier-config.js +12 -0
- package/dist/test/prettier-versions.mock.script.d.ts +1 -0
- package/dist/test/prettier-versions.mock.script.js +123 -0
- package/dist/test/run-tests.mock.d.ts +17 -0
- package/dist/test/run-tests.mock.js +95 -0
- package/dist/test/typescript-tests.mock.d.ts +2 -0
- package/dist/test/typescript-tests.mock.js +1575 -0
- package/dist/test/typescript.test.d.ts +1 -0
- package/dist/test/typescript.test.js +10 -0
- 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,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,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,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 @@
|
|
|
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 {};
|