Skip to content

Commit

Permalink
scripting: Fix equal between undefined and empty string.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Jan 9, 2020
1 parent 9723f6a commit 8c9cf72
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/scripting/vbs-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ describe('The scripting VBS Helper', () => {
expect(vbsHelper.equals(undefined, new VbsUndefined())).to.equal(true);
expect(vbsHelper.equals(new VbsUndefined(), undefined)).to.equal(true);
expect(vbsHelper.equals(new VbsUndefined(), new VbsUndefined())).to.equal(true);
expect(vbsHelper.equals(new VbsUndefined(), '')).to.equal(true);
expect(vbsHelper.equals('', new VbsUndefined())).to.equal(true);
});

it('should compare not equal using "equals"', () => {
Expand Down
19 changes: 16 additions & 3 deletions lib/scripting/vbs-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,31 @@ export class VBSHelper {
return true;
}

const undef1 = typeof value1 === 'object' && value1.__isUndefined;
const undef2 = typeof value2 === 'object' && value2.__isUndefined;

// VbsUndefined == undefined
if (typeof value1 === 'object' && value1.__isUndefined && typeof value2 === 'undefined') {
if (undef1 && typeof value2 === 'undefined') {
return true;
}

// undefined == VbsUndefined
if (typeof value2 === 'object' && value2.__isUndefined && typeof value1 === 'undefined') {
if (typeof value1 === 'undefined' && undef2) {
return true;
}

// VbsUndefined == VbsUndefined
if (typeof value1 === 'object' && value1.__isUndefined && typeof value2 === 'object' && value2.__isUndefined) {
if (undef1 && undef2) {
return true;
}

// '' == VbsUndefined
if (undef1 && value2 === '') {
return true;
}

// VbsUndefined == ''
if (value1 === '' && undef2) {
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/scripting/vbs-undefined.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ describe('The VBScript undefined handler', () => {
expect(() => undef == 1).not.to.throw();
});

it('should return "undefined" as string', () => {
const undef = new VbsUndefined() as unknown;
// tslint:disable-next-line:triple-equals
expect('' + undef).to.equal('undefined');
});

});
3 changes: 3 additions & 0 deletions lib/scripting/vbs-undefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class VbsUndefined implements ProxyHandler<any> {
}

public get(target: any, p: string | number | symbol, receiver: any): any {
if (p === 'toString') {
return () => undefined;
}
if (typeof p === 'symbol' || ['valueOf', 'toString', 'inspect', '__errGet', '__errSet'].includes(p as string)) {
return Reflect.get(target, p);
}
Expand Down

0 comments on commit 8c9cf72

Please sign in to comment.