/
prettier.js
301 lines (277 loc) · 7.95 KB
/
prettier.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* @fileoverview Runs `prettier` as an ESLint rule.
* @author Andres Suarez
*/
'use strict';
// This test is optimized for debuggability.
// Please do not attempt to DRY this file or dynamically load the fixtures.
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const fs = require('fs');
const path = require('path');
const eslintPluginPrettier = require('..');
const rule = eslintPluginPrettier.rules.prettier;
const assert = require('assert');
const { ESLint, RuleTester } = require('eslint');
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
const eslint = new ESLint({
baseConfig: {
parserOptions: {
ecmaVersion: 2021,
ecmaFeatures: {
jsx: true,
},
sourceType: 'module',
},
extends: 'plugin:prettier/recommended',
overrides: [
{
files: '*.mdx',
extends: 'plugin:mdx/recommended',
settings: {
'mdx/code-block': true,
},
},
],
},
useEslintrc: false,
ignore: false,
});
const ruleTester = new RuleTester();
ruleTester.run('prettier', rule, {
valid: [
// Correct style. Also proves that the plugin works if no filename is provided
{ code: `'';\n` },
// Double quote from .prettierrc.
{ code: '"";\n', filename: getPrettierRcJsFilename('double-quote') },
// Override .prettierrc from object option.
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }],
},
// Only use options from plugin, skipping .prettierrc
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }, { usePrettierrc: false }],
},
// Ignores filenames in .prettierignore
{
code: `("");\n`,
filename: getPrettierRcJsFilename('single-quote', 'ignore-me.js'),
},
// Sets a default parser when it can't be inferred from the file extensions
{
code: `('');\n`,
filename: getPrettierRcJsFilename('single-quote', 'dummy.qqq'),
},
// Overwrites the parser for file extensions prettier would try to format
// with not the babylon parser
// In the real world, eslint-plugin-markdown would transform file contents
// into JS snippets that would get passed to ESLint
{
code: `('');\n`,
filename: getPrettierRcJsFilename('single-quote', 'dummy.md'),
},
// Should ignore files from node_modules
{
code: 'a();;;;;;\n',
filename: 'node_modules/dummy.js',
},
],
invalid: [
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11-a',
'11-b',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
].map(loadInvalidFixture),
});
const vueRuleTester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
});
vueRuleTester.run('vue', rule, {
valid: [
{
code: `<template>\n <div>HI</div>\n</template>\n<script>\n3;\n</script>\n`,
filename: 'valid.vue',
},
],
invalid: [
Object.assign(loadInvalidFixture('vue'), {
filename: 'invalid.vue',
}),
Object.assign(loadInvalidFixture('vue-syntax-error'), {
filename: 'syntax-error.vue',
}),
],
});
const atGraphqlEslintRuleTester = new RuleTester({
parser: require.resolve('@graphql-eslint/eslint-plugin'),
});
atGraphqlEslintRuleTester.run('@graphql-eslint/eslint-plugin', rule, {
valid: [
{
code: `type Query {\n foo: String!\n}\n`,
filename: 'valid.graphql',
},
],
invalid: [
Object.assign(loadInvalidFixture('graphql'), {
filename: 'invalid.graphql',
}),
],
});
// eslint-plugin-graphql handles literal graphql files by tranforming graphql
// code with a processor, instead of using a parser. Unfortunatly we cant
// specify custom processors in a RuleTester, so instead we have write test code
// that is the result of eslint-plugin-graphql's processing - this is the
// ESLintPluginGraphQLFile tagged template literal. See
// https://github.com/apollographql/eslint-plugin-graphql/blob/c465fedc8fea239ee1731ad4ec3ee1183a3cdddf/src/index.js#L404
// In the future if ESLint supports processors (https://github.com/eslint/rfcs/pull/31)
// we should be define a RuleTester like
// `newRuleTester({processor: require('eslint-plugin-graphql').processor['.graphql']})
// and then pass in pure graphql into the code value.
const eslintPluginGraphqlRuleTester = new RuleTester({
parserOptions: { ecmaVersion: 2015 },
});
eslintPluginGraphqlRuleTester.run('eslint-plugin-graphql', rule, {
valid: [
{
code: 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n',
filename: getPrettierRcJsFilename('no-semi', 'dummy.graphql'),
},
],
invalid: [],
});
const mdxRuleTester = new RuleTester({
parser: require.resolve('eslint-mdx'),
parserOptions: {
...require('eslint-mdx').DEFAULT_PARSER_OPTIONS,
// mdx-js/eslint-mdx#366
ecmaVersion: 2021,
},
});
mdxRuleTester.run('eslint-plugin-mdx', rule, {
valid: [
{
code: [
"import React from 'react';",
'',
'<div>Hello World</div>',
'',
].join('\n'),
filename: 'valid.mdx',
},
],
invalid: [
Object.assign(loadInvalidFixture('mdx'), {
filename: 'invalid.mdx',
}),
],
});
runFixture('mdx', [
[
{
column: 33,
endColumn: 33,
endLine: 1,
fix: {
range: [32, 32],
text: ';',
},
line: 1,
message: 'Insert `;`',
messageId: 'insert',
nodeType: null,
ruleId: 'prettier/prettier',
severity: 2,
},
{
column: 27,
endColumn: 27,
endLine: 6,
fix: {
range: [91, 91],
text: ';',
},
line: 6,
message: 'Insert `;`',
messageId: 'insert',
nodeType: null,
ruleId: 'prettier/prettier',
severity: 2,
},
],
]);
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Reads a fixture file and returns an "invalid" case for use by RuleTester.
* The fixture format aims to reduce the pain of debugging offsets by keeping
* the lines and columns of the test code as close to what the rule will report
* as possible.
* @param {string} name - Fixture basename.
* @returns {Object} A {code, output, options, errors} test object.
*/
function loadInvalidFixture(name) {
const filename = path.join(__dirname, 'invalid', name + '.txt');
const src = fs.readFileSync(filename, 'utf8');
const sections = src
.split(/^[A-Z]+:\n/m)
.map((x) => x.replace(/(?=\n)\n$/, ''));
const item = {
code: sections[1],
output: sections[2],
options: eval(sections[3]), // eslint-disable-line no-eval
errors: eval(sections[4]), // eslint-disable-line no-eval
filename: getPrettierRcJsFilename('double-quote', name + '.txt'),
};
if (sections.length >= 6) {
item.filename = sections[5];
}
return item;
}
/**
* Builds a dummy javascript file path to trick prettier into resolving a specific .prettierrc file.
* @param {string} dir - Prettierrc fixture basename.
* @returns {string} A javascript filename relative to the .prettierrc config.
*/
function getPrettierRcJsFilename(dir, file = 'dummy.js') {
return path.resolve(__dirname, `./prettierrc/${dir}/${file}`);
}
function runFixture(name, asserts) {
return eslint
.lintFiles(`test/fixtures/${name}.*`)
.then((results) =>
assert.deepStrictEqual(
asserts,
results.map(({ messages }) => messages)
)
)
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
// eslint-disable-next-line no-process-exit
process.exit(1);
});
}