Skip to content

Commit 4f7a7a8

Browse files
committed
feature: @putout/operator-match-files: add support of filename
1 parent 4707a77 commit 4f7a7a8

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

packages/operator-match-files/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = matchFiles({
101101
});
102102
```
103103

104-
### Exclude
104+
### `exclude`
105105

106106
If you want to exclude some files, use:
107107

@@ -138,6 +138,23 @@ module.exports = matchFiles({
138138
});
139139
```
140140

141+
### `filename`
142+
143+
You can pass default `filename`, so when no options provided it will be used.
144+
145+
```js
146+
const {operator} = require('putout');
147+
const {matchFiles} = operator;
148+
const updateTSConfig = require('../update-tsconfig');
149+
150+
module.exports = matchFiles({
151+
filename: '*.d.ts',
152+
files: {
153+
'__name.ts -> __name.js': updateTSConfig,
154+
},
155+
});
156+
```
157+
141158
## License
142159

143160
MIT

packages/operator-match-files/lib/match-files.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ const {entries} = Object;
2626
const report = (path, {message}) => message;
2727

2828
module.exports.matchFiles = (options) => {
29+
const {filename} = options;
2930
const files = options.files ?? options;
3031
const exclude = options.exclude ?? [];
3132

3233
check(files);
3334

3435
const scan = createScan({
36+
defaultFilename: filename,
3537
files,
3638
exclude,
3739
});
@@ -60,10 +62,12 @@ function fix(inputFile, {dirPath, matchInputFilename, outputFilename, matchedJS,
6062
removeFile(inputFile);
6163
}
6264

63-
const createScan = ({files, exclude}) => (mainPath, {push, progress, options}) => {
65+
const createScan = ({files, exclude, defaultFilename}) => (mainPath, {push, progress, options}) => {
6466
const allFiles = [];
6567
const cwd = getFilename(mainPath);
6668

69+
options.filename = options.filename ?? defaultFilename;
70+
6771
for (const [filename, rawOptions] of entries(files)) {
6872
const [matchInputFilenameMask] = parseMatcher(filename, options);
6973
const inputFiles = findFile(mainPath, matchInputFilenameMask, exclude);

packages/operator-match-files/lib/match-files.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,44 @@ test('putout: operator: match-files: __name', (t) => {
515515
t.end();
516516
});
517517

518+
test('putout: operator: match-files: default filename', (t) => {
519+
const source = stringify(['/', [
520+
'/.eslintrc.js',
521+
'{}',
522+
]]);
523+
524+
const jsSource = toJS(source, __filesystem);
525+
const ast = parse(jsSource);
526+
527+
transform(ast, jsSource, {
528+
plugins: [
529+
['match-files', matchFiles({
530+
filename: '*.js',
531+
files: {
532+
'__name.js -> __name.jsx': {
533+
report: () => '',
534+
fix: noop,
535+
include: () => ['Program'],
536+
},
537+
},
538+
})],
539+
],
540+
});
541+
542+
const result = JSON.parse(fromJS(
543+
print(ast),
544+
__filesystem,
545+
));
546+
547+
const expected = ['/', [
548+
'/.eslintrc.jsx',
549+
'e30K',
550+
]];
551+
552+
t.deepEqual(result, expected);
553+
t.end();
554+
});
555+
518556
test('putout: operator: match-files: __name: dot', (t) => {
519557
const source = stringify(['/', [
520558
'/index.mjs',

0 commit comments

Comments
 (0)