-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathindex.ts
553 lines (512 loc) · 15.5 KB
/
index.ts
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import * as fs from 'fs'
import * as path from 'path'
import * as semver from 'semver'
import { Minimatch } from 'minimatch'
import { parse_to_uint32array, TokenTypes } from '@one-ini/wasm'
// @ts-ignore So we can set the rootDir to be 'lib', without processing
// package.json
import pkg from '../package.json'
const escapedSep = new RegExp(path.sep.replace(/\\/g, '\\\\'), 'g')
const matchOptions = { matchBase: true, dot: true, noext: true }
// These are specified by the editorconfig script
/* eslint-disable @typescript-eslint/naming-convention */
export interface KnownProps {
end_of_line?: 'lf' | 'crlf' | 'unset'
indent_style?: 'tab' | 'space' | 'unset'
indent_size?: number | 'tab' | 'unset'
insert_final_newline?: true | false | 'unset'
tab_width?: number | 'unset'
trim_trailing_whitespace?: true | false | 'unset'
charset?: string | 'unset'
}
/* eslint-enable @typescript-eslint/naming-convention */
interface UnknownMap {
[index: string]: unknown
}
export type Props = KnownProps & UnknownMap
export interface ECFile {
name: string
contents?: Buffer
}
type SectionGlob = Minimatch | null
type GlobbedProps = [SectionName, Props, SectionGlob][]
export interface ProcessedFileConfig {
root: boolean
name: string
config: GlobbedProps
notfound?: true
}
export interface Visited {
fileName: string
glob: string
}
export interface Cache {
get(path: string): ProcessedFileConfig | undefined
set(path: string, config: ProcessedFileConfig): this
}
export interface ParseOptions {
config?: string
version?: string
root?: string
files?: Visited[]
cache?: Cache
}
// These are specified by the editorconfig script
/* eslint-disable @typescript-eslint/naming-convention */
const knownProps = {
end_of_line: true,
indent_style: true,
indent_size: true,
insert_final_newline: true,
trim_trailing_whitespace: true,
charset: true,
}
/* eslint-enable @typescript-eslint/naming-convention */
export type SectionName = string | null
export interface SectionBody { [key: string]: string }
export type ParseStringResult = [SectionName, SectionBody][]
/**
* Parse a buffer using the faster one-ini WASM approach into something
* relatively easy to deal with in JS.
*
* @param data UTF8-encoded bytes.
* @returns Parsed contents. Will be truncated if there was a parse error.
*/
export function parseBuffer(data: Buffer): ParseStringResult {
const parsed = parse_to_uint32array(data)
let cur: SectionBody = {}
const res: ParseStringResult = [[null, cur]]
let key: string | null = null
for (let i = 0; i < parsed.length; i += 3) {
switch (parsed[i]) {
case TokenTypes.Section: {
cur = {}
res.push([
data.toString('utf8', parsed[i+1], parsed[i+2]),
cur,
])
break
}
case TokenTypes.Key:
key = data.toString('utf8', parsed[i+1], parsed[i+2])
break
case TokenTypes.Value: {
cur[key as string] = data.toString('utf8', parsed[i+1], parsed[i+2])
break
}
default: // Comments, etc.
break
}
}
return res
}
/**
* Parses a string. If possible, you should always use ParseBuffer instead,
* since this function does a UTF16-to-UTF8 conversion first.
*
* @param data String to parse.
* @returns Parsed contents. Will be truncated if there was a parse error.
* @deprecated Use {@link ParseBuffer} instead.
*/
export function parseString(data: string): ParseStringResult {
return parseBuffer(Buffer.from(data))
}
/**
* Gets a list of *potential* filenames based on the path of the target
* filename.
*
* @param filepath File we are asking about.
* @param options Config file name and root directory
* @returns List of potential fully-qualified filenames that might have configs.
*/
function getConfigFileNames(filepath: string, options: ParseOptions): string[] {
const paths = []
do {
filepath = path.dirname(filepath)
paths.push(path.join(filepath, options.config as string))
} while (filepath !== options.root)
return paths
}
/**
* Take a combined config for the target file, and tweak it slightly based on
* which editorconfig version's rules we are using.
*
* @param matches Combined config.
* @param version Editorconfig version to enforce.
* @returns The passed-in matches object, modified in place.
*/
function processMatches(matches: Props, version: string): Props {
// 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)
&& semver.gte(version, '0.10.0')
) {
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 buildFullGlob(pathPrefix: string, glob: string): Minimatch {
switch (glob.indexOf('/')) {
case -1:
glob = '**/' + glob
break
case 0:
glob = glob.substring(1)
break
default:
break
}
// braces_escaped_backslash2
// backslash_not_on_windows
glob = glob.replace(/\\\\/g, '\\\\\\\\')
// star_star_over_separator{1,3,5,6,9,15}
glob = glob.replace(/\*\*/g, '{*,**/**/**}')
// NOT path.join. Must stay in forward slashes.
return new Minimatch(`${pathPrefix}/${glob}`, matchOptions)
}
/**
* Normalize the properties read from a config file so that their key names
* are lowercased for the known properties, and their values are parsed into
* the correct JS types if possible.
*
* @param options
* @returns
*/
function normalizeProps(options: SectionBody): Props {
const props = {}
for (const key in options) {
if (options.hasOwnProperty(key)) {
const value = options[key]
const key2 = key.toLowerCase()
let value2: unknown = value
// @ts-ignore -- Fix types here
if (knownProps[key2]) {
// All of the values for the known props are lowercase.
value2 = String(value).toLowerCase()
}
try {
value2 = JSON.parse(String(value))
} catch (e) {}
if (typeof value2 === 'undefined' || value2 === null) {
// null and undefined are values specific to JSON (no special meaning
// in editorconfig) & should just be returned as regular strings.
value2 = String(value)
}
// @ts-ignore -- Fix types here
props[key2] = value2
}
}
return props
}
/**
* Take the contents of a config file, and prepare it for use. If a cache is
* provided, the result will be stored there. As such, all of the higher-CPU
* work that is per-file should be done here.
*
* @param filepath The fully-qualified path of the file.
* @param contents The contents as read from that file.
* @param options Access to the cache.
* @returns Processed file with globs pre-computed.
*/
function processFileContents(
filepath: string,
contents: Buffer|undefined,
options: ParseOptions
): ProcessedFileConfig {
let res: ProcessedFileConfig
if (!contents) {
// Negative cache
res = {
root: false,
notfound: true,
name: filepath,
config: [[ null, {}, null ]],
}
} else {
let pathPrefix = path.dirname(filepath)
if (path.sep !== '/') {
// Windows-only
pathPrefix = pathPrefix.replace(escapedSep, '/')
}
// After Windows path backslash's are turned into slashes, so that
// the backslashes we add here aren't turned into forward slashes:
// All of these characters are special to minimatch, but can be
// forced into path names on many file systems. Escape them. Note
// that these are in the order of the case statement in minimatch.
pathPrefix = pathPrefix.replace(/[?*+@!()|[\]{}]/g, '\\$&')
// I can't think of a way for this to happen in the filesystems I've
// seen (because of the path.dirname above), but let's be thorough.
pathPrefix = pathPrefix.replace(/^#/, '\\#')
const globbed: GlobbedProps = parseBuffer(contents).map(([name, body]) => [
name,
normalizeProps(body),
name ? buildFullGlob(pathPrefix, name) : null,
])
res = {
root: !!globbed[0][1].root, // globbed[0] is the global section
name: filepath,
config: globbed,
}
}
if (options.cache) {
options.cache.set(filepath, res)
}
return res
}
/**
* Get a file from the cache, or read its contents from disk, process, and
* insert into the cache (if configured).
*
* @param filepath The fully-qualified path of the config file.
* @param options Access to the cache, if configured.
* @returns The processed file, or undefined if there was an error reading it.
*/
async function getConfig(
filepath: string,
options: ParseOptions
): Promise<ProcessedFileConfig> {
if (options.cache) {
const cached = options.cache.get(filepath)
if (cached) {
return cached
}
}
const contents = await new Promise<Buffer|undefined>(resolve => {
fs.readFile(filepath, (_, buf) => {
// Ignore errors. contents will be undefined
// Perhaps only file-not-found should be ignored?
resolve(buf)
})
})
return processFileContents(filepath, contents, options)
}
/**
* Get a file from the cache, or read its contents from disk, process, and
* insert into the cache (if configured). Synchronous.
*
* @param filepath The fully-qualified path of the config file.
* @param options Access to the cache, if configured.
* @returns The processed file, or undefined if there was an error reading it.
*/
function getConfigSync(
filepath: string,
options: ParseOptions
): ProcessedFileConfig {
if (options.cache) {
const cached = options.cache.get(filepath)
if (cached) {
return cached
}
}
let contents: Buffer | undefined
try {
contents = fs.readFileSync(filepath)
} catch (_) {
// Ignore errors
// Perhaps only file-not-found should be ignored
}
return processFileContents(filepath, contents, options)
}
/**
* Get all of the possibly-existing config files, stopping when one is marked
* root=true.
*
* @param files List of potential files
* @param options Access to cache if configured
* @returns List of processed configs for existing files
*/
async function getAllConfigs(
files: string[],
options: ParseOptions
): Promise<ProcessedFileConfig[]> {
const configs: ProcessedFileConfig[] = []
for (const file of files) {
const config = await getConfig(file, options)
if (!config.notfound) {
configs.push(config)
if (config.root) {
break
}
}
}
return configs
}
/**
* Get all of the possibly-existing config files, stopping when one is marked
* root=true. Synchronous.
*
* @param files List of potential files
* @param options Access to cache if configured
* @returns List of processed configs for existing files
*/
function getAllConfigsSync(
files: string[],
options: ParseOptions
): ProcessedFileConfig[] {
const configs: ProcessedFileConfig[] = []
for (const file of files) {
const config = getConfigSync(file, options)
if (!config.notfound) {
configs.push(config)
if (config.root) {
break
}
}
}
return configs
}
/**
* Normalize the options passed in to the publicly-visible functions.
*
* @param filepath The name of the target file, relative to process.cwd().
* @param options Potentially-incomplete options.
* @returns The fully-qualified target file name and the normalized options.
*/
function opts(filepath: string, options: ParseOptions = {}): [
string,
ParseOptions
] {
const resolvedFilePath = path.resolve(filepath)
return [
resolvedFilePath,
{
config: options.config || '.editorconfig',
version: options.version || pkg.version,
root: path.resolve(options.root || path.parse(resolvedFilePath).root),
files: options.files,
cache: options.cache,
},
]
}
/**
* Low-level interface, which exists only for backward-compatibility.
* Deprecated.
*
* @param filepath The name of the target file, relative to process.cwd().
* @param files A promise for a list of objects describing the files.
* @param options All options
* @returns The properties found for filepath
* @deprecated
*/
export async function parseFromFiles(
filepath: string,
files: Promise<ECFile[]>,
options: ParseOptions = {}
): Promise<Props> {
return parseFromFilesSync(filepath, await files, options)
}
/**
* Low-level interface, which exists only for backward-compatibility.
* Deprecated.
*
* @param filepath The name of the target file, relative to process.cwd().
* @param files A list of objects describing the files.
* @param options All options
* @returns The properties found for filepath
* @deprecated
*/
export function parseFromFilesSync(
filepath: string,
files: ECFile[],
options: ParseOptions = {}
): Props {
const [resolvedFilePath, processedOptions] = opts(filepath, options)
const configs = []
for (const ecf of files) {
let cfg: ProcessedFileConfig | undefined
if (!options.cache || !(cfg = options.cache.get(ecf.name))) { // Single "="!
cfg = processFileContents(ecf.name, ecf.contents, processedOptions)
}
if (!cfg.notfound) {
configs.push(cfg)
}
if (cfg.root) {
break
}
}
return combine(resolvedFilePath, configs, processedOptions)
}
/**
* Combine the pre-parsed results of all matching config file sections, in
* order.
*
* @param filepath The target file path
* @param configs All of the found config files, up to the root
* @param options Adds to `options.files` if it exists
* @returns Combined properties
*/
function combine(
filepath: string,
configs: ProcessedFileConfig[],
options: ParseOptions
): Props {
const ret = configs.reverse().reduce((props: Props, processed) => {
for (const [name, body, glob] of processed.config) {
if (glob && glob.match(filepath)) {
Object.assign(props, body)
if (options.files) {
options.files.push({
fileName: processed.name,
glob: name as string,
})
}
}
}
return props
}, {})
return processMatches(ret, options.version as string)
}
/**
* Find all of the properties from matching sections in config files in the
* same directory or toward the root of the filesystem.
*
* @param filepath The target file name, relative to process.cwd().
* @param options All options
* @returns Combined properties for the target file
*/
export async function parse(
filepath: string,
options: ParseOptions = {}
): Promise<Props> {
const [resolvedFilePath, processedOptions] = opts(filepath, options)
const filepaths = getConfigFileNames(resolvedFilePath, processedOptions)
const configs = await getAllConfigs(filepaths, processedOptions)
return combine(resolvedFilePath, configs, processedOptions)
}
/**
* Find all of the properties from matching sections in config files in the
* same directory or toward the root of the filesystem. Synchronous.
*
* @param filepath The target file name, relative to process.cwd().
* @param options All options
* @returns Combined properties for the target file
*/
export function parseSync(
filepath: string,
options: ParseOptions = {}
): Props {
const [resolvedFilePath, processedOptions] = opts(filepath, options)
const filepaths = getConfigFileNames(resolvedFilePath, processedOptions)
const configs = getAllConfigsSync(filepaths, processedOptions)
return combine(resolvedFilePath, configs, processedOptions)
}