prettier-plugin-wolfram 0.7.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 +7 -0
- package/README.md +290 -0
- package/bin/prettier-wolfram.js +55 -0
- package/package.json +58 -0
- package/src/index.js +80 -0
- package/src/options.js +206 -0
- package/src/parser/adapter.js +690 -0
- package/src/parser/cstEqual.js +18 -0
- package/src/parser/index.js +29 -0
- package/src/parser/operators.js +35 -0
- package/src/parser/position.js +62 -0
- package/src/parser/tree-sitter-wolfram.wasm +0 -0
- package/src/range.js +98 -0
- package/src/rules/index.js +57 -0
- package/src/rules/line-width.js +129 -0
- package/src/rules/newlines-between-definitions.js +103 -0
- package/src/rules/no-bare-symbol-set.js +19 -0
- package/src/rules/no-dynamic-module-leak.js +74 -0
- package/src/rules/no-general-infix-function.js +52 -0
- package/src/rules/no-shadowed-pattern.js +71 -0
- package/src/rules/no-unused-module-var.js +84 -0
- package/src/rules/prefer-rule-delayed.js +59 -0
- package/src/rules/spacing-commas.js +64 -0
- package/src/rules/spacing-operators.js +87 -0
- package/src/translator/commentSpacing.js +51 -0
- package/src/translator/docComments.js +89 -0
- package/src/translator/index.js +98 -0
- package/src/translator/nodes/binary.js +205 -0
- package/src/translator/nodes/call.js +254 -0
- package/src/translator/nodes/compound.js +117 -0
- package/src/translator/nodes/container.js +194 -0
- package/src/translator/nodes/group.js +159 -0
- package/src/translator/nodes/infix.js +408 -0
- package/src/translator/nodes/leaf.js +605 -0
- package/src/translator/nodes/postfix.js +29 -0
- package/src/translator/nodes/prefix.js +27 -0
- package/src/translator/nodes/ternary.js +82 -0
- package/src/translator/ruleAlignment.js +133 -0
- package/src/translator/sourceLines.js +49 -0
- package/src/translator/sourcePreservation.js +22 -0
- package/src/translator/specialForms.js +665 -0
- package/src/utils/codeSpacing.js +420 -0
- package/src/utils/cstErrors.js +36 -0
- package/src/utils/offsets.js +132 -0
- package/src/utils/operatorSpacing.js +49 -0
package/src/options.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// src/options.js
|
|
2
|
+
function isPlainObject(value) {
|
|
3
|
+
return (
|
|
4
|
+
value !== null &&
|
|
5
|
+
typeof value === "object" &&
|
|
6
|
+
!Array.isArray(value)
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function hasOwn(object, key) {
|
|
11
|
+
return Object.prototype.hasOwnProperty.call(object, key);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function wolframOptionDefinition(definition) {
|
|
15
|
+
return {
|
|
16
|
+
type: definition.type,
|
|
17
|
+
category: "Wolfram",
|
|
18
|
+
...(hasOwn(definition, "default")
|
|
19
|
+
? { default: definition.default }
|
|
20
|
+
: {}),
|
|
21
|
+
description: definition.description,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const wolframOptions = {
|
|
26
|
+
newlinesBetweenDefinitions: {
|
|
27
|
+
type: "int",
|
|
28
|
+
default: 1,
|
|
29
|
+
minimum: 0,
|
|
30
|
+
legacyName: "wolframNewlinesBetweenDefinitions",
|
|
31
|
+
description: "Blank lines inserted between adjacent definitions.",
|
|
32
|
+
},
|
|
33
|
+
newlinesBetweenSetDefinitions: {
|
|
34
|
+
type: "int",
|
|
35
|
+
minimum: 0,
|
|
36
|
+
legacyName: "wolframNewlinesBetweenSetDefinitions",
|
|
37
|
+
description:
|
|
38
|
+
"Blank lines inserted between adjacent Set-family definitions. Inherits newlinesBetweenDefinitions when unset.",
|
|
39
|
+
},
|
|
40
|
+
newlinesBetweenSetDelayedDefinitions: {
|
|
41
|
+
type: "int",
|
|
42
|
+
minimum: 0,
|
|
43
|
+
legacyName: "wolframNewlinesBetweenSetDelayedDefinitions",
|
|
44
|
+
description:
|
|
45
|
+
"Blank lines inserted between adjacent SetDelayed-family definitions. Inherits newlinesBetweenDefinitions when unset.",
|
|
46
|
+
},
|
|
47
|
+
newlinesBetweenSetAndSetDelayedDefinitions: {
|
|
48
|
+
type: "int",
|
|
49
|
+
minimum: 0,
|
|
50
|
+
legacyName: "wolframNewlinesBetweenSetAndSetDelayedDefinitions",
|
|
51
|
+
description:
|
|
52
|
+
"Blank lines inserted between mixed Set-family and SetDelayed-family definitions. Inherits newlinesBetweenDefinitions when unset.",
|
|
53
|
+
},
|
|
54
|
+
newlinesBetweenSameNameDefinitions: {
|
|
55
|
+
type: "int",
|
|
56
|
+
default: 0,
|
|
57
|
+
minimum: 0,
|
|
58
|
+
legacyName: "wolframNewlinesBetweenSameNameDefinitions",
|
|
59
|
+
description:
|
|
60
|
+
"Blank lines inserted between adjacent definitions that belong to the same symbol.",
|
|
61
|
+
},
|
|
62
|
+
maxBlankLinesBetweenCode: {
|
|
63
|
+
type: "int",
|
|
64
|
+
default: 1,
|
|
65
|
+
minimum: 0,
|
|
66
|
+
legacyName: "wolframMaxBlankLinesBetweenCode",
|
|
67
|
+
description:
|
|
68
|
+
"Maximum source blank lines preserved between non-definition code statements.",
|
|
69
|
+
},
|
|
70
|
+
trailingNewline: {
|
|
71
|
+
type: "boolean",
|
|
72
|
+
default: false,
|
|
73
|
+
legacyName: "wolframTrailingNewline",
|
|
74
|
+
description:
|
|
75
|
+
"Emit one trailing newline at the end of non-empty formatted files.",
|
|
76
|
+
},
|
|
77
|
+
spaceAfterComma: {
|
|
78
|
+
type: "boolean",
|
|
79
|
+
default: true,
|
|
80
|
+
legacyName: "wolframSpaceAfterComma",
|
|
81
|
+
description: "Insert space after commas in argument lists.",
|
|
82
|
+
},
|
|
83
|
+
spaceAroundOperators: {
|
|
84
|
+
type: "boolean",
|
|
85
|
+
default: true,
|
|
86
|
+
legacyName: "wolframSpaceAroundOperators",
|
|
87
|
+
description: "Insert spaces around infix operators.",
|
|
88
|
+
},
|
|
89
|
+
alignRuleValues: {
|
|
90
|
+
type: "boolean",
|
|
91
|
+
default: false,
|
|
92
|
+
legacyName: "wolframAlignRuleValues",
|
|
93
|
+
description:
|
|
94
|
+
"Align Rule and RuleDelayed values vertically in multiline argument, list, and association layouts.",
|
|
95
|
+
},
|
|
96
|
+
documentationCommentColumn: {
|
|
97
|
+
type: "int",
|
|
98
|
+
default: 0,
|
|
99
|
+
minimum: 0,
|
|
100
|
+
legacyName: "wolframDocumentationCommentColumn",
|
|
101
|
+
description:
|
|
102
|
+
"Column for trailing documentation comments. 0 = auto-compute per contiguous block.",
|
|
103
|
+
},
|
|
104
|
+
documentationCommentPadding: {
|
|
105
|
+
type: "int",
|
|
106
|
+
default: 2,
|
|
107
|
+
minimum: 1,
|
|
108
|
+
legacyName: "wolframDocumentationCommentPadding",
|
|
109
|
+
description:
|
|
110
|
+
"Minimum spaces between code and an aligned trailing documentation comment in auto mode.",
|
|
111
|
+
},
|
|
112
|
+
topLevelSpacingMode: {
|
|
113
|
+
type: "string",
|
|
114
|
+
default: "declarations",
|
|
115
|
+
enum: ["declarations", "all", "none"],
|
|
116
|
+
markdownEnumDescriptions: [
|
|
117
|
+
"Apply definition spacing only to adjacent declarations; preserve ordinary code spacing up to maxBlankLinesBetweenCode.",
|
|
118
|
+
"Apply top-level spacing to all statements.",
|
|
119
|
+
"Remove top-level blank lines.",
|
|
120
|
+
],
|
|
121
|
+
legacyName: "wolframTopLevelSpacingMode",
|
|
122
|
+
description: "Top-level spacing policy: declarations, all, or none.",
|
|
123
|
+
},
|
|
124
|
+
preserveTildeInfixFunctions: {
|
|
125
|
+
type: "string",
|
|
126
|
+
default: "",
|
|
127
|
+
legacyName: "wolframPreserveTildeInfixFunctions",
|
|
128
|
+
description:
|
|
129
|
+
"Comma-separated function names that stay in ~f~ infix form instead of normalizing to f[x, y].",
|
|
130
|
+
},
|
|
131
|
+
moduleVarsBreakThreshold: {
|
|
132
|
+
type: "int",
|
|
133
|
+
default: 40,
|
|
134
|
+
minimum: 0,
|
|
135
|
+
legacyName: "wolframModuleVarsBreakThreshold",
|
|
136
|
+
description:
|
|
137
|
+
"Character count at which Module/With/Block var list breaks.",
|
|
138
|
+
},
|
|
139
|
+
conditionFirstFunctions: {
|
|
140
|
+
type: "string",
|
|
141
|
+
default: "If,Switch",
|
|
142
|
+
legacyName: "wolframConditionFirstFunctions",
|
|
143
|
+
description:
|
|
144
|
+
"Comma-separated symbols whose first arg stays on same line as head.",
|
|
145
|
+
},
|
|
146
|
+
blockStructureFunctions: {
|
|
147
|
+
type: "string",
|
|
148
|
+
default: "Module,With,Block,DynamicModule",
|
|
149
|
+
legacyName: "wolframBlockStructureFunctions",
|
|
150
|
+
description:
|
|
151
|
+
"Comma-separated symbols using block-structure formatting.",
|
|
152
|
+
},
|
|
153
|
+
caseStructureFunctions: {
|
|
154
|
+
type: "string",
|
|
155
|
+
default: "Which",
|
|
156
|
+
legacyName: "wolframCaseStructureFunctions",
|
|
157
|
+
description:
|
|
158
|
+
"Comma-separated symbols using alternating condition/body indentation.",
|
|
159
|
+
},
|
|
160
|
+
lintRules: {
|
|
161
|
+
type: "string",
|
|
162
|
+
default: "{}",
|
|
163
|
+
legacyName: "wolframLintRules",
|
|
164
|
+
description:
|
|
165
|
+
'JSON object of per-rule level overrides, e.g. {"prefer-rule-delayed":"error"}.',
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const legacyWolframOptions = Object.fromEntries(
|
|
170
|
+
Object.entries(wolframOptions).map(([name, definition]) => [
|
|
171
|
+
definition.legacyName,
|
|
172
|
+
{
|
|
173
|
+
...wolframOptionDefinition(definition),
|
|
174
|
+
deprecated: `Use wolfram.${name} instead.`,
|
|
175
|
+
description: `${definition.description} Deprecated: use wolfram.${name}.`,
|
|
176
|
+
},
|
|
177
|
+
]),
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
export function normalizeWolframOptions(inputOptions = {}) {
|
|
181
|
+
if (!isPlainObject(inputOptions)) return inputOptions ?? {};
|
|
182
|
+
|
|
183
|
+
const nestedOptions = isPlainObject(inputOptions.wolfram)
|
|
184
|
+
? inputOptions.wolfram
|
|
185
|
+
: {};
|
|
186
|
+
let normalized = inputOptions;
|
|
187
|
+
|
|
188
|
+
for (const [name, definition] of Object.entries(wolframOptions)) {
|
|
189
|
+
if (!hasOwn(nestedOptions, name)) continue;
|
|
190
|
+
if (normalized === inputOptions) normalized = { ...inputOptions };
|
|
191
|
+
normalized[definition.legacyName] = nestedOptions[name];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return normalized;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export const options = {
|
|
198
|
+
wolfram: {
|
|
199
|
+
type: "string",
|
|
200
|
+
category: "Wolfram",
|
|
201
|
+
default: {},
|
|
202
|
+
description: "Wolfram formatter options object.",
|
|
203
|
+
exception: (value) => value == null || isPlainObject(value),
|
|
204
|
+
},
|
|
205
|
+
...legacyWolframOptions,
|
|
206
|
+
};
|