Skip to content

Commit

Permalink
Merge pull request #211 from benmosher/issue-202-hoisting
Browse files Browse the repository at this point in the history
hoisting
  • Loading branch information
benmosher committed Mar 11, 2016
2 parents 47d6dea + 10fb144 commit 97c0d5f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
54 changes: 30 additions & 24 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,43 @@ module.exports = function (context) {

return {

'ImportDeclaration': function (declaration) {
if (declaration.specifiers.length === 0) return
// pick up all imports at body entry time, to properly respect hoisting
'Program': function ({ body }) {
function processBodyStatement(declaration) {
if (declaration.type !== 'ImportDeclaration') return

const imports = Exports.get(declaration.source.value, context)
if (imports == null) return null
if (declaration.specifiers.length === 0) return

if (imports.errors.length) {
imports.reportErrors(context, declaration)
return
}
const imports = Exports.get(declaration.source.value, context)
if (imports == null) return null

if (imports.errors.length) {
imports.reportErrors(context, declaration)
return
}

for (let specifier of declaration.specifiers) {
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
if (!imports.hasNamed) {
context.report(specifier,
`No exported names found in module '${declaration.source.value}'.`)
for (let specifier of declaration.specifiers) {
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
if (!imports.hasNamed) {
context.report(specifier,
`No exported names found in module '${declaration.source.value}'.`)
}
namespaces.set(specifier.local.name, imports.named)
break
case 'ImportDefaultSpecifier':
case 'ImportSpecifier': {
const meta = imports.named.get(
// default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
specifier.imported ? specifier.imported.name : 'default')
if (!meta || !meta.namespace) break
namespaces.set(specifier.local.name, meta.namespace)
break
}
namespaces.set(specifier.local.name, imports.named)
break
case 'ImportDefaultSpecifier':
case 'ImportSpecifier': {
const meta = imports.named.get(
// default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
specifier.imported ? specifier.imported.name : 'default')
if (!meta || !meta.namespace) break
namespaces.set(specifier.local.name, meta.namespace)
break
}
}
}
body.forEach(processBodyStatement)
},

// same as above, but does not add names to local map
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (context) {
, namespaces = new Map()

function checkSpecifiers(node) {
if (node.type !== 'ImportDeclaration') return
if (node.source == null) return // local export, ignore

const imports = Exports.get(node.source.value, context)
Expand Down Expand Up @@ -64,7 +65,7 @@ module.exports = function (context) {
}

return {
'ImportDeclaration': checkSpecifiers,
'Program': ({ body }) => body.forEach(checkSpecifiers),

'Identifier': function (node) {
if (node.parent.type === 'MemberExpression' && node.parent.property === node) {
Expand Down
22 changes: 22 additions & 0 deletions tests/src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ const valid = [
parser: 'babel-eslint',
}),

// respect hoisting
test({
code:
'function x() { console.log((names.b).c); } ' +
'import * as names from "./named-exports"; ',
}),

]

const invalid = [
Expand Down Expand Up @@ -138,6 +145,21 @@ const invalid = [
code: "import b from './deep/default'; console.log(b.e)",
errors: [ "'e' not found in imported namespace 'b'." ],
}),

// respect hoisting
test({
code:
'console.log(names.c);' +
"import * as names from './named-exports'; ",
errors: [error('c', 'names')],
}),
test({
code:
'function x() { console.log(names.c) } ' +
"import * as names from './named-exports'; ",
errors: [error('c', 'names')],
}),

]

///////////////////////
Expand Down
22 changes: 22 additions & 0 deletions tests/src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,25 @@ ruleTester.run('no-deprecated', rule, {
}),
],
})

ruleTester.run('no-deprecated: hoisting', rule, {
valid: [

test({
code: "function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) } import { deepDep } from './deep-deprecated'",
}),

],

invalid: [

test({
code: "console.log(MY_TERRIBLE_ACTION); import { MY_TERRIBLE_ACTION } from './deprecated'",
errors: [
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },
],
}),

],
})

0 comments on commit 97c0d5f

Please sign in to comment.