Skip to content

Commit 491d8b7

Browse files
authored
feat!(NODE-4440): bump TS target version to es2020 (#520)
1 parent 3d3d0dc commit 491d8b7

19 files changed

+83
-121
lines changed

docs/upgrade-to-v5.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ new TextDecoder('utf-16le').decode(bin.value(true));
4444
> **TL;DR**: TODO
4545
4646
TODO(NODE-4771): serializeFunctions bug fix makes function names outside the ascii range get serialized correctly
47+
48+
### TS "target" set to es2020
49+
50+
We have set our typescript compilation target to `es2020` which aligns with our minimum supported Node.js version 14+. The following is from the typescript release notes on es2020 support, so it's some of the syntax that can be expected to be preserved after compilation:
51+
52+
> This will preserve newer ECMAScript 2020 features like optional chaining, nullish coalescing, export * as ns, and dynamic import(...) syntax. It also means bigint literals now have a stable target below esnext.

rollup.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ const tsConfig = {
1212
checkJs: false,
1313
strict: true,
1414
alwaysStrict: true,
15-
target: 'es5',
15+
target: 'es2020',
1616
module: 'esnext',
1717
moduleResolution: 'node',
18-
lib: ['ES2017', 'ES2020.BigInt', 'ES2017.TypedArrays'],
18+
lib: ['es2020'],
1919
// We don't make use of tslib helpers
2020
importHelpers: true,
2121
noEmitHelpers: false,

src/binary.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export interface BinaryExtended {
2828
* @category BSONType
2929
*/
3030
export class Binary {
31-
_bsontype!: 'Binary';
31+
get _bsontype(): 'Binary' {
32+
return 'Binary';
33+
}
3234

3335
/**
3436
* Binary default subtype
@@ -73,8 +75,6 @@ export class Binary {
7375
* @param subType - the option binary type.
7476
*/
7577
constructor(buffer?: string | BinarySequence, subType?: number) {
76-
if (!(this instanceof Binary)) return new Binary(buffer, subType);
77-
7878
if (
7979
!(buffer == null) &&
8080
!(typeof buffer === 'string') &&
@@ -294,8 +294,6 @@ export class Binary {
294294
}
295295
}
296296

297-
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
298-
299297
/** @public */
300298
export type UUIDExtended = {
301299
$uuid: string;

src/code.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export interface CodeExtended {
1212
* @category BSONType
1313
*/
1414
export class Code {
15-
_bsontype!: 'Code';
15+
get _bsontype(): 'Code' {
16+
return 'Code';
17+
}
1618

1719
code!: string | Function;
1820
scope?: Document;
@@ -21,8 +23,6 @@ export class Code {
2123
* @param scope - an optional scope for the function.
2224
*/
2325
constructor(code: string | Function, scope?: Document) {
24-
if (!(this instanceof Code)) return new Code(code, scope);
25-
2626
this.code = code;
2727
this.scope = scope;
2828
}
@@ -57,5 +57,3 @@ export class Code {
5757
})`;
5858
}
5959
}
60-
61-
Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });

src/db_ref.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function isDBRefLike(value: unknown): value is DBRefLike {
2626
* @category BSONType
2727
*/
2828
export class DBRef {
29-
_bsontype!: 'DBRef';
29+
get _bsontype(): 'DBRef' {
30+
return 'DBRef';
31+
}
3032

3133
collection!: string;
3234
oid!: ObjectId;
@@ -39,8 +41,6 @@ export class DBRef {
3941
* @param db - optional db name, if omitted the reference is local to the current db.
4042
*/
4143
constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
42-
if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
43-
4444
// check if namespace has been provided
4545
const parts = collection.split('.');
4646
if (parts.length === 2) {
@@ -120,5 +120,3 @@ export class DBRef {
120120
})`;
121121
}
122122
}
123-
124-
Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });

src/decimal128.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ export interface Decimal128Extended {
127127
* @category BSONType
128128
*/
129129
export class Decimal128 {
130-
_bsontype!: 'Decimal128';
130+
get _bsontype(): 'Decimal128' {
131+
return 'Decimal128';
132+
}
131133

132134
readonly bytes!: Uint8Array;
133135

@@ -136,8 +138,6 @@ export class Decimal128 {
136138
* or a string representation as returned by .toString()
137139
*/
138140
constructor(bytes: Uint8Array | string) {
139-
if (!(this instanceof Decimal128)) return new Decimal128(bytes);
140-
141141
if (typeof bytes === 'string') {
142142
this.bytes = Decimal128.fromString(bytes).bytes;
143143
} else if (isUint8Array(bytes)) {
@@ -773,5 +773,3 @@ export class Decimal128 {
773773
return `new Decimal128("${this.toString()}")`;
774774
}
775775
}
776-
777-
Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });

src/double.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export interface DoubleExtended {
1111
* @category BSONType
1212
*/
1313
export class Double {
14-
_bsontype!: 'Double';
14+
get _bsontype(): 'Double' {
15+
return 'Double';
16+
}
1517

1618
value!: number;
1719
/**
@@ -20,8 +22,6 @@ export class Double {
2022
* @param value - the number we want to represent as a double.
2123
*/
2224
constructor(value: number) {
23-
if (!(this instanceof Double)) return new Double(value);
24-
2525
if ((value as unknown) instanceof Number) {
2626
value = value.valueOf();
2727
}
@@ -87,5 +87,3 @@ export class Double {
8787
return `new Double(${eJSON.$numberDouble})`;
8888
}
8989
}
90-
91-
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });

src/error.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
export class BSONError extends Error {
33
constructor(message: string) {
44
super(message);
5-
Object.setPrototypeOf(this, BSONError.prototype);
65
}
76

87
get name(): string {
@@ -14,7 +13,6 @@ export class BSONError extends Error {
1413
export class BSONTypeError extends TypeError {
1514
constructor(message: string) {
1615
super(message);
17-
Object.setPrototypeOf(this, BSONTypeError.prototype);
1816
}
1917

2018
get name(): string {

src/int_32.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export interface Int32Extended {
1111
* @category BSONType
1212
*/
1313
export class Int32 {
14-
_bsontype!: 'Int32';
14+
get _bsontype(): 'Int32' {
15+
return 'Int32';
16+
}
1517

1618
value!: number;
1719
/**
@@ -20,8 +22,6 @@ export class Int32 {
2022
* @param value - the number we want to represent as an int32.
2123
*/
2224
constructor(value: number | string) {
23-
if (!(this instanceof Int32)) return new Int32(value);
24-
2525
if ((value as unknown) instanceof Number) {
2626
value = value.valueOf();
2727
}
@@ -66,5 +66,3 @@ export class Int32 {
6666
return `new Int32(${this.valueOf()})`;
6767
}
6868
}
69-
70-
Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });

src/long.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,14 @@ export interface LongExtended {
100100
* Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
101101
*/
102102
export class Long {
103-
_bsontype!: 'Long';
103+
get _bsontype(): 'Long' {
104+
return 'Long';
105+
}
104106

105107
/** An indicator used to reliably determine if an object is a Long or not. */
106-
__isLong__!: true;
108+
get __isLong__(): boolean {
109+
return true;
110+
}
107111

108112
/**
109113
* The high 32 bits as a signed value.
@@ -134,8 +138,6 @@ export class Long {
134138
* @param unsigned - Whether unsigned or not, defaults to signed
135139
*/
136140
constructor(low: number | bigint | string = 0, high?: number | boolean, unsigned?: boolean) {
137-
if (!(this instanceof Long)) return new Long(low, high, unsigned);
138-
139141
if (typeof low === 'bigint') {
140142
Object.assign(this, Long.fromBigInt(low, !!high));
141143
} else if (typeof low === 'string') {
@@ -145,13 +147,6 @@ export class Long {
145147
this.high = (high as number) | 0;
146148
this.unsigned = !!unsigned;
147149
}
148-
149-
Object.defineProperty(this, '__isLong__', {
150-
value: true,
151-
configurable: false,
152-
writable: false,
153-
enumerable: false
154-
});
155150
}
156151

157152
static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1035,6 +1030,3 @@ export class Long {
10351030
return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
10361031
}
10371032
}
1038-
1039-
Object.defineProperty(Long.prototype, '__isLong__', { value: true });
1040-
Object.defineProperty(Long.prototype, '_bsontype', { value: 'Long' });

0 commit comments

Comments
 (0)