Skip to content

Commit dbd64f9

Browse files
committed
Enable extended glob patterns. Fixes #84.
1 parent fcb5b16 commit dbd64f9

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

src/index.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,25 @@ describe('parseString', () => {
178178
cfg.should.eql([[null, {}], ['a\\\\b', {}]])
179179
})
180180

181-
182181
it('handles blank comments', () => {
183182
const cfg = editorconfig.parseString('#')
184183
cfg.should.eql([[null, {}]])
185184
})
186185
})
186+
187+
describe('extra behavior', () => {
188+
it('handles extended globs', () => {
189+
// These failed when we had noext: true in matchOptions
190+
const matcher = editorconfig.matcher({
191+
root: __dirname,
192+
}, Buffer.from(`\
193+
[*]
194+
indent_size = 4
195+
196+
[!(package).json]
197+
indent_size = 3`))
198+
199+
matcher(path.join(__dirname, 'package.json')).should.match({ indent_size: 4 })
200+
matcher(path.join(__dirname, 'foo.json')).should.match({ indent_size: 3 })
201+
})
202+
})

src/index.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { parse_to_uint32array, TokenTypes } from '@one-ini/wasm'
1010
import pkg from '../package.json'
1111

1212
const escapedSep = new RegExp(path.sep.replace(/\\/g, '\\\\'), 'g')
13-
const matchOptions = { matchBase: true, dot: true, noext: true }
13+
const matchOptions = { matchBase: true, dot: true }
1414

1515
// These are specified by the editorconfig script
1616
/* eslint-disable @typescript-eslint/naming-convention */
@@ -63,17 +63,15 @@ export interface ParseOptions {
6363
cache?: Cache
6464
}
6565

66-
// These are specified by the editorconfig script
67-
/* eslint-disable @typescript-eslint/naming-convention */
68-
const knownProps = {
69-
end_of_line: true,
70-
indent_style: true,
71-
indent_size: true,
72-
insert_final_newline: true,
73-
trim_trailing_whitespace: true,
74-
charset: true,
75-
}
76-
/* eslint-enable @typescript-eslint/naming-convention */
66+
const knownPropNames: (keyof KnownProps)[] = [
67+
'end_of_line',
68+
'indent_style',
69+
'indent_size',
70+
'insert_final_newline',
71+
'trim_trailing_whitespace',
72+
'charset',
73+
]
74+
const knownProps = new Set<string>(knownPropNames)
7775

7876
export type SectionName = string | null
7977
export interface SectionBody { [key: string]: string }
@@ -190,7 +188,7 @@ function processMatches(matches: Props, version: string): Props {
190188
function buildFullGlob(pathPrefix: string, glob: string): Minimatch {
191189
switch (glob.indexOf('/')) {
192190
case -1:
193-
glob = '**/' + glob
191+
glob = `**/${glob}`
194192
break
195193
case 0:
196194
glob = glob.substring(1)
@@ -217,14 +215,13 @@ function buildFullGlob(pathPrefix: string, glob: string): Minimatch {
217215
* @returns
218216
*/
219217
function normalizeProps(options: SectionBody): Props {
220-
const props = {}
218+
const props: Props = {}
221219
for (const key in options) {
222220
if (options.hasOwnProperty(key)) {
223221
const value = options[key]
224222
const key2 = key.toLowerCase()
225223
let value2: unknown = value
226-
// @ts-ignore -- Fix types here
227-
if (knownProps[key2]) {
224+
if (knownProps.has(key2)) {
228225
// All of the values for the known props are lowercase.
229226
value2 = String(value).toLowerCase()
230227
}
@@ -236,7 +233,6 @@ function normalizeProps(options: SectionBody): Props {
236233
// in editorconfig) & should just be returned as regular strings.
237234
value2 = String(value)
238235
}
239-
// @ts-ignore -- Fix types here
240236
props[key2] = value2
241237
}
242238
}
@@ -551,3 +547,33 @@ export function parseSync(
551547
const configs = getAllConfigsSync(filepaths, processedOptions)
552548
return combine(resolvedFilePath, configs, processedOptions)
553549
}
550+
551+
/**
552+
* I think this may be of limited utility at the moment, but I need something
553+
* like this for testing. As such, the interface of this may change without
554+
* warning.
555+
*
556+
* Something this direction may be better for editors than the caching bits
557+
* we've got today, but that will need some thought.
558+
*
559+
* @param options All options. root will be process.cwd if not specified.
560+
* @param buffers 1 or more Buffers that have .editorconfig contents.
561+
* @returns Function that can be called multiple times for different paths.
562+
* @private
563+
*/
564+
export function matcher(
565+
options: ParseOptions,
566+
...buffers: Buffer[]
567+
): (filepath: string) => Props {
568+
const processedOptions = opts('', options)[1]
569+
const configs = buffers.map(
570+
(buf, i) => processFileContents(
571+
path.join(processedOptions.root as string, `buffer-${i}`),
572+
buf,
573+
processedOptions
574+
))
575+
return (filepath: string) => {
576+
const resolvedFilePath = path.resolve(filepath)
577+
return combine(resolvedFilePath, configs, processedOptions)
578+
}
579+
}

0 commit comments

Comments
 (0)