Skip to content

Commit 0ca2a9d

Browse files
committed
feature(@putout/engine-processor) extensions -> files
1 parent d7d9ebe commit 0ca2a9d

4 files changed

Lines changed: 55 additions & 38 deletions

File tree

packages/engine-processor/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ runProcessors({
3737
Simplest possible procesor exampmle can look like this:
3838

3939
```js
40-
module.exports.extensions = [
41-
'js',
40+
module.exports.files = [
41+
'*.js',
4242
];
4343

4444
// with help of process you can lint an types of files with corresponding tools

packages/engine-processor/lib/processor.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const {extname} = require('path');
43
const {loadProcessors} = require('@putout/engine-loader');
4+
const picomatch = require('picomatch');
55

66
const defaultProcessors = [
77
'javascript',
@@ -12,19 +12,18 @@ const stubProcess = (a) => [a, []];
1212

1313
module.exports.defaultProcessors = defaultProcessors;
1414

15-
module.exports.getExtensions = (processors) => {
15+
module.exports.getFilePatterns = (processors) => {
1616
const result = [];
1717

18-
for (const {extensions} of processors) {
19-
result.push(...extensions);
18+
for (const {files} of processors) {
19+
result.push(...files);
2020
}
2121

2222
return result;
2323
};
2424

2525
module.exports.runProcessors = async ({name, fix, processFile, options, rawSource, index, length}) => {
2626
const allPlaces = [];
27-
const ext = extname(name).slice(1);
2827
const {
2928
processors = defaultProcessors,
3029
} = options;
@@ -37,8 +36,8 @@ module.exports.runProcessors = async ({name, fix, processFile, options, rawSourc
3736
let processedPlaces = [];
3837
let isProcessed = false;
3938

40-
for (const {extensions, preProcess, postProcess, process = stubProcess} of loadedProcessors) {
41-
if (!extensions.includes(ext))
39+
for (const {files, preProcess, postProcess, process = stubProcess} of loadedProcessors) {
40+
if (!isMatchName(name, files))
4241
continue;
4342

4443
[processedSource, processedPlaces] = process(rawSource);
@@ -85,3 +84,16 @@ module.exports.runProcessors = async ({name, fix, processFile, options, rawSourc
8584
};
8685
};
8786

87+
function isMatchName(name, files) {
88+
for (const current of files) {
89+
const isMatch = picomatch(current, {
90+
dot: true,
91+
matchBase: true,
92+
});
93+
94+
if (isMatch(name))
95+
return true;
96+
}
97+
98+
return false;
99+
}

packages/engine-processor/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"report": "madrun report"
3131
},
3232
"dependencies": {
33-
"@putout/engine-loader": "^2.1.3"
33+
"@putout/engine-loader": "^2.1.3",
34+
"picomatch": "^2.2.2"
3435
},
3536
"keywords": [
3637
"putout",

packages/engine-processor/test/processor.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const test = require('supertape');
77
const stub = require('@cloudcmd/stub');
88
const processFile = require('putout/lib/cli/process-file');
99

10-
const {runProcessors, getExtensions} = require('..');
10+
const {
11+
runProcessors,
12+
getFilePatterns,
13+
} = require('..');
1114

1215
test('putout: engine-processor: no processor', async (t) => {
1316
const name = 'hello.xxx';
@@ -133,33 +136,6 @@ test('putout: engine-processor: markdown: fix', async (t) => {
133136
t.end();
134137
});
135138

136-
test('putout: engine-processor: getExtensions', (t) => {
137-
const js = {
138-
extensions: [
139-
'js',
140-
'ts',
141-
],
142-
};
143-
144-
const css = {
145-
extensions: [
146-
'css',
147-
],
148-
};
149-
150-
const processors = [js, css];
151-
const result = getExtensions(processors);
152-
153-
const expected = [
154-
'js',
155-
'ts',
156-
'css',
157-
];
158-
159-
t.deepEqual(expected, result, 'should equal');
160-
t.end();
161-
});
162-
163139
test('putout: engine-processor: markdown: no fix', async (t) => {
164140
const name = join(__dirname, 'fixture/no-fix.md');
165141
const options = {
@@ -215,3 +191,31 @@ test('putout: engine-processor: markdown: js changed', async (t) => {
215191
t.equal(processedSource, expectedSource, 'should equal');
216192
t.end();
217193
});
194+
195+
test('putout: engine-processor: getFilePatterns', (t) => {
196+
const js = {
197+
files: [
198+
'*.js',
199+
'*.ts',
200+
],
201+
};
202+
203+
const css = {
204+
files: [
205+
'*.css',
206+
],
207+
};
208+
209+
const processors = [js, css];
210+
const result = getFilePatterns(processors);
211+
212+
const expected = [
213+
'*.js',
214+
'*.ts',
215+
'*.css',
216+
];
217+
218+
t.deepEqual(expected, result, 'should equal');
219+
t.end();
220+
});
221+

0 commit comments

Comments
 (0)