Skip to content

Commit

Permalink
Optionally process 'unset' properties. Fixes #123
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Jul 2, 2023
1 parent dbd64f9 commit 30fe8ae
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 2.0.0

- **Breaking**: Now requires Node v16+
- Enable extended globbing from minimatch. This means that some patterns will
work in this version might not work in other editorconfig implementations.
Fixes #84.
- Add `unset` option to API and CLI. When enabled, properties with the value
"unset" will be removed from the returned object. Defaults to false in all
cases, since according to the core team, this is something that the editor
plugin is supposed to do, and the tests reinforce this. An `unset()`
function is now exported if you'd like to call it explicitly.
Fixes #123.

## 1.0.3

- Updated all dependencies, including security fixes for semver 7.3.8
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default async function cli(
.option('-f <path>', 'Specify conf filename other than \'.editorconfig\'')
.option('-b <version>', 'Specify version (used by devs to test compatibility)')
.option('--files', 'Output file names that contributed to the configuration, rather than the configuation itself')
.option('--unset', 'Remove all properties whose final value is \'unset\'')
.parse(args)

const files = program.args
Expand All @@ -73,6 +74,7 @@ export default async function cli(
version: opts.b as string,
files: visited ? visited[i++] : undefined,
cache,
unset: Boolean(opts.unset),
}))
}
return p
Expand Down
17 changes: 17 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,20 @@ indent_size = 3`))
matcher(path.join(__dirname, 'foo.json')).should.match({ indent_size: 3 })
})
})

describe('unset', () => {
it('pair witht the value `unset`', () => {
const matcher = editorconfig.matcher({
root: __dirname,
unset: true,
}, Buffer.from(`\
[*]
indent_size = 4
[*.json]
indent_size = unset
`))
matcher(path.join(__dirname, 'index.js')).should.match({ indent_size: 4 })
matcher(path.join(__dirname, 'index.json')).should.be.eql({ })
})
})
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface ParseOptions {
root?: string
files?: Visited[]
cache?: Cache
unset?: boolean
}

const knownPropNames: (keyof KnownProps)[] = [
Expand Down Expand Up @@ -426,6 +427,7 @@ function opts(filepath: string, options: ParseOptions = {}): [
root: path.resolve(options.root || path.parse(resolvedFilePath).root),
files: options.files,
cache: options.cache,
unset: options.unset,
},
]
}
Expand Down Expand Up @@ -507,11 +509,33 @@ function combine(
}
}
}

return props
}, {})

if (options.unset) {
unset(ret)
}

return processMatches(ret, options.version as string)
}

/**
* For any pair, a value of `unset` removes the effect of that pair, even if
* it has been set before. This method modifies the properties object in
* place to remove any property that has a value of `unset`.
*
* @param props Properties object to modify.
*/
export function unset(props: Props): void {
const keys = Object.keys(props)
for (const k of keys) {
if (props[k] === 'unset') {
delete props[k]
}
}
}

/**
* Find all of the properties from matching sections in config files in the
* same directory or toward the root of the filesystem.
Expand Down

0 comments on commit 30fe8ae

Please sign in to comment.