improve array to set deprecation for access to the first item

This commit is contained in:
Tobias Koppers 2020-09-22 16:46:01 +02:00
parent 9c0b320fd4
commit 746d377614
1 changed files with 14 additions and 10 deletions

View File

@ -134,20 +134,24 @@ exports.arrayToSetDeprecation = (set, name) => {
};
return fn;
};
let indexerDefined = 0;
const defineIndexGetter = index => {
Object.defineProperty(set, index, {
get: createIndexGetter(index),
set(value) {
throw new Error(
`${name} was changed from Array to Set (indexing Array with write is not possible)`
);
}
});
};
defineIndexGetter(0);
let indexerDefined = 1;
Object.defineProperty(set, "length", {
get() {
dLength();
const length = this.size;
for (indexerDefined; indexerDefined < length; indexerDefined++) {
Object.defineProperty(set, indexerDefined, {
get: createIndexGetter(indexerDefined),
set(value) {
throw new Error(
`${name} was changed from Array to Set (indexing Array with write is not possible)`
);
}
});
for (indexerDefined; indexerDefined < length + 1; indexerDefined++) {
defineIndexGetter(indexerDefined);
}
return length;
},