-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exports-last will check that all export statements are at the end of the file Closes #620
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function isExportStatement({ type }) { | ||
// ES Module export statements | ||
if (type === 'ExportDefaultDeclaration' || type === 'ExportNamedDeclaration') { | ||
return true | ||
|
||
// CommonJS export statements | ||
} else if (type === 'ExpressionStatement') { | ||
// TODO | ||
} | ||
|
||
return false | ||
} | ||
|
||
const rule = { | ||
create(context) { | ||
return { | ||
Program({ body }) { | ||
const lastNonExportStatement = body.reduce((acc, node, index) => { | ||
if (isExportStatement(node)) { | ||
return acc | ||
} | ||
return index | ||
}, 0) | ||
|
||
body.forEach((node, index) => { | ||
if (isExportStatement(node) && index < lastNonExportStatement) { | ||
|
||
context.report({ | ||
node, | ||
message: 'Export statements should appear at the end of the file', | ||
}) | ||
} | ||
}) | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
export default rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
import rule from 'rules/exports-last' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
const errors = ['Export statements should appear at the end of the file'] | ||
|
||
ruleTester.run('exports-last', rule, { | ||
valid: [ | ||
test({ | ||
code: ` | ||
const foo = 'bar'; | ||
const bar = 'baz'; | ||
`, | ||
}), | ||
test({ | ||
code: ` | ||
const foo = 'bar'; | ||
export {foo}; | ||
`, | ||
}), | ||
test({ | ||
code: ` | ||
const foo = 'bar'; | ||
export default foo; | ||
`, | ||
}), | ||
test({ | ||
code: ` | ||
const foo = 'bar'; | ||
export default foo; | ||
export const bar = true; | ||
`, | ||
}), | ||
// test({ | ||
// code: ` | ||
// const foo = 'bar'; | ||
// module.exports = foo | ||
// `, | ||
// }), | ||
// test({ | ||
// code: ` | ||
// const foo = 'bar'; | ||
// module.exports = foo; | ||
// exports.bar = true | ||
// `, | ||
// }), | ||
|
||
], | ||
invalid: [ | ||
test({ | ||
code: ` | ||
export default 'bar'; | ||
const bar = true; | ||
`, | ||
errors, | ||
}), | ||
test({ | ||
code: ` | ||
export const foo = 'bar'; | ||
const bar = true; | ||
`, | ||
errors, | ||
}), | ||
// test({ | ||
// code: ` | ||
// module.exports = 'bar'; | ||
// console.log('hi'); | ||
// `, | ||
// errors, | ||
// }), | ||
// test({ | ||
// code: ` | ||
// exports.foo = 'bar'; | ||
// const bar = true; | ||
// `, | ||
// errors, | ||
// }), | ||
], | ||
}) |