.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / prettier-stylelint / src / index.js
1 'use strict';
2
3 const fs = require('fs');
4 const path = require('path');
5 const resolveFrom = require('resolve-from');
6 const debug = require('debug')('prettier-stylelint:main');
7
8 /**
9  * Resolve Config for the given file
10  *
11  * @export
12  * @param {string} file - filepath
13  * @param {Object} options - options
14  * @returns {Promise} -
15  */
16 function resolveConfig({
17     filePath,
18     stylelintPath,
19     stylelintConfig,
20     prettierOptions
21 }) {
22     const resolve = resolveConfig.resolve;
23     const stylelint = requireRelative(stylelintPath, filePath, 'stylelint');
24     const linterAPI = stylelint.createLinter();
25
26     if (stylelintConfig) {
27         return Promise.resolve(resolve(stylelintConfig, prettierOptions));
28     }
29
30     return linterAPI
31         .getConfigForFile(filePath)
32         .then(({ config }) => resolve(config, prettierOptions));
33 }
34
35 resolveConfig.resolve = (stylelintConfig, prettierOptions = {}) => {
36     const { rules } = stylelintConfig;
37
38     if (rules['max-line-length']) {
39         const printWidth = rules['max-line-length'][0];
40
41         prettierOptions.printWidth = printWidth;
42     }
43
44     if (rules['string-quotes']) {
45         const quotes = rules['string-quotes'][0];
46
47         if (quotes === 'single') {
48             prettierOptions.singleQuote = true;
49         }
50     }
51
52     if (rules.indentation) {
53         const indentation = rules.indentation[0];
54
55         if (indentation === 'tab') {
56             prettierOptions.useTabs = true;
57             prettierOptions.tabWidth = 2;
58         } else {
59             prettierOptions.useTabs = false;
60             prettierOptions.tabWidth = indentation;
61         }
62     }
63     prettierOptions.parser = 'postcss';
64     debug('prettier %O', prettierOptions);
65     debug('linter %O', stylelintConfig);
66
67     return [prettierOptions, stylelintConfig];
68 };
69
70 function stylelinter(code, { filePath, stylelintPath }) {
71     const stylelint = requireRelative(stylelintPath, filePath, 'stylelint');
72     const linterAPI = stylelint.createLinter({ fix: true });
73
74     return linterAPI
75         ._lintSource({
76             code,
77             codeFilename: filePath
78         })
79         .then((result) => {
80             const fixed = result.root.toString(result.opts.syntax);
81
82             return fixed;
83         });
84 }
85
86 function requireRelative(path, filePath, packageName) {
87     try {
88         if (path) {
89             return require(resolveFrom(path, packageName));
90         }
91
92         return require(resolveFrom(filePath, packageName));
93     } catch (err) {
94         return require(packageName);
95     }
96 }
97
98 function getPrettierConfig(filePath, prettierPath) {
99     const prettier = requireRelative(prettierPath, filePath, 'prettier');
100
101     // NOTE: Backward-compatibility with old prettier versions (<1.7)
102     //       that don't have ``resolveConfig.sync` method.
103     return typeof prettier.resolveConfig.sync === 'undefined' ?
104         {} :
105         prettier.resolveConfig.sync(filePath);
106 }
107
108 function format({
109     filePath = '',
110     text = fs.readFileSync(filePath, 'utf8'),
111     prettierPath,
112     stylelintPath,
113     prettierOptions = getPrettierConfig(filePath, prettierPath),
114     stylelintConfig
115 }) {
116     const options = {
117         filePath: path.isAbsolute(filePath) ?
118             filePath :
119             path.resolve(process.cwd(), filePath),
120         text,
121         prettierPath,
122         stylelintPath,
123         stylelintConfig,
124         prettierOptions
125     };
126     const prettier = requireRelative(prettierPath, filePath, 'prettier');
127
128     return resolveConfig(options).then(([prettierConfig]) =>
129         stylelinter(prettier.format(text, prettierConfig), options)
130     );
131 }
132
133 exports.format = format;
134 exports.resolveConfig = resolveConfig;
135 exports.getPrettierConfig = getPrettierConfig;