-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
75 lines (59 loc) · 1.68 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { vue: vueParser } = require('prettier/parser-html').parsers
const excludeDefaults = ['style']
const excludeNodeType = 'vue-excluded-content'
let defaultHtmlPrinter
const pluginOptions = {
vueExcludeBlocks: {
type: 'string',
category: 'Vue',
array: true,
default: [{ value: excludeDefaults }],
description: 'Ignore this blocks while formatting Vue SFC files.',
},
}
function printOriginal(node, options) {
const { originalText } = options
const { start, end } = node.sourceSpan
return originalText.slice(start.offset, end.offset)
}
function collapseChildren({ children }) {
if (!children.length) {
return children
}
const { start } = children[0].sourceSpan
const { end } = children[children.length - 1].sourceSpan
return [
{
sourceSpan: { start, end },
type: excludeNodeType,
},
]
}
function extendPrinter(defaultPrint) {
return (path, options, print) => {
const { vueExcludeBlocks } = options
const node = path.getValue()
if (node.parent && node.parent.type === 'root' && vueExcludeBlocks.includes(node.name)) {
node.children = collapseChildren(node)
}
if (node.type === excludeNodeType) {
return printOriginal(node, options).trim()
}
return defaultPrint(path, options, print)
}
}
function preprocess(text, options) {
if (!defaultHtmlPrinter) {
defaultHtmlPrinter = options.plugins.find(({ printers }) => printers.html).printers.html
if (defaultHtmlPrinter) {
defaultHtmlPrinter.print = extendPrinter(defaultHtmlPrinter.print)
}
}
return text
}
module.exports = {
parsers: {
vue: Object.assign(vueParser, { preprocess }),
},
options: pluginOptions,
}