prettify-bru 1.9.1 → 1.9.2
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/lib/format.mjs +24 -7
- package/package.json +1 -1
package/lib/format.mjs
CHANGED
|
@@ -25,6 +25,9 @@ const formattableBlocks = [
|
|
|
25
25
|
'tests',
|
|
26
26
|
]
|
|
27
27
|
|
|
28
|
+
const maskPattern = /("(?:\\.|[^"\\])*")|(\{\{.*?\}\})/g
|
|
29
|
+
const unmaskPattern = /"__⇎⇎START__(\{\{.*?\}\})__END⇎⇎__"/g
|
|
30
|
+
|
|
28
31
|
/**
|
|
29
32
|
* @typedef {Object} FileOutcome
|
|
30
33
|
* @property {string} newContents
|
|
@@ -140,8 +143,12 @@ async function formatBlock(fileContents, blockName, config) {
|
|
|
140
143
|
if (blockName === 'body:graphql') {
|
|
141
144
|
opts.parser = 'graphql'
|
|
142
145
|
opts.bracketSpacing = true
|
|
146
|
+
unindented = wrapNonStringPlaceholdersInDelimiters(unindented)
|
|
143
147
|
}
|
|
144
148
|
reformatted = await prettier.format(unindented, opts)
|
|
149
|
+
if (blockName === 'body:graphql') {
|
|
150
|
+
reformatted = unwrapDelimitedPlaceholders(reformatted)
|
|
151
|
+
}
|
|
145
152
|
} catch (e) {
|
|
146
153
|
outcome.errorMessage = `Prettier could not format ${blockName} because...\n${e.message}`
|
|
147
154
|
return outcome
|
|
@@ -189,23 +196,33 @@ function shortenGetters(blockContents) {
|
|
|
189
196
|
}
|
|
190
197
|
|
|
191
198
|
/**
|
|
192
|
-
* Turns Bruno variable placeholders into strings with special delimiters, effectively making it valid JSON
|
|
199
|
+
* Turns Bruno variable placeholders into strings with special delimiters, effectively making it valid JSON,
|
|
200
|
+
* and for standard cases also valid GraphQL.
|
|
193
201
|
*
|
|
194
|
-
* @param {string}
|
|
202
|
+
* @param {string} block
|
|
195
203
|
* @returns {string}
|
|
196
204
|
*/
|
|
197
|
-
function wrapNonStringPlaceholdersInDelimiters(
|
|
198
|
-
return
|
|
205
|
+
function wrapNonStringPlaceholdersInDelimiters(block) {
|
|
206
|
+
return block.replace(maskPattern, (match, group1, group2) => {
|
|
207
|
+
// If group1 exists, we found a standard JSON string. Return it untouched.
|
|
208
|
+
if (group1) {
|
|
209
|
+
return group1
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// If group2 exists, we found a placeholder outside a string.
|
|
213
|
+
// Wrap it in a unique dummy string.
|
|
214
|
+
return `"__⇎⇎START__${group2}__END⇎⇎__"`
|
|
215
|
+
})
|
|
199
216
|
}
|
|
200
217
|
|
|
201
218
|
/**
|
|
202
219
|
* Reverts delimited Bruno variable placeholders back to their original form within a JSON block.
|
|
203
220
|
*
|
|
204
|
-
* @param {string}
|
|
221
|
+
* @param {string} block
|
|
205
222
|
* @returns {string}
|
|
206
223
|
*/
|
|
207
|
-
function unwrapDelimitedPlaceholders(
|
|
208
|
-
return
|
|
224
|
+
function unwrapDelimitedPlaceholders(block) {
|
|
225
|
+
return block.replace(unmaskPattern, '$1')
|
|
209
226
|
}
|
|
210
227
|
|
|
211
228
|
/**
|