Skip to content

Commit

Permalink
Throw on unexpected node types
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Feb 5, 2024
1 parent f309fa4 commit bd52a05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,13 @@ function getSourceWithControlCharacter(originalSource, item) {
// Exclude "ImportDefaultSpecifier" – the "def" in `import def, {a, b}`.
function getSpecifiers(importNode) {
switch (importNode.type) {
case "ImportDeclaration":
return importNode.specifiers.filter((node) => isImportSpecifier(node));
case "TSImportEqualsDeclaration":
return [];
default: // ImportDeclaration
return importNode.specifiers.filter((node) => isImportSpecifier(node));
// istanbul ignore next
default:
throw new Error(`Unsupported import node type: ${importNode.type}`);
}
}

Expand All @@ -179,9 +182,7 @@ function isImportSpecifier(node) {
// And not: import type {} from "setup"
function isSideEffectImport(importNode, sourceCode) {
switch (importNode.type) {
case "TSImportEqualsDeclaration":
return false;
default: // ImportDeclaration
case "ImportDeclaration":
return (
importNode.specifiers.length === 0 &&
(!importNode.importKind ||
Expand All @@ -191,5 +192,10 @@ function isSideEffectImport(importNode, sourceCode) {
"{"
)
);
case "TSImportEqualsDeclaration":
return false;
// istanbul ignore next
default:
throw new Error(`Unsupported import node type: ${importNode.type}`);
}
}
21 changes: 16 additions & 5 deletions src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,18 @@ function getSource(sourceCode, node) {

function getSourceTextAndKind(sourceCode, node) {
switch (node.type) {
case "ImportDeclaration":
case "ExportNamedDeclaration":
case "ExportAllDeclaration":
return [node.source.value, getImportExportKind(node)];
case "TSImportEqualsDeclaration":
return getSourceTextAndKindFromModuleReference(
sourceCode,
node.moduleReference
);
default: // ImportDeclaration, ExportNamedDeclaration, ExportAllDeclaration
return [node.source.value, getImportExportKind(node)];
// istanbul ignore next
default:
throw new Error(`Unsupported import node type: ${node.type}`);
}
}

Expand Down Expand Up @@ -873,19 +878,25 @@ function getSourceTextAndKindFromModuleReference(sourceCode, node) {
getSourceTextFromTSQualifiedName(sourceCode, node),
KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE,
];
default: // Identifier
case "Identifier":
return [node.name, KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE];
// istanbul ignore next
default:
throw new Error(`Unsupported module reference node type: ${node.type}`);
}
}

function getSourceTextFromTSQualifiedName(sourceCode, node) {
switch (node.left.type) {
case "Identifier":
return `${node.left.name}.${node.right.name}`;
case "TSQualifiedName":
return `${getSourceTextFromTSQualifiedName(sourceCode, node.left)}.${
node.right.name
}`;
default: // Identifier
return `${node.left.name}.${node.right.name}`;
// istanbul ignore next
default:
throw new Error(`Unsupported TS qualified name node type: ${node.type}`);
}
}

Expand Down

0 comments on commit bd52a05

Please sign in to comment.