-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
parser.js
188 lines (176 loc) · 5.22 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
import path from 'path'
import getLogger from 'loglevel-colored-level-prefix'
import findUp from 'find-up'
import yargs from 'yargs'
import {oneLine, stripIndent} from 'common-tags'
import arrify from 'arrify'
const logger = getLogger({prefix: 'prettier-eslint-cli'})
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',
},
'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)
`,
},
'list-different': {
default: false,
type: 'boolean',
describe: oneLine`
Print filenames of files that are different
from Prettier + Eslint formatting.
`,
},
// allow `--eslint-path` and `--eslintPath`
'eslint-path': {
default: getPathInHostNodeModules('eslint'),
describe: 'The path to the eslint module to use',
coerce: coercePath,
},
// allow `--prettier-path` and `--prettierPath`
'prettier-path': {
describe: 'The path to the prettier module to use',
default: getPathInHostNodeModules('prettier'),
coerce: coercePath,
},
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': {
default: false,
type: 'boolean',
describe: 'Indent lines with tabs instead of spaces.',
},
'print-width': {
default: 80,
type: 'number',
describe: 'Specify the length of line that the printer will wrap on.',
},
'tab-width': {
default: 2,
type: 'number',
describe: 'Specify the number of spaces per indentation-level.',
},
'trailing-comma': {
default: 'none',
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)
`,
},
'bracket-spacing': {
default: true,
type: 'boolean',
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': {
default: false,
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: {
default: 'babylon',
type: 'string',
describe: 'Specify which parser to use.',
},
semi: {
default: true,
type: 'boolean',
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': {
default: false,
type: 'boolean',
describe: 'Use single quotes instead of double quotes.',
},
// TODO: support range-start and range-end
// would require changes in prettier-eslint
// TODO: if we allow people to to specify a config path,
// we need to read that somehow. These can come invarious
// formats and we'd have to work out `extends` somehow as well.
// I don't know whether ESLint exposes a way to do this...
// Contributions welcome!
// eslintConfigPath: {
// describe: 'Path to the eslint config to use for eslint --fix',
// },
})
.strict()
export default parser
function getPathInHostNodeModules(module) {
logger.debug(`Looking for a local installation of the module "${module}"`)
const modulePath = findUp.sync(`node_modules/${module}`)
if (modulePath) {
return modulePath
}
logger.debug(
oneLine`
Local installation of "${module}" not found,
looking again starting in "${__dirname}"
`,
)
return findUp.sync(`node_modules/${module}`, {cwd: __dirname})
}
function coercePath(input) {
return path.isAbsolute(input) ? input : path.join(process.cwd(), input)
}