Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5559): account for quotes when inspecting Code and BSONSymbol #612

Merged
merged 1 commit into from Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/code.ts
Expand Up @@ -62,7 +62,7 @@ export class Code extends BSONValue {

inspect(): string {
const codeJson = this.toJSON();
return `new Code("${String(codeJson.code)}"${
return `new Code(${JSON.stringify(String(codeJson.code))}${
codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/symbol.ts
Expand Up @@ -34,7 +34,7 @@ export class BSONSymbol extends BSONValue {
}

inspect(): string {
return `new BSONSymbol("${this.value}")`;
return `new BSONSymbol(${JSON.stringify(this.value)})`;
}

toJSON(): string {
Expand Down
8 changes: 8 additions & 0 deletions test/node/code.test.ts
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as BSON from '../register-bson';
import { inspect } from 'util';

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

it('prints re-evaluatable output for Code that contains quotes', () => {
const codeStringInput = new BSON.Code('function a(){ return "asdf"; }');
expect(inspect(codeStringInput)).to.equal(
String.raw`new Code("function a(){ return \"asdf\"; }")`
);
});

describe('new Code()', () => {
it('defines a code property that is a string', () => {
const codeStringInput = new BSON.Code('function a(){}');
Expand Down
6 changes: 6 additions & 0 deletions test/node/symbol.test.ts
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { BSONSymbol, BSON } from '../register-bson';
import { bufferFromHexArray } from './tools/utils';
import { inspect } from 'util';

describe('class BSONSymbol', () => {
it('get _bsontype returns BSONSymbol', () => {
Expand Down Expand Up @@ -41,4 +42,9 @@ describe('class BSONSymbol', () => {
const result = BSON.deserialize(bytes, { promoteValues: false });
expect(result).to.have.nested.property('sym._bsontype', 'BSONSymbol');
});

it('prints re-evaluatable output for BSONSymbol that contains quotes', () => {
const input = new BSONSymbol('asdf"ghjk');
expect(inspect(input)).to.equal(String.raw`new BSONSymbol("asdf\"ghjk")`);
});
});