Skip to content

Commit

Permalink
import-style: Fix crash on destructing require (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 24, 2020
1 parent 0caace1 commit 8335d41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rules/import-style.js
Expand Up @@ -69,7 +69,7 @@ const getActualExportDeclarationStyles = exportDeclaration => {
};

const getActualAssignmentTargetImportStyles = assignmentTarget => {
if (assignmentTarget.type === 'Identifier') {
if (assignmentTarget.type === 'Identifier' || assignmentTarget.type === 'ArrayPattern') {
return ['namespace'];
}

Expand All @@ -81,6 +81,11 @@ const getActualAssignmentTargetImportStyles = assignmentTarget => {
const styles = new Set();

for (const property of assignmentTarget.properties) {
if (property.type === 'RestElement') {
styles.add('named');
continue;
}

if (property.key.type === 'Identifier') {
if (property.key.name === 'default') {
styles.add('default');
Expand Down
23 changes: 23 additions & 0 deletions test/import-style.js
Expand Up @@ -75,6 +75,7 @@ test({

'const x = require(\'default\')',
'const {default: x} = require(\'default\')',
'const [] = require("default")',
'import x from \'default\'',
outdent`
async () => {
Expand All @@ -84,6 +85,7 @@ test({
'export {default} from \'default\'',

'const x = require(\'namespace\')',
'const [] = require("namespace")',
'import * as x from \'namespace\'',
outdent`
async () => {
Expand All @@ -93,6 +95,7 @@ test({
'export * from \'namespace\'',

'const {x} = require(\'named\')',
'const {...rest} = require("named")',
'const {x: y} = require(\'named\')',
'import {x} from \'named\'',
'import {x as y} from \'named\'',
Expand Down Expand Up @@ -268,6 +271,14 @@ test({
`,
errors: [unassignedError]
},
{
code: 'const {...rest} = require("unassigned")',
errors: [unassignedError]
},
{
code: 'const [] = require("unassigned")',
errors: [unassignedError]
},
{
code: 'export * from \'unassigned\'',
errors: [unassignedError]
Expand All @@ -293,6 +304,10 @@ test({
code: 'const {} = require(\'default\')',
errors: [defaultError]
},
{
code: 'const {...rest} = require("default")',
errors: [defaultError]
},
{
code: 'import \'default\'',
errors: [defaultError]
Expand Down Expand Up @@ -386,6 +401,10 @@ test({
code: 'const {default: x} = require(\'namespace\')',
errors: [namespaceError]
},
{
code: 'const {...rest} = require("namespace")',
errors: [namespaceError]
},
{
code: 'import x from \'namespace\'',
errors: [namespaceError]
Expand Down Expand Up @@ -443,6 +462,10 @@ test({
code: 'const {} = require(\'named\')',
errors: [namedError]
},
{
code: 'const [] = require("named")',
errors: [namedError]
},
{
code: 'import \'named\'',
errors: [namedError]
Expand Down

0 comments on commit 8335d41

Please sign in to comment.