Skip to content

Commit

Permalink
feature: @putout/eslint-flat: add support of FlatConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Apr 19, 2024
1 parent 1284984 commit 8c8705b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
21 changes: 2 additions & 19 deletions packages/eslint-flat/lib/flat.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
'use strict';

const process = require('node:process');
const {join} = require('node:path');
const {matchToFlatDir} = require('./match-flat-to-dir');
const {entries} = Object;

const CWD = process.cwd();

module.exports.matchToFlatDir = (path, config) => {
const configPath = join(CWD, path, 'eslint.config.js');
const {match} = config || require(configPath);

const result = [];

for (const [name, rules] of entries(match)) {
result.push({
files: ['**/' + join(path, name)],
rules,
});
}

return result;
};
module.exports.matchToFlatDir = matchToFlatDir;

module.exports.matchToFlat = (config) => {
const result = [];
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-flat/lib/flat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ test('eslint-flat: matchToFlatDir: require', (t) => {
t.ok(error);
t.end();
});

test('eslint-flat: matchToFlatDir: no match', (t) => {
const result = matchToFlatDir('./hello', [{
files: ['world'],
rules: {
semi: 'off',
},
}]);

const expected = [{
files: ['**/hello/world'],
rules: {
semi: 'off',
},
}];

t.deepEqual(result, expected);
t.end();
});
45 changes: 45 additions & 0 deletions packages/eslint-flat/lib/match-flat-to-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const process = require('node:process');
const {join} = require('node:path');
const {entries} = Object;
const CWD = process.cwd();

module.exports.matchToFlatDir = (path, config) => {
const configPath = join(CWD, path, 'eslint.config.js');
const flatConfig = config || require(configPath);
const {match} = flatConfig;

if (match)
return parseMatch(path, match);

return parseFlatConfig(path, flatConfig);
};

function parseFlatConfig(path, flatConfig) {
const result = [];

for (const {files, rules} of flatConfig) {
const [name] = files;

result.push({
files: ['**/' + join(path, name)],
rules,
});
}

return result;
}

function parseMatch(path, match) {
const result = [];

for (const [name, rules] of entries(match)) {
result.push({
files: ['**/' + join(path, name)],
rules,
});
}

return result;
}

0 comments on commit 8c8705b

Please sign in to comment.