-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathindex.js
491 lines (454 loc) · 13.5 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* eslint no-console:0, import/default:0 */
import path from 'path';
import fsMock from 'fs';
import stripIndent from 'strip-indent';
import eslintMock from 'eslint';
import prettierMock from 'prettier';
import loglevelMock from 'loglevel-colored-level-prefix';
import {format, analyze} from '../';
jest.mock('fs');
const {
mock: { logger }
} = loglevelMock;
// loglevelMock.mock.logThings = ['debug']
const tests = [
{
title: 'sanity test',
input: {
text: defaultInputText(),
eslintConfig: getESLintConfigWithDefaultRules()
},
output: defaultOutput()
},
{
title: 'README example',
input: {
text: 'const {foo} = bar',
eslintConfig: {
parserOptions: { ecmaVersion: 7 },
rules: { semi: ['error', 'never'] }
},
prettierOptions: { bracketSpacing: true }
},
output: 'const { foo } = bar'
},
{
// this one's actually hard to test now. This test doesn't
// really do too much. Before, when prettier didn't support
// semi, it was easy to tell based on the presence of the
// semicolon. Now prettier removes the semicolon so I'm
// honestly not sure how to test that prettier fixed
// something that eslint fixed
title: 'with prettierLast: true',
input: {
text: defaultInputText(),
filePath: path.resolve('./mock/default-config.js'),
prettierLast: true
},
output: prettierLastOutput()
},
{
title: 'with a filePath and no config',
input: {
text: defaultInputText(),
filePath: path.resolve('./mock/default-config.js')
},
output: defaultOutput()
},
{
title: 'with a default config and overrides',
input: {
text: 'const { foo } = bar;',
eslintConfig: {
// Won't be overridden
parserOptions: {
ecmaVersion: 7
},
rules: {
// Will be overridden
semi: ['error', 'always'],
// Won't be overridden
'object-curly-spacing': ['error', 'never']
}
},
filePath: path.resolve('./mock/default-config.js')
},
output: 'const {foo} = bar'
},
{
title: 'with an empty config and fallbacks',
input: {
text: 'const { foo } = bar;',
eslintConfig: {},
filePath: path.resolve('./mock/default-config.js'),
fallbackPrettierOptions: { bracketSpacing: false }
},
output: 'const {foo} = bar'
},
{
title: 'without a filePath and no config',
input: { text: defaultInputText() },
output: noopOutput()
},
{
title: 'inferring bracketSpacing',
input: {
text: 'var foo = {bar: baz};',
eslintConfig: { rules: { 'object-curly-spacing': ['error', 'always'] } }
},
output: 'var foo = { bar: baz };'
},
{
title: 'inferring bracketSpacing with eslint object-curly-spacing options',
input: {
text: 'var foo = {bar: {baz: qux}};\nvar fop = {bar: [1, 2, 3]};',
eslintConfig: {
rules: {
'object-curly-spacing': [
'error',
'always',
{ objectsInObjects: false, arraysInObjects: false }
]
}
}
},
output: 'var foo = { bar: { baz: qux }};\nvar fop = { bar: [1, 2, 3]};'
},
{
title: 'with a filePath-aware config',
input: {
text: 'var x = 0;',
eslintConfig: {
rules: { 'no-var': 'error' },
ignorePattern: 'should-be-ignored'
},
filePath: path.resolve('should-be-ignored.js')
},
output: 'var x = 0;'
},
// if you have a bug report or something,
// go ahead and add a test case here
{
title: 'with code that needs no fixing',
input: {
text: 'var [foo, { bar }] = window.APP;',
eslintConfig: { rules: {} }
},
output: 'var [foo, { bar }] = window.APP;'
},
{
title: 'accepts config globals as array',
input: {
text: defaultInputText(),
eslintConfig: { globals: ['window:writable'] }
},
output: noopOutput()
},
{
title: 'CSS example',
input: {
text: '.stop{color:red};',
filePath: path.resolve('./test.css')
},
output: '.stop {\n color: red;\n}'
},
{
title: 'LESS example',
input: {
text: '.stop{color:red};',
filePath: path.resolve('./test.less')
},
output: '.stop {\n color: red;\n}'
},
{
title: 'SCSS example',
input: {
text: '.stop{color:red};',
filePath: path.resolve('./test.scss')
},
output: '.stop {\n color: red;\n}'
},
{
title: 'TypeScript example',
input: {
text: 'function Foo (this: void) { return this; }',
filePath: path.resolve('./test.ts')
},
output: 'function Foo(this: void) {\n return this;\n}'
},
{
title: 'Vue.js example',
input: {
eslintConfig: {
rules: {
'space-before-function-paren': [2, 'always']
}
},
text: '<template>\n <div></div>\n</template>\n<script>\nfunction foo() { return "foo" }\n</script>\n<style>\n</style>',
filePath: path.resolve('./test.vue')
},
output:
'<template>\n <div></div>\n</template>\n<script>\nfunction foo () {\n return "foo";\n}\n</script>\n<style></style>'
},
{
title: 'GraphQL example',
input: {
text: 'type Query{test: Test}',
filePath: path.resolve('./test.gql')
},
output: 'type Query {\n test: Test\n}'
},
{
title: 'JSON example',
input: {
text: '{ "foo": "bar"}',
filePath: path.resolve('./test.json')
},
output: '{ "foo": "bar" }'
},
{
title: 'Markdown example',
input: {
text: '# Foo\n _bar_',
filePath: path.resolve('./test.md')
},
output: '# Foo\n\n_bar_'
},
{
title: 'Test eslintConfig.globals as an object',
input: {
text: 'var foo = { "bar": "baz"}',
eslintConfig: {
globals: {
someGlobal: true
}
}
},
output: 'var foo = { bar: "baz" };'
}
];
beforeEach(() => {
eslintMock.mock.lintText.mockClear();
eslintMock.mock.calculateConfigForFile.mockClear();
prettierMock.format.mockClear();
prettierMock.resolveConfig.mockClear();
fsMock.readFileSync.mockClear();
loglevelMock.mock.clearAll();
global.__PRETTIER_ESLINT_TEST_STATE__ = {};
});
tests.forEach(({ title, modifier, input, output }) => {
let fn = test;
if (modifier) {
fn = test[modifier];
}
fn(title, async () => {
input.text = stripIndent(input.text).trim();
const expected = stripIndent(output).trim();
const actual = await format(input);
// adding the newline in the expected because
// prettier adds a newline to the end of the input
expect(actual).toBe(`${expected}\n`);
});
});
test('analyze returns the messages', async () => {
const text = 'var x = 0;';
const result = await analyze({
text,
eslintConfig: {
rules: { 'no-var': 'error' }
}
})
expect(result.output).toBe(`${text}\n`);
expect(result.messages).toHaveLength(1);
const msg = result.messages[0];
expect(msg.ruleId).toBe('no-var');
expect(msg.column).toBe(1);
expect(msg.endColumn).toBe(11);
})
test('failure to fix with eslint throws and logs an error', async () => {
const { lintText } = eslintMock.mock;
const error = new Error('Something happened');
lintText.throwError = error;
await expect(() => format({ text: '' })).rejects.toThrowError(error);
expect(logger.error).toHaveBeenCalledTimes(1);
lintText.throwError = null;
});
test('logLevel is used to configure the logger', async () => {
logger.setLevel = jest.fn();
await format({ text: '', logLevel: 'silent' });
expect(logger.setLevel).toHaveBeenCalledTimes(1);
expect(logger.setLevel).toHaveBeenCalledWith('silent');
});
test('when prettier throws, log to logger.error and throw the error', async () => {
const error = new Error('something bad happened');
prettierMock.format.throwError = error;
await expect(() => format({ text: '' })).rejects.toThrowError(error);
expect(logger.error).toHaveBeenCalledTimes(1);
prettierMock.format.throwError = null;
});
test('can accept a path to an eslint module and uses that instead.', async () => {
const eslintPath = path.join(__dirname, '../__mocks__/eslint');
await format({ text: '', eslintPath });
expect(eslintMock.mock.lintText).toHaveBeenCalledTimes(1);
});
test('fails with an error if the eslint module cannot be resolved.', async () => {
const eslintPath = path.join(
__dirname,
'../__mocks__/non-existent-eslint-module'
);
await expect(() => format({ text: '', eslintPath })).rejects.toThrowError(
/non-existent-eslint-module/
);
expect(logger.error).toHaveBeenCalledTimes(1);
const errorString = expect.stringMatching(
/trouble getting.*?eslint.*non-existent-eslint-module/
);
expect(logger.error).toHaveBeenCalledWith(errorString);
});
test('can accept a path to a prettier module and uses that instead.', async () => {
const prettierPath = path.join(__dirname, '../__mocks__/prettier');
await format({ text: '', prettierPath });
expect(prettierMock.format).toHaveBeenCalledTimes(1);
});
test('fails with an error if the prettier module cannot be resolved.', async () => {
const prettierPath = path.join(
__dirname,
'../__mocks__/non-existent-prettier-module'
);
await expect(() => format({ text: '', prettierPath })).rejects.toThrowError(
/non-existent-prettier-module/
);
expect(logger.error).toHaveBeenCalledTimes(1);
const errorString = expect.stringMatching(
/trouble getting.*?eslint.*non-existent-prettier-module/
);
expect(logger.error).toHaveBeenCalledWith(errorString);
});
test('resolves to the eslint module relative to the given filePath', async () => {
const filePath = require.resolve('../../tests/fixtures/paths/foo.js');
await format({ text: '', filePath });
const stateObj = {
eslintPath: require.resolve(
'../../tests/fixtures/paths/node_modules/eslint/index.js'
),
prettierPath: require.resolve(
'../../tests/fixtures/paths/node_modules/prettier/index.js'
)
};
expect(global.__PRETTIER_ESLINT_TEST_STATE__).toMatchObject(stateObj);
});
test('resolves to the local eslint module', async () => {
const filePath = '/blah-blah/default-config.js';
await format({ text: '', filePath });
expect(global.__PRETTIER_ESLINT_TEST_STATE__).toMatchObject({
// without Jest's mocking, these would actually resolve to the
// project modules :) The fact that jest's mocking is being
// applied is good enough for this test.
eslintPath: require.resolve('../__mocks__/eslint'),
prettierPath: require.resolve('../__mocks__/prettier')
});
});
test('reads text from fs if filePath is provided but not text', async () => {
const readFileSyncMockSpy = jest.spyOn(fsMock, 'readFileSync');
const filePath = '/blah-blah/some-file.js';
await format({ filePath });
expect(readFileSyncMockSpy).toHaveBeenCalledWith(filePath, 'utf8');
});
test('logs error if it cannot read the file from the filePath', async () => {
const originalMock = fsMock.readFileSync;
fsMock.readFileSync = jest.fn(() => {
throw new Error('some error');
});
await expect(() =>
format({ filePath: '/some-path.js' })
).rejects.toThrowError(/some error/);
expect(logger.error).toHaveBeenCalledTimes(1);
fsMock.readFileSync = originalMock;
});
test('calls prettier.resolveConfig with the file path', async () => {
const filePath = require.resolve('../../tests/fixtures/paths/foo.js');
await format({
filePath,
text: defaultInputText(),
eslintConfig: getESLintConfigWithDefaultRules()
});
expect(prettierMock.resolveConfig).toHaveBeenCalledTimes(1);
expect(prettierMock.resolveConfig).toHaveBeenCalledWith(filePath);
});
test('does not raise an error if prettier.resolveConfig is not defined', async () => {
const filePath = require.resolve('../../tests/fixtures/paths/foo.js');
const originalPrettierMockResolveConfig = prettierMock.resolveConfig;
prettierMock.resolveConfig = undefined;
async function callingFormat() {
return format({
filePath,
text: defaultInputText(),
eslintConfig: getESLintConfigWithDefaultRules()
});
}
await expect(callingFormat).not.toThrowError();
prettierMock.resolveConfig = originalPrettierMockResolveConfig;
});
test('logs if there is a problem making the CLIEngine', async () => {
const error = new Error('fake error');
eslintMock.ESLint.mockImplementation(() => {
throw error;
});
await expect(() => format({ text: '' })).rejects.toThrowError(error);
eslintMock.ESLint.mockReset();
expect(logger.error).toHaveBeenCalledTimes(1);
});
function getESLintConfigWithDefaultRules(overrides) {
return {
parserOptions: { ecmaVersion: 7 },
rules: {
semi: [2, 'never'],
'max-len': [2, 120, 2],
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single', { avoidEscape: true, allowTemplateLiterals: true }],
'comma-dangle': [
2,
{
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'always-multiline'
}
],
'arrow-parens': [2, 'as-needed'],
...overrides
}
};
}
function defaultInputText() {
return `
function foo (){ // stuff
console.log( "Hello world!", and, stuff );
}
`;
}
function noopOutput() {
return `
function foo() {
// stuff
console.log("Hello world!", and, stuff);
}
`;
}
function defaultOutput() {
return `
function foo() {
// stuff
console.log('Hello world!', and, stuff)
}
`;
}
function prettierLastOutput() {
return `
function foo() {
// stuff
console.log('Hello world!', and, stuff)
}
`;
}