1
0
Fork 0

feat(reflect): handle missing prototypes

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.
This commit is contained in:
ssube 2021-03-27 18:22:36 -05:00
parent 3f02223d64
commit 99f18ae5d1
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
5 changed files with 32 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
.awcache/ .awcache/
.licenses/ .licenses/
.nyc_output/ .nyc_output/
.vscode/
node_modules/ node_modules/
out/ out/
temp/ temp/

View File

@ -29,7 +29,7 @@
"@types/lodash": "4.14.168", "@types/lodash": "4.14.168",
"@types/mocha": "8.2.2", "@types/mocha": "8.2.2",
"@types/mock-fs": "4.13.0", "@types/mock-fs": "4.13.0",
"@types/node": "14.14.25", "@types/node": "14.14.37",
"@types/sinon-chai": "3.2.5", "@types/sinon-chai": "3.2.5",
"@types/source-map-support": "0.5.3", "@types/source-map-support": "0.5.3",
"@typescript-eslint/eslint-plugin": "4.19.0", "@typescript-eslint/eslint-plugin": "4.19.0",
@ -73,7 +73,7 @@
"tslint-etc": "1.13.9", "tslint-etc": "1.13.9",
"tslint-microsoft-contrib": "6.2.0", "tslint-microsoft-contrib": "6.2.0",
"tslint-sonarts": "1.9.0", "tslint-sonarts": "1.9.0",
"typescript": "4.1.3" "typescript": "4.2.3"
}, },
"nyc": { "nyc": {
"extends": "@istanbuljs/nyc-config-typescript" "extends": "@istanbuljs/nyc-config-typescript"

View File

@ -1,3 +1,4 @@
import { InvalidArgumentError } from './error/InvalidArgumentError';
import { doesExist, isNil } from './Maybe'; import { doesExist, isNil } from './Maybe';
/* eslint-disable-next-line @typescript-eslint/ban-types */ /* eslint-disable-next-line @typescript-eslint/ban-types */
@ -50,5 +51,10 @@ export function getMethods<TValue extends Reflectable>(value: TValue): Set<Metho
* @public * @public
*/ */
export function constructorName(val: Reflectable) { 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;
}
} }

View File

@ -1,4 +1,5 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { InvalidArgumentError } from '../../src';
import { getMethods, getConstructor, constructorName } from '../../src/Reflect'; import { getMethods, getConstructor, constructorName } from '../../src/Reflect';
class Test { class Test {
@ -29,5 +30,13 @@ describe('reflect utils', () => {
const instance = new Test(); const instance = new Test();
expect(constructorName(instance)).to.equal(Test.name); 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');
}); });
}); });

View File

@ -423,10 +423,10 @@
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-10.17.13.tgz#ccebcdb990bd6139cd16e84c39dc2fb1023ca90c" resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-10.17.13.tgz#ccebcdb990bd6139cd16e84c39dc2fb1023ca90c"
integrity sha512-pMCcqU2zT4TjqYFrWtYHKal7Sl30Ims6ulZ4UFXxI4xbtQqK/qqKwkDoBFCfooRqqmRu9vY3xaJRwxSh673aYg== integrity sha512-pMCcqU2zT4TjqYFrWtYHKal7Sl30Ims6ulZ4UFXxI4xbtQqK/qqKwkDoBFCfooRqqmRu9vY3xaJRwxSh673aYg==
"@types/node@14.14.25": "@types/node@14.14.37":
version "14.14.25" version "14.14.37"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-14.14.25.tgz#15967a7b577ff81383f9b888aa6705d43fbbae93" resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
integrity sha512-EPpXLOVqDvisVxtlbvzfyqSsFeQxltFbluZNRndIb8tr9KiBnYNLzrc1N3pyKUCww2RNrfHDViqDWWE1LCJQtQ== integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==
"@types/normalize-package-data@^2.4.0": "@types/normalize-package-data@^2.4.0":
version "2.4.0" version "2.4.0"
@ -4641,16 +4641,21 @@ typedarray@^0.0.6:
resolved "https://artifacts.apextoaster.com/repository/group-npm/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" resolved "https://artifacts.apextoaster.com/repository/group-npm/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
typescript@4.1.3, typescript@~4.1.3: typescript@4.2.3:
version "4.1.3" version "4.2.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg== integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==
typescript@^3.0.0: typescript@^3.0.0:
version "3.6.3" version "3.6.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-3.6.3.tgz#fea942fabb20f7e1ca7164ff626f1a9f3f70b4da" resolved "https://artifacts.apextoaster.com/repository/group-npm/typescript/-/typescript-3.6.3.tgz#fea942fabb20f7e1ca7164ff626f1a9f3f70b4da"
integrity sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw== 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: uglify-js@^3.1.4:
version "3.6.0" version "3.6.0"
resolved "https://artifacts.apextoaster.com/repository/group-npm/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5" resolved "https://artifacts.apextoaster.com/repository/group-npm/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"