3 const fs = require('fs');
4 const test = require('ava');
5 const tempWrite = require('temp-write');
6 const stylelint = require('stylelint');
7 const resolveFrom = require('resolve-from');
8 const { format, resolveConfig, getPrettierConfig } = require('./index');
10 const linterAPI = stylelint.createLinter({ fix: true });
12 test('resolveConfig', t =>
13 resolveConfig({ filePath: './fixtures/style.css' }).then(config =>
14 t.deepEqual(config[1], {
16 'string-quotes': ['single'],
17 'indentation': [4, { except: ['value'] }],
18 'color-hex-case': ['upper'],
19 'color-hex-length': ['short'],
20 'block-no-empty': null,
21 'color-no-invalid-hex': [true],
22 'comment-empty-line-before': [
24 { ignore: ['stylelint-commands', 'after-comment'] }
26 'declaration-colon-space-after': ['always'],
27 'max-empty-lines': [2],
28 'rule-empty-line-before': [
31 except: ['first-nested'],
32 ignore: ['after-comment']
35 'unit-whitelist': [['em', 'rem', '%', 's']]
40 test('resolveConfig not found fallback process.cwd', (t) => {
41 const tempPath = tempWrite.sync(
42 'a[id="foo"] { content: "x"; }',
46 return resolveConfig({ filePath: tempPath }).then((config) => {
47 t.is(config[1].rules['function-comma-newline-after'], null);
53 test('resolveConfig shortcircuit ', t =>
54 resolveConfig({ stylelintConfig: { rules: { 'max-line-length': [20] } } }).then((config) => {
55 t.is(config[0].printWidth, 20);
60 test('resolve string quotes === double ', (t) => {
61 const config = resolveConfig.resolve({ rules: { 'string-quotes': ['double'] } });
63 t.is(config[0].singleQuote, undefined);
66 test('resolve indentation === tab', (t) => {
67 const config = resolveConfig.resolve({ rules: { indentation: ['tab'] } });
70 t.is(config[0].useTabs, true);
71 t.is(config[0].tabWidth, 2);
74 test('resolveConfig prettier merge', t =>
76 filePath: './fixtures/style.css',
77 prettierOptions: getPrettierConfig('./fixtures/style.css')
79 t.is(config[0].semi, false);
84 test('format', (t) => {
85 const source = fs.readFileSync('./fixtures/style.css', 'utf8');
89 filePath: './fixtures/style.css'
96 background-position: top left, top right;
110 test('format without code but with filePath', t =>
111 format({ filePath: './fixtures/style.css' }).then((source) => {
117 background-position: top left, top right;
130 test('format less', (t) => {
131 const source = fs.readFileSync('./fixtures/less.less', 'utf8');
135 filePath: './fixtures/less.less'
136 }).then((source) => {
141 .box-shadow(@style, @c) when (iscolor(@c)) {
142 -webkit-box-shadow: @style @c;
143 box-shadow: @style @c;
145 .box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {
146 .box-shadow(@style, rgba(0, 0, 0, @alpha));
150 color: saturate(@base, 5%);
151 border-color: lighten(@base, 30%);
154 .box-shadow(0 0 5px, 30%);
164 test('format with syntax error from prettier', (t) => {
165 const source = fs.readFileSync('./tests/error-syntax.css', 'utf8');
169 filePath: './tests/error-syntax.css'
171 t.is(err.name, 'SyntaxError');
175 test('alternate stylelint format', (t) => {
176 const source = fs.readFileSync('./fixtures/style.css', 'utf8');
181 // codeFilename: process.cwd()
182 // codeFilename: path.resolve(process.cwd(), './tests/less.less')
183 // filePath: path.resolve(process.cwd(), './fixtures/style.css')
186 const fixed = result.root.toString(result.opts.syntax);
189 result.root.toString(result.opts.syntax),
193 background-position: top left,
201 a[id="foo"] { content: "x"; }
209 test('resolve relative package', (t) => {
210 const path = resolveFrom('./fixtures/find-package/style.css', 'prettier');
212 t.is('1.6.0', require(path).version);
215 test('resolve relative package deep', (t) => {
216 const path = resolveFrom(
217 './fixtures/find-package/deep/style.css',
221 t.is('1.6.0', require(path).version);
224 test('resolve relative package fallback', (t) => {
225 const path = resolveFrom('./fixtures/style.css', 'prettier');
227 t.is('1.7.0', require(path).version);
230 test('resolve relative package null', (t) => {
231 const path = resolveFrom(__filename, 'prettier');
233 t.is('1.7.0', require(path).version);