Skip to content

Commit 0664840

Browse files
authored
fix(NODE-5559): account for quotes when inspecting Code and BSONSymbol (#612)
1 parent db2fc68 commit 0664840

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

src/code.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class Code extends BSONValue {
6262

6363
inspect(): string {
6464
const codeJson = this.toJSON();
65-
return `new Code("${String(codeJson.code)}"${
65+
return `new Code(${JSON.stringify(String(codeJson.code))}${
6666
codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''
6767
})`;
6868
}

src/symbol.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class BSONSymbol extends BSONValue {
3434
}
3535

3636
inspect(): string {
37-
return `new BSONSymbol("${this.value}")`;
37+
return `new BSONSymbol(${JSON.stringify(this.value)})`;
3838
}
3939

4040
toJSON(): string {

test/node/code.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import * as BSON from '../register-bson';
3+
import { inspect } from 'util';
34

45
describe('class Code', () => {
56
it('defines a nodejs inspect method', () => {
@@ -8,6 +9,13 @@ describe('class Code', () => {
89
.that.is.a('function');
910
});
1011

12+
it('prints re-evaluatable output for Code that contains quotes', () => {
13+
const codeStringInput = new BSON.Code('function a(){ return "asdf"; }');
14+
expect(inspect(codeStringInput)).to.equal(
15+
String.raw`new Code("function a(){ return \"asdf\"; }")`
16+
);
17+
});
18+
1119
describe('new Code()', () => {
1220
it('defines a code property that is a string', () => {
1321
const codeStringInput = new BSON.Code('function a(){}');

test/node/symbol.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai';
22
import { BSONSymbol, BSON } from '../register-bson';
33
import { bufferFromHexArray } from './tools/utils';
4+
import { inspect } from 'util';
45

56
describe('class BSONSymbol', () => {
67
it('get _bsontype returns BSONSymbol', () => {
@@ -41,4 +42,9 @@ describe('class BSONSymbol', () => {
4142
const result = BSON.deserialize(bytes, { promoteValues: false });
4243
expect(result).to.have.nested.property('sym._bsontype', 'BSONSymbol');
4344
});
45+
46+
it('prints re-evaluatable output for BSONSymbol that contains quotes', () => {
47+
const input = new BSONSymbol('asdf"ghjk');
48+
expect(inspect(input)).to.equal(String.raw`new BSONSymbol("asdf\"ghjk")`);
49+
});
4450
});

0 commit comments

Comments
 (0)