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

Consider ecmaFeatures.jsx during dependency parse #95

Merged
merged 2 commits into from
Nov 8, 2015
Merged
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
21 changes: 12 additions & 9 deletions src/core/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import isIgnored from './ignore'
var exportCache = new Map()

export default class ExportMap {
constructor(settings) {
this.settings = settings
constructor(context) {
this.context = context
this.named = new Set()

this.errors = []
}

get settings() { return this.context && this.context.settings }

get hasDefault() { return this.named.has('default') }
get hasNamed() { return this.named.size > (this.hasDefault ? 1 : 0) }

Expand All @@ -23,14 +25,14 @@ export default class ExportMap {
var path = resolve(source, context)
if (path == null || isIgnored(path, context)) return null

return ExportMap.for(path, context.settings)
return ExportMap.for(path, context)
}

static for(path, settings) {
static for(path, context) {
var exportMap = exportCache.get(path)
if (exportMap != null) return exportMap

exportMap = ExportMap.parse(path, settings)
exportMap = ExportMap.parse(path, context)

exportCache.set(path, exportMap)

Expand All @@ -40,11 +42,11 @@ export default class ExportMap {
return exportMap
}

static parse(path, settings) {
var m = new ExportMap(settings)
static parse(path, context) {
var m = new ExportMap(context)

try {
var ast = parse(path, settings)
var ast = parse(path, context)
} catch (err) {
m.errors.push(err)
return m // can't continue
Expand All @@ -63,7 +65,7 @@ export default class ExportMap {
var remotePath = resolve.relative(node.source.value, base, this.settings)
if (remotePath == null) return null

return ExportMap.for(remotePath, this.settings)
return ExportMap.for(remotePath, this.context)
}

captureDefault(n) {
Expand Down Expand Up @@ -125,3 +127,4 @@ export default class ExportMap {
}.bind(this))
}
}
//
22 changes: 17 additions & 5 deletions src/core/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ const defaultParseOptions = { ecmaVersion: 6 // for espree, esprima. not needed
, sourceType: "module"
}

export default function parse(path, settings) {
const parser = require(settings['import/parser'] || "babylon")
export default function parse(path, context) {
const { settings } = context
, parser = settings['import/parser'] || "babylon"

const { parse } = require(parser)
, options = Object.assign( {}
, defaultParseOptions
, settings['import/parse-options'])

const ast = parser.parse( fs.readFileSync(path, {encoding: 'utf8'})
, options
)
// detect and handle "jsx" ecmaFeature
if (context.ecmaFeatures && parser === "babylon") {
const { jsx } = context.ecmaFeatures
if (jsx && (!options.plugins || options.plugins.indexOf('jsx') < 0)) {
if (!options.plugins) options.plugins = ['jsx']
else options.plugins.push('jsx')
}
}

const ast = parse( fs.readFileSync(path, {encoding: 'utf8'})
, options
)

// bablyon returns top-level "File" node.
return ast.type === 'File' ? ast.program : ast
Expand Down
2 changes: 1 addition & 1 deletion src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function fileExists(filepath) {
function opts(basedir, settings) {
// pulls all items from 'import/resolve'
return Object.assign( { }
, settings['import/resolve']
, settings && settings['import/resolve']
, { basedir: basedir }
)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/files/jsx/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { Component } from 'react'

// class App extends Component {
// render() {
// return <div>hello, JSX</div>
// }
// }

export default connect()(App);
2 changes: 2 additions & 0 deletions tests/files/redux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// redux idiom
export default connect()(App);
6 changes: 3 additions & 3 deletions tests/src/core/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ describe('getExports', function () {

it('finds exports for an ES7 module with babel-eslint', function () {
var imports = ExportMap.parse( getFilename('jsx/FooES7.js')
, { 'import/parser': 'babel-eslint' })
, { settings: { 'import/parser': 'babel-eslint' } })

expect(imports).to.exist
expect(imports).to.have.property('hasDefault', true)
expect(imports.named.has('Bar')).to.be.true
})

it('finds exports for an ES7 module with proper parse options', function () {
var imports = ExportMap.parse(getFilename('jsx/FooES7.js'), {
var imports = ExportMap.parse(getFilename('jsx/FooES7.js'), { settings: {
'import/parse-options': {
plugins: [
'decorators',
Expand All @@ -68,7 +68,7 @@ describe('getExports', function () {
'objectRestSpread'
]
}
})
}})

expect(imports).to.exist
expect(imports).to.have.property('hasDefault', true)
Expand Down
9 changes: 9 additions & 0 deletions tests/src/rules/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ ruleTester.run('default', rule, {
, test({ code: 'import twofer from "./trampoline"'
, settings: { 'import/parse-options': { plugins: ['exportExtensions']}}
})
// jsx
, test({ code: 'import MyCoolComponent from "./jsx/MyCoolComponent.jsx"'
, ecmaFeatures: { jsx: true, modules: true }
})

// #54: import of named export default
, test({ code: 'import foo from "./named-default-export"' })

// #94: redux export of execution result
, test({ code: 'import connectedApp from "./redux"' })
, test({ code: 'import App from "./jsx/App"'
, ecmaFeatures: { jsx: true, modules: true }
})
],

invalid: [
Expand Down