-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement helper for our ESLint plugin
- Loading branch information
1 parent
c10e38c
commit 51433be
Showing
8 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
babel: false, | ||
files: ['tests/**/*'], | ||
helpers: ['helpers/*'], | ||
extensions: ['foo'] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters