Fix import assertion occurrences crash and make import assertion parsing more generous (#47535)

This commit is contained in:
Wesley Wigham
2022-01-20 14:08:47 -08:00
committed by GitHub
parent 04d77fe900
commit bae0f50818
15 changed files with 154 additions and 14 deletions

View File

@@ -39786,6 +39786,16 @@ namespace ts {
return false;
}
}
if (!isImportEqualsDeclaration(node) && node.assertClause) {
let hasError = false;
for (const clause of node.assertClause.elements) {
if (!isStringLiteral(clause.value)) {
hasError = true;
error(clause.value, Diagnostics.Import_assertion_values_must_be_string_literal_expressions);
}
}
return !hasError;
}
return true;
}

View File

@@ -3369,6 +3369,10 @@
"category": "Error",
"code": 2836
},
"Import assertion values must be string literal expressions.": {
"category": "Error",
"code": 2837
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",

View File

@@ -4024,7 +4024,7 @@ namespace ts {
}
// @api
function createAssertEntry(name: AssertionKey, value: StringLiteral): AssertEntry {
function createAssertEntry(name: AssertionKey, value: Expression): AssertEntry {
const node = createBaseNode<AssertEntry>(SyntaxKind.AssertEntry);
node.name = name;
node.value = value;
@@ -4033,7 +4033,7 @@ namespace ts {
}
// @api
function updateAssertEntry(node: AssertEntry, name: AssertionKey, value: StringLiteral): AssertEntry {
function updateAssertEntry(node: AssertEntry, name: AssertionKey, value: Expression): AssertEntry {
return node.name !== name
|| node.value !== value
? update(createAssertEntry(name, value), node)

View File

@@ -7281,7 +7281,7 @@ namespace ts {
const pos = getNodePos();
const name = tokenIsIdentifierOrKeyword(token()) ? parseIdentifierName() : parseLiteralLikeNode(SyntaxKind.StringLiteral) as StringLiteral;
parseExpected(SyntaxKind.ColonToken);
const value = parseLiteralLikeNode(SyntaxKind.StringLiteral) as StringLiteral;
const value = parseAssignmentExpressionOrHigher();
return finishNode(factory.createAssertEntry(name, value), pos);
}

View File

@@ -3055,7 +3055,7 @@ namespace ts {
readonly kind: SyntaxKind.AssertEntry;
readonly parent: AssertClause;
readonly name: AssertionKey;
readonly value: StringLiteral;
readonly value: Expression;
}
export interface AssertClause extends Node {
@@ -7446,8 +7446,8 @@ namespace ts {
updateImportClause(node: ImportClause, isTypeOnly: boolean, name: Identifier | undefined, namedBindings: NamedImportBindings | undefined): ImportClause;
createAssertClause(elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause;
updateAssertClause(node: AssertClause, elements: NodeArray<AssertEntry>, multiLine?: boolean): AssertClause;
createAssertEntry(name: AssertionKey, value: StringLiteral): AssertEntry;
updateAssertEntry (node: AssertEntry, name: AssertionKey, value: StringLiteral): AssertEntry;
createAssertEntry(name: AssertionKey, value: Expression): AssertEntry;
updateAssertEntry(node: AssertEntry, name: AssertionKey, value: Expression): AssertEntry;
createNamespaceImport(name: Identifier): NamespaceImport;
updateNamespaceImport(node: NamespaceImport, name: Identifier): NamespaceImport;
createNamespaceExport(name: Identifier): NamespaceExport;

View File

@@ -1090,7 +1090,7 @@ namespace ts {
Debug.type<AssertEntry>(node);
return factory.updateAssertEntry(node,
nodeVisitor(node.name, visitor, isAssertionKey),
nodeVisitor(node.value, visitor, isStringLiteral));
nodeVisitor(node.value, visitor, isExpressionNode));
case SyntaxKind.ImportClause:
Debug.type<ImportClause>(node);