Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import/core-modules #365

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Added
- [`import/core-modules` setting]: allow configuration of additional module names,
to be treated as builtin modules (a la `path`, etc. in Node). ([#275] + [#365], thanks [@sindresorhus] for driving)

## [1.9.1] - 2016-06-16
### Fixed
Expand Down Expand Up @@ -201,6 +204,7 @@ for info on changes for earlier releases.
[`import/cache` setting]: ./README.md#importcache
[`import/ignore` setting]: ./README.md#importignore
[`import/extensions` setting]: ./README.md#importextensions
[`import/core-modules` setting]: ./README.md#importcore-modules

[`no-unresolved`]: ./docs/rules/no-unresolved.md
[`no-deprecated`]: ./docs/rules/no-deprecated.md
Expand All @@ -221,6 +225,7 @@ for info on changes for earlier releases.
[`no-mutable-exports`]: ./docs/rules/no-mutable-exports.md
[`prefer-default-export`]: ./docs/rules/prefer-default-export.md

[#365]: https://github.com/benmosher/eslint-plugin-import/pull/365
[#359]: https://github.com/benmosher/eslint-plugin-import/pull/359
[#343]: https://github.com/benmosher/eslint-plugin-import/pull/343
[#332]: https://github.com/benmosher/eslint-plugin-import/pull/332
Expand Down Expand Up @@ -258,6 +263,7 @@ for info on changes for earlier releases.
[#313]: https://github.com/benmosher/eslint-plugin-import/issues/313
[#286]: https://github.com/benmosher/eslint-plugin-import/issues/286
[#281]: https://github.com/benmosher/eslint-plugin-import/issues/281
[#275]: https://github.com/benmosher/eslint-plugin-import/issues/275
[#272]: https://github.com/benmosher/eslint-plugin-import/issues/272
[#267]: https://github.com/benmosher/eslint-plugin-import/issues/267
[#266]: https://github.com/benmosher/eslint-plugin-import/issues/266
Expand Down Expand Up @@ -316,3 +322,4 @@ for info on changes for earlier releases.
[@jkimbo]: https://github.com/jkimbo
[@le0nik]: https://github.com/le0nik
[@scottnonnenberg]: https://github.com/scottnonnenberg
[@sindresorhus]: https://github.com/sindresorhus
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,33 @@ settings:

[`jsnext:main`]: https://github.com/rollup/rollup/wiki/jsnext:main

#### `import/core-modules`

An array of additional modules to consider as "core" modules--modules that should
be considered resolved but have no path on the filesystem. Your resolver may
already define some of these (for example, the Node resolver knows about `fs` and
`path`), so you need not redefine those.

For example, Electron exposes an `electron` module:

```js
import 'electron' // without extra config, will be flagged as unresolved!
```

that would otherwise be unresolved. To avoid this, you may provide `electron` as a
core module:

```yaml
# .eslintrc.yml
settings:
import/core-modules: [ electron ]
```

In Electron's specific case, there is a shared config named `electron`
that specifies this for you.

Contribution of more such shared configs for other platforms are welcome!

#### `import/resolver`

See [resolvers](#resolvers).
Expand Down
8 changes: 8 additions & 0 deletions config/electron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Default settings for Electron applications.
*/
module.exports = {
settings: {
'import/core-modules': ['electron'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus switched to this model where the core module lists are not baked in, but made a shared config for Electron that can be maintained by the community.

Another such config can be made for any of Atom, Meteor, etc..

Avoids a constant stream of breaking-change platform-related PRs, let's people solve such problems quickly. I think it's better than baking the sets into the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me.

},
}
11 changes: 6 additions & 5 deletions src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ function constant(value) {
return () => value
}

export function isBuiltIn(name) {
return builtinModules.indexOf(name) !== -1
export function isBuiltIn(name, settings) {
const extras = (settings && settings['import/core-modules']) || []
return builtinModules.indexOf(name) !== -1 || extras.indexOf(name) > -1
}

const externalModuleRegExp = /^\w/
function isExternalModule(name, path) {
function isExternalModule(name, settings, path) {
if (!externalModuleRegExp.test(name)) return false
return (!path || -1 < path.indexOf(join('node_modules', name)))
}
Expand All @@ -23,7 +24,7 @@ function isScoped(name) {
return scopedRegExp.test(name)
}

function isInternalModule(name, path) {
function isInternalModule(name, settings, path) {
if (!externalModuleRegExp.test(name)) return false
return (path && -1 === path.indexOf(join('node_modules', name)))
}
Expand Down Expand Up @@ -53,5 +54,5 @@ const typeTest = cond([
])

export default function resolveImportType(name, context) {
return typeTest(name, resolve(name, context))
return typeTest(name, context.settings, resolve(name, context))
}
26 changes: 17 additions & 9 deletions src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ function fileExistsWithCaseSync(filepath, cacheSettings) {
}

export function relative(modulePath, sourceFile, settings) {
return fullResolve(modulePath, sourceFile, settings).path
}

function fullResolve(modulePath, sourceFile, settings) {
// check if this is a bonus core module
const coreSet = new Set(settings['import/core-modules'])
if (coreSet != null && coreSet.has(modulePath)) return { found: true, path: null }

const sourceDir = path.dirname(sourceFile)
, cacheKey = sourceDir + hashObject(settings) + modulePath
Expand All @@ -68,11 +75,10 @@ export function relative(modulePath, sourceFile, settings) {
}

const cachedPath = checkCache(cacheKey, cacheSettings)
if (cachedPath !== undefined) return cachedPath
if (cachedPath !== undefined) return { found: true, path: cachedPath }

function cache(resolvedPath) {
cachePath(cacheKey, resolvedPath)
return resolvedPath
}

function withResolver(resolver, config) {
Expand Down Expand Up @@ -108,19 +114,21 @@ export function relative(modulePath, sourceFile, settings) {

for (let [name, config] of resolvers) {
const resolver = requireResolver(name, sourceFile)
, resolved = withResolver(resolver, config)

let { path: fullPath, found } = withResolver(resolver, config)
if (!resolved.found) continue

// resolvers imply file existence, this double-check just ensures the case matches
if (found && !fileExistsWithCaseSync(fullPath, cacheSettings)) {
// reject resolved path
fullPath = undefined
}
if (!fileExistsWithCaseSync(resolved.path, cacheSettings)) continue

if (found) return cache(fullPath)
// else, counts
cache(resolved.path)
return resolved
}

return cache(undefined)
// failed
// cache(undefined)
return { found: false }
}

function resolverReducer(resolvers, map) {
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export const configs = {
'errors': require('../config/errors'),
'warnings': require('../config/warnings'),

// useful stuff for folks using React
'react': require('../config/react'),

// shhhh... work in progress "secret" rules
'stage-0': require('../config/stage-0'),

// useful stuff for folks using various environments
'react': require('../config/react'),
'electron': require('../config/electron'),
}
2 changes: 1 addition & 1 deletion src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function (context) {
const importPath = source.value

// don't enforce anything on builtins
if (isBuiltIn(importPath)) return
if (isBuiltIn(importPath, context.settings)) return

const resolvedPath = resolve(importPath, context)

Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ describe('importType(name)', function () {
expect(importType('/malformed', context)).to.equal('unknown')
expect(importType(' foo', context)).to.equal('unknown')
})

it("should return 'builtin' for additional core modules", function() {
// without extra config, should be marked external
expect(importType('electron', context)).to.equal('external')

const electronContext = testContext({ 'import/core-modules': ['electron'] })
expect(importType('electron', electronContext)).to.equal('builtin')
})
})
2 changes: 2 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ ruleTester.run('no-extraneous-dependencies', rule, {
test({ code: 'import "lodash.isarray"'}),
test({ code: 'import "@scope/core"'}),

test({ code: 'import "electron"', settings: { 'import/core-modules': ['electron'] } }),

// 'project' type
test({
code: 'import "importType"',
Expand Down
15 changes: 15 additions & 0 deletions tests/src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,18 @@ ruleTester.run('no-unresolved unknown resolver', rule, {
}),
],
})

ruleTester.run('no-unresolved electron', rule, {
valid: [
test({
code: 'import "electron"',
settings: { 'import/core-modules': ['electron'] },
}),
],
invalid:[
test({
code: 'import "electron"',
errors: [`Unable to resolve path to module 'electron'.`],
}),
],
})