mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-07-02 06:35:09 -05:00
Fix ambient initializer emit for bigint
This commit is contained in:
@@ -30128,6 +30128,12 @@ namespace ts {
|
||||
(<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.NumericLiteral;
|
||||
}
|
||||
|
||||
function isBigIntLiteralExpression(expr: Expression) {
|
||||
return expr.kind === SyntaxKind.BigIntLiteral ||
|
||||
expr.kind === SyntaxKind.PrefixUnaryExpression && (<PrefixUnaryExpression>expr).operator === SyntaxKind.MinusToken &&
|
||||
(<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.BigIntLiteral;
|
||||
}
|
||||
|
||||
function isSimpleLiteralEnumReference(expr: Expression) {
|
||||
if (
|
||||
(isPropertyAccessExpression(expr) || (isElementAccessExpression(expr) && isStringOrNumberLiteralExpression(expr.argumentExpression))) &&
|
||||
@@ -30136,19 +30142,25 @@ namespace ts {
|
||||
}
|
||||
|
||||
function checkAmbientInitializer(node: VariableDeclaration | PropertyDeclaration | PropertySignature) {
|
||||
if (node.initializer) {
|
||||
const isInvalidInitializer = !(isStringOrNumberLiteralExpression(node.initializer) || isSimpleLiteralEnumReference(node.initializer) || node.initializer.kind === SyntaxKind.TrueKeyword || node.initializer.kind === SyntaxKind.FalseKeyword);
|
||||
const {initializer} = node;
|
||||
if (initializer) {
|
||||
const isInvalidInitializer = !(
|
||||
isStringOrNumberLiteralExpression(initializer) ||
|
||||
isSimpleLiteralEnumReference(initializer) ||
|
||||
initializer.kind === SyntaxKind.TrueKeyword || initializer.kind === SyntaxKind.FalseKeyword ||
|
||||
isBigIntLiteralExpression(initializer)
|
||||
);
|
||||
const isConstOrReadonly = isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node);
|
||||
if (isConstOrReadonly && !node.type) {
|
||||
if (isInvalidInitializer) {
|
||||
return grammarErrorOnNode(node.initializer!, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference);
|
||||
return grammarErrorOnNode(initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
return grammarErrorOnNode(initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
}
|
||||
if (!isConstOrReadonly || isInvalidInitializer) {
|
||||
return grammarErrorOnNode(node.initializer!, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
return grammarErrorOnNode(initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +74,10 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12
|
||||
bigintVal = dataView.getBigInt64(1);
|
||||
bigintVal = dataView.getBigInt64(1, true);
|
||||
bigintVal = dataView.getBigUint64(2);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
|
||||
// Test emitted declarations files
|
||||
const w = 12n; // should emit as const w = 12n
|
||||
const x = -12n; // should emit as const x = -12n
|
||||
const y: 12n = 12n; // should emit type 12n
|
||||
let z = 12n; // should emit type bigint in declaration file
|
||||
@@ -45,7 +45,13 @@ dataView.setBigUint64(2, 123); // should error
|
||||
bigintVal = dataView.getBigInt64(1);
|
||||
bigintVal = dataView.getBigInt64(1, true);
|
||||
bigintVal = dataView.getBigUint64(2);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
|
||||
// Test emitted declarations files
|
||||
const w = 12n; // should emit as const w = 12n
|
||||
const x = -12n; // should emit as const x = -12n
|
||||
const y: 12n = 12n; // should emit type 12n
|
||||
let z = 12n; // should emit type bigint in declaration file
|
||||
|
||||
//// [bigintWithLib.js]
|
||||
// Test BigInt functions
|
||||
@@ -92,3 +98,22 @@ bigintVal = dataView.getBigInt64(1);
|
||||
bigintVal = dataView.getBigInt64(1, true);
|
||||
bigintVal = dataView.getBigUint64(2);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
// Test emitted declarations files
|
||||
const w = 12n; // should emit as const w = 12n
|
||||
const x = -12n; // should emit as const x = -12n
|
||||
const y = 12n; // should emit type 12n
|
||||
let z = 12n; // should emit type bigint in declaration file
|
||||
|
||||
|
||||
//// [bigintWithLib.d.ts]
|
||||
declare let bigintVal: bigint;
|
||||
declare let stringVal: string;
|
||||
declare let bigIntArray: BigInt64Array;
|
||||
declare let len: number;
|
||||
declare let arrayBufferLike: ArrayBufferView;
|
||||
declare let bigUintArray: BigUint64Array;
|
||||
declare const dataView: DataView;
|
||||
declare const w = 12n;
|
||||
declare const x = -12n;
|
||||
declare const y: 12n;
|
||||
declare let z: bigint;
|
||||
|
||||
@@ -204,3 +204,16 @@ bigintVal = dataView.getBigUint64(2, true);
|
||||
>dataView : Symbol(dataView, Decl(bigintWithLib.ts, 36, 5))
|
||||
>getBigUint64 : Symbol(DataView.getBigUint64, Decl(lib.esnext.bigint.d.ts, --, --))
|
||||
|
||||
// Test emitted declarations files
|
||||
const w = 12n; // should emit as const w = 12n
|
||||
>w : Symbol(w, Decl(bigintWithLib.ts, 49, 5))
|
||||
|
||||
const x = -12n; // should emit as const x = -12n
|
||||
>x : Symbol(x, Decl(bigintWithLib.ts, 50, 5))
|
||||
|
||||
const y: 12n = 12n; // should emit type 12n
|
||||
>y : Symbol(y, Decl(bigintWithLib.ts, 51, 5))
|
||||
|
||||
let z = 12n; // should emit type bigint in declaration file
|
||||
>z : Symbol(z, Decl(bigintWithLib.ts, 52, 3))
|
||||
|
||||
|
||||
@@ -332,3 +332,21 @@ bigintVal = dataView.getBigUint64(2, true);
|
||||
>2 : 2
|
||||
>true : true
|
||||
|
||||
// Test emitted declarations files
|
||||
const w = 12n; // should emit as const w = 12n
|
||||
>w : 12n
|
||||
>12n : 12n
|
||||
|
||||
const x = -12n; // should emit as const x = -12n
|
||||
>x : -12n
|
||||
>-12n : -12n
|
||||
>12n : 12n
|
||||
|
||||
const y: 12n = 12n; // should emit type 12n
|
||||
>y : 12n
|
||||
>12n : 12n
|
||||
|
||||
let z = 12n; // should emit type bigint in declaration file
|
||||
>z : bigint
|
||||
>12n : 12n
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// @experimentalBigInt: true
|
||||
// @target: esnext
|
||||
// @declaration: true
|
||||
|
||||
// Test BigInt functions
|
||||
let bigintVal: bigint = BigInt(123);
|
||||
@@ -47,4 +48,10 @@ dataView.setBigUint64(2, 123); // should error
|
||||
bigintVal = dataView.getBigInt64(1);
|
||||
bigintVal = dataView.getBigInt64(1, true);
|
||||
bigintVal = dataView.getBigUint64(2);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
bigintVal = dataView.getBigUint64(2, true);
|
||||
|
||||
// Test emitted declarations files
|
||||
const w = 12n; // should emit as const w = 12n
|
||||
const x = -12n; // should emit as const x = -12n
|
||||
const y: 12n = 12n; // should emit type 12n
|
||||
let z = 12n; // should emit type bigint in declaration file
|
||||
Reference in New Issue
Block a user