-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
92 lines (87 loc) · 2.45 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const { parseLines, stringify } = require('dot-properties')
const { hardline } = require('prettier').doc.builders
const { languages } = require('./languages')
const parser = {
parse: text => parseLines(text, true),
astFormat: 'dot-properties',
hasPragma: text => /^\s*[#!][ \t\f]*@(pragma|format)\b/.test(text),
locStart(node) {
// istanbul ignore if
if (Array.isArray(node)) node = node[0]
return node.range[0]
},
locEnd(node) {
// istanbul ignore if
if (Array.isArray(node)) node = node[node.length - 1]
return node.range[node.range.length - 1]
}
}
const printer = {
print(path, options, print) {
const node = path.getValue()
if (Array.isArray(node)) {
const result = path.map(astPath => {
// This print method does not get called on the nodes that have a
// # prettier-ignore comment above them. So we need to return them
// with a hardline here.
if (this.hasPrettierIgnore(astPath)) {
return [print(astPath), hardline]
}
return print(astPath)
})
return result
}
const opt = {
indent: options.useTabs ? '\t' : ' '.repeat(options.tabWidth),
keySep: options.keySeparator,
latin1: options.escapeNonLatin1,
lineWidth: options.printWidth
}
return [stringify([node], opt), hardline]
},
hasPrettierIgnore(path) {
const node = path.getValue()
if (Array.isArray(node)) return false
const tokens = path.stack[0]
// istanbul ignore else
if (Array.isArray(tokens)) {
const prev = tokens[tokens.indexOf(node) - 1]
return (
prev &&
prev.type === 'COMMENT' &&
/^\s*[#!][ \t\f]*prettier-ignore/.test(prev.comment)
)
} else return false
},
insertPragma: text => `# @format\n${text}`
}
const options = {
escapeNonLatin1: {
category: 'Format',
type: 'boolean',
description:
'Escape with \\u all non-Latin-1 characters, to allow safely encoding as ISO-8859-1',
default: false
},
keySeparator: {
category: 'Format',
type: 'choice',
description: 'The separator to use between a property key and its value',
default: ' = ',
choices: [
{ value: ' ' },
{ value: ':' },
{ value: '=' },
{ value: ': ' },
{ value: '= ' },
{ value: ' : ' },
{ value: ' = ' }
]
}
}
module.exports = {
languages,
parsers: { 'dot-properties': parser },
printers: { 'dot-properties': printer },
options
}