-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathprettier.js
204 lines (187 loc) · 5.9 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
/**
* @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 assert = require('assert');
const eslintPluginPrettier = require('..');
const rule = eslintPluginPrettier.rules.prettier;
const RuleTester = require('eslint').RuleTester;
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run('prettier', rule, {
valid: [
// Correct style.
{ code: '"";\n' },
// No pragma = No prettier check.
{ code: '""\n', options: [null, '@format'] },
// Facebook style uses single quotes.
{ code: `('');\n`, options: ['fb'] },
// Facebook style but missing pragma.
{ code: `"";\n`, options: ['fb', '@format'] },
// Facebook style with pragma.
{ code: `/** @format */\n('');\n`, options: ['fb', '@format'] },
// Shebang with pragma.
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] },
// Shebang with pragma from options.
{
code: `#!/bin/node\n/** @format */\n"";\n`,
options: [null, { pragma: '@format' }]
},
// Single quote from .prettierrc.
{ code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') },
// Override .prettierrc from object option.
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }]
},
// Override .prettierrc from facebook option.
{
code: `('');\n`,
filename: getPrettierRcJsFilename('double-quote'),
options: ['fb']
},
// 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')
}
],
invalid: [
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11-a',
'11-b',
'11-c',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19'
].map(loadInvalidFixture)
});
const vueRuleTester = new RuleTester({
parser: require.resolve('vue-eslint-parser')
});
vueRuleTester.run('prettier', rule, {
valid: [
{
code: `<template>foo</template>\n<script>\n"";\n</script>\n`,
filename: 'valid.vue'
}
],
invalid: [
Object.assign(loadInvalidFixture('vue'), {
filename: 'invalid.vue'
})
]
});
describe('showInvisibles', () => {
it('shows invisibles', () => {
assert.equal(
eslintPluginPrettier.showInvisibles('1 2\n3\t4\r5'),
'1·2⏎3↹4␍5'
);
});
});
describe('generateDifferences', () => {
it('operation: insert', () => {
const differences = eslintPluginPrettier.generateDifferences(
'abc',
'abcdef'
);
assert.deepEqual(differences, [
{ operation: 'insert', offset: 3, insertText: 'def' }
]);
});
it('operation: delete', () => {
const differences = eslintPluginPrettier.generateDifferences(
'abcdef',
'abc'
);
assert.deepEqual(differences, [
{ operation: 'delete', offset: 3, deleteText: 'def' }
]);
});
it('operation: replace', () => {
const differences = eslintPluginPrettier.generateDifferences('abc', 'def');
assert.deepEqual(differences, [
{ operation: 'replace', offset: 0, deleteText: 'abc', insertText: 'def' }
]);
});
});
// ------------------------------------------------------------------------------
// 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
};
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) {
// Use default parameters when we drop support for node 4
file = typeof file !== 'undefined' ? file : 'dummy.js';
return path.resolve(__dirname, `./prettierrc/${dir}/${file}`);
}