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

hoisting #211

Merged
merged 2 commits into from
Mar 11, 2016
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
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.' },
],
}),

],
})