Skip to content

Commit 30fe8ae

Browse files
committed
Optionally process 'unset' properties. Fixes #123
1 parent dbd64f9 commit 30fe8ae

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## 2.0.0
2+
3+
- **Breaking**: Now requires Node v16+
4+
- Enable extended globbing from minimatch. This means that some patterns will
5+
work in this version might not work in other editorconfig implementations.
6+
Fixes #84.
7+
- Add `unset` option to API and CLI. When enabled, properties with the value
8+
"unset" will be removed from the returned object. Defaults to false in all
9+
cases, since according to the core team, this is something that the editor
10+
plugin is supposed to do, and the tests reinforce this. An `unset()`
11+
function is now exported if you'd like to call it explicitly.
12+
Fixes #123.
13+
114
## 1.0.3
215

316
- Updated all dependencies, including security fixes for semver 7.3.8

src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default async function cli(
5454
.option('-f <path>', 'Specify conf filename other than \'.editorconfig\'')
5555
.option('-b <version>', 'Specify version (used by devs to test compatibility)')
5656
.option('--files', 'Output file names that contributed to the configuration, rather than the configuation itself')
57+
.option('--unset', 'Remove all properties whose final value is \'unset\'')
5758
.parse(args)
5859

5960
const files = program.args
@@ -73,6 +74,7 @@ export default async function cli(
7374
version: opts.b as string,
7475
files: visited ? visited[i++] : undefined,
7576
cache,
77+
unset: Boolean(opts.unset),
7678
}))
7779
}
7880
return p

src/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,20 @@ indent_size = 3`))
200200
matcher(path.join(__dirname, 'foo.json')).should.match({ indent_size: 3 })
201201
})
202202
})
203+
204+
describe('unset', () => {
205+
it('pair witht the value `unset`', () => {
206+
const matcher = editorconfig.matcher({
207+
root: __dirname,
208+
unset: true,
209+
}, Buffer.from(`\
210+
[*]
211+
indent_size = 4
212+
213+
[*.json]
214+
indent_size = unset
215+
`))
216+
matcher(path.join(__dirname, 'index.js')).should.match({ indent_size: 4 })
217+
matcher(path.join(__dirname, 'index.json')).should.be.eql({ })
218+
})
219+
})

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface ParseOptions {
6161
root?: string
6262
files?: Visited[]
6363
cache?: Cache
64+
unset?: boolean
6465
}
6566

6667
const knownPropNames: (keyof KnownProps)[] = [
@@ -426,6 +427,7 @@ function opts(filepath: string, options: ParseOptions = {}): [
426427
root: path.resolve(options.root || path.parse(resolvedFilePath).root),
427428
files: options.files,
428429
cache: options.cache,
430+
unset: options.unset,
429431
},
430432
]
431433
}
@@ -507,11 +509,33 @@ function combine(
507509
}
508510
}
509511
}
512+
510513
return props
511514
}, {})
515+
516+
if (options.unset) {
517+
unset(ret)
518+
}
519+
512520
return processMatches(ret, options.version as string)
513521
}
514522

523+
/**
524+
* For any pair, a value of `unset` removes the effect of that pair, even if
525+
* it has been set before. This method modifies the properties object in
526+
* place to remove any property that has a value of `unset`.
527+
*
528+
* @param props Properties object to modify.
529+
*/
530+
export function unset(props: Props): void {
531+
const keys = Object.keys(props)
532+
for (const k of keys) {
533+
if (props[k] === 'unset') {
534+
delete props[k]
535+
}
536+
}
537+
}
538+
515539
/**
516540
* Find all of the properties from matching sections in config files in the
517541
* same directory or toward the root of the filesystem.

0 commit comments

Comments
 (0)