Skip to content

Commit

Permalink
feat(reflect): handle missing prototypes
Browse files Browse the repository at this point in the history
Typescript 4.2 fixed a return type for objects without a prototype.
This change throws an InvalidValueError for these, rather than the
TypeError that was previously thrown.
  • Loading branch information
ssube committed Mar 27, 2021
1 parent 3f02223 commit 99f18ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
.awcache/
.licenses/
.nyc_output/
.vscode/
node_modules/
out/
temp/
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -29,7 +29,7 @@
"@types/lodash": "4.14.168",
"@types/mocha": "8.2.2",
"@types/mock-fs": "4.13.0",
"@types/node": "14.14.25",
"@types/node": "14.14.37",
"@types/sinon-chai": "3.2.5",
"@types/source-map-support": "0.5.3",
"@typescript-eslint/eslint-plugin": "4.19.0",
Expand Down Expand Up @@ -73,7 +73,7 @@
"tslint-etc": "1.13.9",
"tslint-microsoft-contrib": "6.2.0",
"tslint-sonarts": "1.9.0",
"typescript": "4.1.3"
"typescript": "4.2.3"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript"
Expand Down
8 changes: 7 additions & 1 deletion src/Reflect.ts
@@ -1,3 +1,4 @@
import { InvalidArgumentError } from './error/InvalidArgumentError';
import { doesExist, isNil } from './Maybe';

/* eslint-disable-next-line @typescript-eslint/ban-types */
Expand Down Expand Up @@ -50,5 +51,10 @@ export function getMethods<TValue extends Reflectable>(value: TValue): Set<Metho
* @public
*/
export function constructorName(val: Reflectable) {
return getConstructor(Reflect.getPrototypeOf(val)).name;
const proto = Reflect.getPrototypeOf(val);
if (isNil(proto)) {
throw new InvalidArgumentError('value has no prototype');
} else {
return getConstructor(proto).name;
}
}
9 changes: 9 additions & 0 deletions test/utils/TestReflect.ts
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { InvalidArgumentError } from '../../src';
import { getMethods, getConstructor, constructorName } from '../../src/Reflect';

class Test {
Expand Down Expand Up @@ -29,5 +30,13 @@ describe('reflect utils', () => {
const instance = new Test();
expect(constructorName(instance)).to.equal(Test.name);
});

it('should throw when value has no prototype', () => {
/* eslint-disable-next-line no-null/no-null */
const protoless = Object.create(null, {});
expect(() => constructorName(protoless)).to.throw(InvalidArgumentError);
});

xit('should handle nil values');
});
});
21 changes: 13 additions & 8 deletions yarn.lock
Expand Up @@ -423,10 +423,10 @@
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-10.17.13.tgz#ccebcdb990bd6139cd16e84c39dc2fb1023ca90c"
integrity sha512-pMCcqU2zT4TjqYFrWtYHKal7Sl30Ims6ulZ4UFXxI4xbtQqK/qqKwkDoBFCfooRqqmRu9vY3xaJRwxSh673aYg==

"@types/node@14.14.25":
version "14.14.25"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-14.14.25.tgz#15967a7b577ff81383f9b888aa6705d43fbbae93"
integrity sha512-EPpXLOVqDvisVxtlbvzfyqSsFeQxltFbluZNRndIb8tr9KiBnYNLzrc1N3pyKUCww2RNrfHDViqDWWE1LCJQtQ==
"@types/node@14.14.37":
version "14.14.37"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
Expand Down Expand Up @@ -4641,16 +4641,21 @@ typedarray@^0.0.6:
resolved "https://artifacts.apextoaster.com/repository/group-npm/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@4.1.3, typescript@~4.1.3:
version "4.1.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
typescript@4.2.3:
version "4.2.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==

typescript@^3.0.0:
version "3.6.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-3.6.3.tgz#fea942fabb20f7e1ca7164ff626f1a9f3f70b4da"
integrity sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==

typescript@~4.1.3:
version "4.1.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==

uglify-js@^3.1.4:
version "3.6.0"
resolved "https://artifacts.apextoaster.com/repository/group-npm/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"
Expand Down

0 comments on commit 99f18ae

Please sign in to comment.