Skip to content
Permalink
Browse files

Implement helper for our ESLint plugin

  • Loading branch information...
novemberborn committed May 25, 2019
1 parent c10e38c commit 51433bed947d31e5f3df26bdf6eee10ad4344efa
@@ -0,0 +1,37 @@
'use strict';
const babelPipeline = require('./lib/babel-pipeline');
const normalizeExtensions = require('./lib/extensions');
const {hasExtension, normalizeGlobs, classify} = require('./lib/globs');
const loadConfig = require('./lib/load-config');

const cache = new Map();

function load(projectDir) {
if (cache.has(projectDir)) {
return cache.get(projectDir);
}

const conf = loadConfig(projectDir);
const babelConfig = babelPipeline.validate(conf.babel);
const extensions = normalizeExtensions(conf.extensions || [], babelConfig);
const globs = {cwd: projectDir, ...normalizeGlobs(conf.files, conf.helpers, conf.sources, extensions.all)};

const helper = Object.freeze({
classifyFile: file => classify(file, globs),
classifyImport: importPath => {
if (hasExtension(globs.extensions, importPath)) {
// The importPath has one of the test file extensions: we can classify
// it directly.
return classify(importPath, globs);
}

// Add the first extension. If multiple extensions are available, assume
// patterns are not biased to any particular extension.
return classify(`${importPath}.${globs.extensions[0]}`, globs);
}
});
cache.set(projectDir, helper);
return helper;
}

exports.load = load;
@@ -92,6 +92,8 @@ exports.normalizeGlobs = normalizeGlobs;

const hasExtension = (extensions, file) => extensions.includes(path.extname(file).slice(1));

exports.hasExtension = hasExtension;

const findFiles = async (cwd, patterns) => {
const files = await globby(patterns, {
absolute: true,
@@ -7,10 +7,10 @@ const pkgConf = require('pkg-conf');
const NO_SUCH_FILE = Symbol('no ava.config.js file');
const MISSING_DEFAULT_EXPORT = Symbol('missing default export');

function loadConfig(defaults = {}) {
const packageConf = pkgConf.sync('ava');
function loadConfig(resolveFrom = process.cwd(), defaults = {}) {
const packageConf = pkgConf.sync('ava', {cwd: resolveFrom});
const filepath = pkgConf.filepath(packageConf);
const projectDir = filepath === null ? process.cwd() : path.dirname(filepath);
const projectDir = filepath === null ? resolveFrom : path.dirname(filepath);

let fileConf;
try {
@@ -32,7 +32,7 @@ function resolveModules(modules) {

Promise.longStackTraces();

const conf = loadConfig({
const conf = loadConfig(undefined, {
babel: {
testOptions: {}
},
@@ -0,0 +1,94 @@
'use strict';
const path = require('path');
const {test} = require('tap');

const {load} = require('../eslint-plugin-helper');

const projectDir = path.join(__dirname, 'fixture/eslint-plugin-helper');

test('caches loaded configuration', t => {
const expected = load(projectDir);
const actual = load(projectDir);
t.is(expected, actual);
t.end();
});

test('classifies files according to the configuration', t => {
const helper = load(projectDir);
t.deepEqual(helper.classifyFile(path.join(projectDir, 'tests/test.foo')), {
isHelper: false,
isSource: false,
isTest: true
});
t.deepEqual(helper.classifyFile(path.join(projectDir, 'tests/_helper.foo')), {
isHelper: true,
isSource: false,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(projectDir, 'helpers/helper.foo')), {
isHelper: true,
isSource: false,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(projectDir, 'source.foo')), {
isHelper: false,
isSource: true,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(projectDir, 'tests/test.js')), {
isHelper: false,
isSource: false,
isTest: false
});
t.end();
});

test('classifies imports with extension according to the configuration', t => {
const helper = load(projectDir);
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/test.foo')), {
isHelper: false,
isSource: false,
isTest: true
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/_helper.foo')), {
isHelper: true,
isSource: false,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'helpers/helper.foo')), {
isHelper: true,
isSource: false,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'source.foo')), {
isHelper: false,
isSource: true,
isTest: false
});
t.end();
});

test('classifies imports without extension according to the configuration', t => {
const helper = load(projectDir);
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/test')), {
isHelper: false,
isSource: false,
isTest: true
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/_helper')), {
isHelper: true,
isSource: false,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'helpers/helper')), {
isHelper: true,
isSource: false,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'source')), {
isHelper: false,
isSource: true,
isTest: false
});
t.end();
});
@@ -0,0 +1,6 @@
export default {
babel: false,
files: ['tests/**/*'],
helpers: ['helpers/*'],
extensions: ['foo']
};
@@ -0,0 +1 @@
{}
@@ -21,6 +21,13 @@ test('finds config in package.json', t => {
t.end();
});

test('loads config from a particular directory', t => {
changeDir('throws');
const conf = loadConfig(path.resolve(__dirname, 'fixture', 'load-config', 'package-only'));
t.is(conf.failFast, true);
t.end();
});

test('throws a warning of both configs are present', t => {
changeDir('package-yes-file-yes');
t.throws(loadConfig);
@@ -32,7 +39,7 @@ test('merges in defaults passed with initial call', t => {
const defaults = {
files: ['123', '!456']
};
const {files, failFast} = loadConfig(defaults);
const {files, failFast} = loadConfig(undefined, defaults);
t.is(failFast, true, 'preserves original props');
t.is(files, defaults.files, 'merges in extra props');
t.end();

0 comments on commit 51433be

Please sign in to comment.
You can’t perform that action at this time.