From e120b2e26c45170ce66d1a56410714a464d6919e Mon Sep 17 00:00:00 2001 From: "Gavri (Gabriel) Guy" Date: Thu, 19 May 2016 08:38:07 +0300 Subject: [PATCH] allow multiple named export through variable deconstructing fixes #342 --- docs/rules/prefer-default-export.md | 7 +++++++ package.json | 1 + src/rules/prefer-default-export.js | 10 +++++++++- tests/src/rules/prefer-default-export.js | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/rules/prefer-default-export.md b/docs/rules/prefer-default-export.md index 892abfa38..c4381841e 100644 --- a/docs/rules/prefer-default-export.md +++ b/docs/rules/prefer-default-export.md @@ -49,3 +49,10 @@ export { foo, bar } const foo = 'foo'; export { foo as default } ``` + +```javascript +// good5.js + +// export multiple vars through deconstructing. +export const { foo, bar } = baz; +``` diff --git a/package.json b/package.json index 009426ebe..d4217a04e 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "lodash.endswith": "^4.0.1", "lodash.find": "^4.3.0", "lodash.findindex": "^4.3.0", + "lodash.get": "4.3.0", "object-assign": "^4.0.1", "pkg-up": "^1.0.0" } diff --git a/src/rules/prefer-default-export.js b/src/rules/prefer-default-export.js index 871cd8187..4a41c75ec 100644 --- a/src/rules/prefer-default-export.js +++ b/src/rules/prefer-default-export.js @@ -1,5 +1,8 @@ 'use strict' +import get from 'lodash.get' +// import forEach from 'lodash.forEach' + module.exports = function(context) { let namedExportCount = 0 let specifierExportCount = 0 @@ -15,7 +18,12 @@ module.exports = function(context) { } }, 'ExportNamedDeclaration': function(node) { - namedExportCount++ + const properties = get(node, 'declaration.declarations[0].id.properties') + if (properties) { + namedExportCount = namedExportCount + properties.length + } else { + namedExportCount++ + } namedExportNode = node }, 'ExportDefaultDeclaration': function() { diff --git a/tests/src/rules/prefer-default-export.js b/tests/src/rules/prefer-default-export.js index c3827eeba..6f37249f8 100644 --- a/tests/src/rules/prefer-default-export.js +++ b/tests/src/rules/prefer-default-export.js @@ -25,6 +25,11 @@ ruleTester.run('prefer-default-export', rule, { code: ` export { foo as default }`, }), + test({ + code: ` + export const { foo, bar } = baz;`, + }), + ], invalid: [ test({ @@ -44,5 +49,13 @@ ruleTester.run('prefer-default-export', rule, { message: 'Prefer default export.', }], }), + test({ + code: ` + export const { foo } = baz;`, + errors: [{ + ruleId: 'ExportNamedDeclaration', + message: 'Prefer default export.', + }], + }), ], })