diff --git a/src/utils/parseAssertions.ts b/src/utils/parseAssertions.ts index 7a54a172777..4a0f535477a 100644 --- a/src/utils/parseAssertions.ts +++ b/src/utils/parseAssertions.ts @@ -19,7 +19,7 @@ export function getAssertionsFromImportExpression(node: ImportExpression): Recor const key = getPropertyKey(property); if ( typeof key === 'string' && - typeof ((property as Property).value as Literal)?.value === 'string' + typeof ((property as Property).value as Literal).value === 'string' ) { return [key, ((property as Property).value as Literal).value] as [string, string]; } @@ -34,9 +34,10 @@ export function getAssertionsFromImportExpression(node: ImportExpression): Recor const getPropertyKey = ( property: Property | SpreadElement | ImportAttribute -): LiteralValue | undefined => - ((property as Property | ImportAttribute).key as Identifier).name || - ((property as Property | ImportAttribute).key as Literal).value; +): LiteralValue | undefined => { + const key = (property as Property | ImportAttribute).key; + return key && ((key as Identifier).name || (key as Literal).value); +}; export function getAssertionsFromImportExportDeclaration( assertions: ImportAttribute[] | undefined diff --git a/test/form/samples/import-assertions/assertion-shapes/_config.js b/test/form/samples/import-assertions/assertion-shapes/_config.js new file mode 100644 index 00000000000..3686c79e002 --- /dev/null +++ b/test/form/samples/import-assertions/assertion-shapes/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'handles special shapes of assertions', + expectedWarnings: 'UNRESOLVED_IMPORT', + options: { + external: () => true + } +}; diff --git a/test/form/samples/import-assertions/assertion-shapes/_expected.js b/test/form/samples/import-assertions/assertion-shapes/_expected.js new file mode 100644 index 00000000000..b5d8cbaa42d --- /dev/null +++ b/test/form/samples/import-assertions/assertion-shapes/_expected.js @@ -0,0 +1,4 @@ +import('external-a', { assert: { type: 'json' } }); +import('external-b'); +import('external-c'); +import('external-d'); diff --git a/test/form/samples/import-assertions/assertion-shapes/main.js b/test/form/samples/import-assertions/assertion-shapes/main.js new file mode 100644 index 00000000000..a82cad812bc --- /dev/null +++ b/test/form/samples/import-assertions/assertion-shapes/main.js @@ -0,0 +1,4 @@ +import('external-a', { 'assert': { 'type': 'json', foo: 1, ...{} } }); +import('external-b', { assert: {} }); +import('external-c', { ...{} }); +import('external-d', {});