-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
parser.js
235 lines (225 loc) · 6.67 KB
/
parser.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import path from 'path';
import yargs from 'yargs';
import { oneLine, stripIndent } from 'common-tags';
import arrify from 'arrify';
const parser = yargs
.usage(
stripIndent`
Usage: $0 <globs>... [--option-1 option-1-value --option-2]
Prefix an option with "no-" to set it to false, such as --no-semi to
disable semicolons and --no-eslint-ignore to disable default ignores.
`
)
.help('h')
.alias('h', 'help')
.version()
.options({
write: {
default: false,
describe: 'Edit the file in-place (beware!)',
type: 'boolean'
},
stdin: {
default: false,
describe: 'Read input via stdin',
type: 'boolean'
},
'stdin-filepath': {
describe: 'Path to the file to pretend that stdin comes from.',
coerce: coercePath
},
'eslint-ignore': {
default: true,
type: 'boolean',
describe: oneLine`
Only format matching files even if
they are not ignored by .eslintignore.
(can use --no-eslint-ignore to disable this)
`
},
'prettier-ignore': {
default: true,
type: 'boolean',
describe: oneLine`
Only format matching files even if
they are not ignored by .prettierignore.
(can use --no-prettier-ignore to disable this)
`
},
'list-different': {
default: false,
type: 'boolean',
describe: oneLine`
Print filenames of files that are different
from Prettier + Eslint formatting.
`
},
includeDotFiles: {
default: false,
type: 'boolean',
describe: oneLine`
Include files that start with a dot in the search.
`
},
// allow `--eslint-path` and `--eslintPath`
'eslint-path': {
describe: 'The path to the eslint module to use',
coerce: coercePath
},
// allow `--eslint-config-path` and `--eslintConfigPath`
'eslint-config-path': {
default: undefined,
describe: 'Path to the eslint config to use for eslint --fix'
},
// allow `--prettier-path` and `--prettierPath`
'prettier-path': {
describe: 'The path to the prettier module to use',
coerce: coercePath
},
config: {
default: undefined,
describe: 'Path to the prettier config'
},
ignore: {
describe: oneLine`
pattern(s) you wish to ignore
(can be used multiple times
and includes **/node_modules/** automatically)
`,
coerce: arrify
},
'log-level': {
describe: 'The log level to use',
choices: ['silent', 'error', 'warn', 'info', 'debug', 'trace'],
alias: 'l',
default: process.env.LOG_LEVEL || 'warn'
},
'prettier-last': {
describe: 'Run prettier last',
default: false,
type: 'boolean'
},
'use-tabs': {
type: 'boolean',
default: undefined,
describe: 'Indent lines with tabs instead of spaces.'
},
'print-width': {
type: 'number',
describe: 'Specify the length of line that the printer will wrap on.'
},
'tab-width': {
type: 'number',
describe: 'Specify the number of spaces per indentation-level.'
},
'trailing-comma': {
type: 'string',
describe: stripIndent`
Print trailing commas wherever possible.
Valid options:
- "none" - no trailing commas
- "es5" - trailing commas where valid in ES5 (objects, arrays, etc)
- "all" - trailing commas wherever possible (function arguments)
`,
choices: ['none', 'es5', 'all']
},
'bracket-spacing': {
type: 'boolean',
default: undefined,
describe: stripIndent`
Print spaces between brackets in object literals.
Can use --no-bracket-spacing for "false" to disable it.
Valid options:
- true - Example: { foo: bar }
- false - Example: {foo: bar}
`
},
'jsx-bracket-same-line': {
type: 'boolean',
describe: oneLine`
Put the > of a multi-line JSX element at
the end of the last line instead of
being alone on the next line
`
},
parser: {
type: 'string',
describe: 'Specify which parser to use.'
},
semi: {
type: 'boolean',
default: undefined,
describe: stripIndent`
Print semicolons at the ends of statements.
Can use --no-semi.
Valid options:
- true - add a semicolon at the end of every statement
- false - ${oneLine`
only add semicolons at the beginning of lines
that may introduce ASI failures
`}
`
},
'single-quote': {
type: 'boolean',
default: undefined,
describe: 'Use single quotes instead of double quotes.'
},
'arrow-parens': {
type: 'string',
default: undefined,
describe: stripIndent`
Include parentheses around a sole arrow function parameter.
Valid options:
- "avoid" - Omit parens when possible. Example: x => x
- "always" - Always include parens. Example: (x) => x
`
},
'require-pragma': {
type: 'boolean',
default: undefined,
describe: stripIndent`
Prettier can restrict itself to only format files
that contain a special comment, called a pragma,
at the top of the file.
This is very useful when gradually transitioning
large, unformatted codebases to prettier.
`
},
'insert-pragma': {
type: 'boolean',
default: undefined,
describe: stripIndent`
Prettier can insert a special @format marker at the
top of files specifying that the file has been
formatted with prettier. This works well when used in
tandem with the --require-pragma option.
If there is already a docblock at the top of the file
then this option will add a newline to it with the
@format marker.
`
},
'prose-wrap': {
type: 'boolean',
default: undefined,
describe: stripIndent`
By default, Prettier will wrap markdown text as-is
since some services use a linebreak-sensitive
renderer, e.g. GitHub comment and BitBucket.
In some cases you may want to rely on
editor/viewer soft wrapping instead, so this
option allows you to opt out with "never".
Valid options:
"always" - Wrap prose if it exceeds the print width.
"never" - Do not wrap prose.
"preserve" - Wrap prose as-is. available in v1.9.0+
`
}
// TODO: support range-start and range-end
// would require changes in prettier-eslint
})
.strict();
export default parser;
function coercePath(input) {
return path.isAbsolute(input) ? input : path.join(process.cwd(), input);
}