Skip to content

Commit

Permalink
tests passing for "plugin" node resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Nov 20, 2015
1 parent 8fb2a0a commit 33c77f0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"ci-test": "npm run-script pretest && mocha --recursive --reporter dot tests/lib/",
"debug": "mocha debug --recursive --reporter dot tests/lib/",
"compile": "rm -rf lib/ && babel -d lib/ src/",
"prepublish": "npm run compile",
"pretest": "eslint ./src && npm run compile && babel -d tests/lib/ tests/src/",
"prepublish": "eslint ./src && npm run compile",
"pretest": "npm run compile && babel -d tests/lib/ tests/src/",
"coveralls": "istanbul cover --report lcovonly --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot && remap-istanbul -i reports/coverage/coverage.json -o reports/coverage/lcov.info --type lcovonly && cat ./reports/coverage/lcov.info | coveralls"
},
"repository": {
Expand Down
7 changes: 4 additions & 3 deletions resolvers/node/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import resolve from 'resolve'
var resolve = require('resolve')
, path = require('path')

export default function resolveImport(source, file, settings) {
exports.resolveImport = function resolveImport(source, file, settings) {
if (resolve.isCore(source)) return null

return resolve.sync(source, opts(file, settings))
return resolve.sync(source, opts(path.dirname(file), settings))
}

function opts(basedir, settings) {
Expand Down
57 changes: 25 additions & 32 deletions src/core/resolve.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs')
import fs from 'fs'
import { dirname, basename, join } from 'path'

import defaultResolve from '../../resolvers/node'
import * as defaultResolve from '../../resolvers/node'

const CASE_INSENSITIVE = fs.existsSync(join(__dirname, 'reSOLVE.js'))

Expand All @@ -25,22 +25,17 @@ function fileExists(filepath) {
}
}

/**
* wrapper around resolve
* @param {string} p - module path
* @param {object} context - ESLint context
* @return {string} - the full module filesystem path;
* null if package is core;
* undefined if not found
*/
module.exports = function (p, context) {
export function relative(modulePath, sourceFile, settings) {

function withResolver(resolver) {
// resolve just returns the core module id, which won't appear to exist
try {
const file = resolver(p, dirname(context.getFilename()))
if (file === null) return null
const filePath = resolver.resolveImport(modulePath, sourceFile, settings)
if (filePath === null) return null

if (filePath === undefined || !fileExists(filePath)) return undefined

if (file === undefined || !fileExists(file)) return undefined
return filePath
} catch (err) {
return undefined
}
Expand All @@ -51,26 +46,24 @@ module.exports = function (p, context) {
const resolvers = [ defaultResolve ]

for (let resolver of resolvers) {
let file = withResolver(resolver)
if (file) return file
let fullPath = withResolver(resolver)
if (fullPath !== undefined) return fullPath
}

return undefined
}

// todo: only this? or default function wraps this
module.exports.relative = function (p, r, settings) {
try {

var file = resolve.sync(p, opts(path.dirname(r), settings))
if (!fileExists(file)) return null
return file

} catch (err) {

if (err.message.indexOf('Cannot find module') === 0) return null

throw err // else

}
/**
* Givent
* @param {string} p - module path
* @param {object} context - ESLint context
* @return {string} - the full module filesystem path;
* null if package is core;
* undefined if not found
*/
export default function resolve(p, context) {
return relative( p
, context.getFilename()
, context.settings
)
}
resolve.relative = relative
2 changes: 1 addition & 1 deletion src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (context) {
function checkSourceValue(source) {
if (source == null) return

if (resolve(source.value, context) == null) {
if (resolve(source.value, context) === undefined) {
context.report(source,
'Unable to resolve path to module \'' + source.value + '\'.')
}
Expand Down
10 changes: 4 additions & 6 deletions tests/src/core/resolve.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
import { expect } from 'chai'
import resolve from 'core/resolve'

var expect = require('chai').expect
, resolve = require('../../../lib/core/resolve')

var utils = require('../utils')
import * as utils from '../utils'

describe('resolve', function () {
it('should throw on bad parameters.', function () {
Expand All @@ -24,6 +22,6 @@ describe('resolve', function () {
, utils.testContext({ 'import/resolve': { 'extensions': ['.jsx'] }})
)

expect(file).to.equal(null)
expect(file, 'path to ./jsx/MyUncoolComponent').to.be.undefined
})
})

0 comments on commit 33c77f0

Please sign in to comment.