/
editorconfig.js
165 lines (148 loc) · 4.85 KB
/
editorconfig.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
var os = require('os');
var path = require('path');
var Promise = require('bluebird');
var whenReadFile = Promise.promisify(require('fs').readFile);
var minimatch = require('./lib/fnmatch');
var iniparser = require('./lib/ini');
var Version = require('./lib/version');
var pkg = require('./package.json');
var knownProps = [
'end_of_line',
'indent_style',
'indent_size',
'insert_final_newline',
'trim_trailing_whitespace',
'charset'
].reduce(function (set, prop) {
set[prop] = true;
return set;
}, {});
function fnmatch(filepath, glob) {
var matchOptions = {matchBase: true, dot: true, noext: true};
glob = glob.replace(/\*\*/g, '{*,**/**/**}');
return minimatch(filepath, glob, matchOptions);
}
function getConfigFileNames(filepath, options) {
var paths = [];
do {
filepath = path.dirname(filepath);
paths.push(path.join(filepath, options.config));
} while (filepath !== options.root);
return paths;
}
function getFilepathRoot(filepath) {
if (path.parse !== undefined) {
// Node.js >= 0.11.15
return path.parse(filepath).root;
}
if (os.platform() === 'win32') {
return path.resolve(filepath).match(/^(\\\\[^\\]+\\)?[^\\]+\\/)[0];
}
return '/';
}
function processMatches(matches, version) {
// Set indent_size to "tab" if indent_size is unspecified and
// indent_style is set to "tab".
if ("indent_style" in matches && matches.indent_style === "tab" &&
!("indent_size" in matches) && version.gte(new Version(0, 10))) {
matches.indent_size = "tab";
}
// Set tab_width to indent_size if indent_size is specified and
// tab_width is unspecified
if ("indent_size" in matches && !("tab_width" in matches) &&
matches.indent_size !== "tab")
matches.tab_width = matches.indent_size;
// Set indent_size to tab_width if indent_size is "tab"
if("indent_size" in matches && "tab_width" in matches &&
matches.indent_size === "tab")
matches.indent_size = matches.tab_width;
return matches;
}
function processOptions(options, filepath) {
options = options || {};
return {
config: options.config || '.editorconfig',
version: new Version(options.version || pkg.version),
root: path.resolve(options.root || getFilepathRoot(filepath))
};
}
function parseFromFiles(filepath, files, options) {
return getConfigsForFiles(files).then(function (configs) {
return configs.reverse();
}).reduce(function (matches, file) {
var pathPrefix = path.dirname(file.name);
file.contents.forEach(function (section) {
var glob = section[0], options = section[1];
if (!glob) return;
switch (glob.indexOf('/')) {
case -1: glob = "**/" + glob; break;
case 0: glob = glob.substring(1); break;
}
var fullGlob = path.join(pathPrefix, glob);
if (!fnmatch(filepath, fullGlob)) return;
for (var key in options) {
var value = options[key];
key = key.toLowerCase();
if (knownProps[key]) {
value = value.toLowerCase();
}
try {
value = JSON.parse(value);
} catch(e) {}
if (typeof value === 'undefined' || value === null) {
// null and undefined are values specific to JSON (no special meaning
// in editorconfig) & should just be returned as regular strings.
value = String(value);
}
matches[key] = value;
}
});
return matches;
}, {}).then(function (matches) {
return processMatches(matches, options.version);
});
}
function StopReduce(array) {
this.array = array;
}
StopReduce.prototype = Object.create(Error.prototype);
function getConfigsForFiles(files) {
return Promise.reduce(files, function (configs, file) {
var contents = iniparser.parseString(file.contents);
configs.push({
name: file.name,
contents: contents
});
if ((contents[0][1].root || '').toLowerCase() === 'true') {
return Promise.reject(new StopReduce(configs));
}
return configs;
}, []).catch(StopReduce, function (stop) {
return stop.array;
});
}
function readConfigFiles(filepaths) {
return Promise.map(filepaths, function (path) {
return whenReadFile(path, 'utf-8').catch(function () {
return '';
}).then(function (contents) {
return {name: path, contents: contents};
});
});
}
module.exports.parseFromFiles = function (filepath, files, options) {
return new Promise (function (resolve, reject) {
filepath = path.resolve(filepath);
options = processOptions(options, filepath);
resolve(parseFromFiles(filepath, files, option));
});
};
module.exports.parse = function (filepath, options) {
return new Promise (function (resolve, reject) {
filepath = path.resolve(filepath);
options = processOptions(options, filepath);
var filepaths = getConfigFileNames(filepath, options);
var files = readConfigFiles(filepaths);
resolve(parseFromFiles(filepath, files, options));
});
};