Skip to content

Commit 97d2ea8

Browse files
committed
Minor improvements of code and comments.
1 parent 9140cbc commit 97d2ea8

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

src/compiler/core.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ namespace ts {
121121
export function shimMap(): new <T>() => Map<T> {
122122

123123
interface MapEntry<T> {
124-
key?: string;
124+
readonly key?: string;
125125
value?: T;
126126

127-
// Linked list references
127+
// Linked list references for iterators.
128128
nextEntry?: MapEntry<T>;
129129
previousEntry?: MapEntry<T>;
130130

131131
/**
132-
* Specifies if the iterator should skip the next entry.
132+
* Specifies if iterators should skip the next entry.
133133
* This will be set when an entry is deleted.
134134
* See https://github.com/Microsoft/TypeScript/pull/27292 for more information.
135135
*/
@@ -146,7 +146,7 @@ namespace ts {
146146
}
147147

148148
public next(): { value: U, done: false } | { value: never, done: true } {
149-
// Navigate to the next element.
149+
// Navigate to the next entry.
150150
while (this.currentEntry) {
151151
const skipNext = !!this.currentEntry.skipNext;
152152
this.currentEntry = this.currentEntry.nextEntry;
@@ -172,16 +172,22 @@ namespace ts {
172172
// Linked list references for iterators.
173173
// See https://github.com/Microsoft/TypeScript/pull/27292
174174
// for more information.
175-
private readonly linkedListStart: MapEntry<T>;
176-
private linkedListEnd: MapEntry<T>;
175+
176+
/**
177+
* The first entry in the linked list.
178+
* Note that this is only a stub that serves as starting point
179+
* for iterators and doesn't contain a key and a value.
180+
*/
181+
private readonly firstEntry: MapEntry<T>;
182+
private lastEntry: MapEntry<T>;
177183

178184
constructor() {
179-
// Create a (stub) start element that will not
180-
// contain a key and value.
181-
this.linkedListStart = {};
182-
// When the map is empty, the end element is the same as the
183-
// start element.
184-
this.linkedListEnd = this.linkedListStart;
185+
// Create a first (stub) map entry that will not contain a key
186+
// and value but serves as starting point for iterators.
187+
this.firstEntry = {};
188+
// When the map is empty, the last entry is the same as the
189+
// first one.
190+
this.lastEntry = this.firstEntry;
185191
}
186192

187193
get(key: string): T | undefined {
@@ -193,17 +199,19 @@ namespace ts {
193199
if (!this.has(key)) {
194200
this.size++;
195201

196-
// Append the new element at the end of the linked list.
202+
// Create a new entry that will be appended at the
203+
// end of the linked list.
197204
const newEntry: MapEntry<T> = {
198205
key,
199206
value
200207
};
201208
this.data[key] = newEntry;
202209

203-
const previousEndElement = this.linkedListEnd;
204-
previousEndElement.nextEntry = newEntry;
205-
newEntry.previousEntry = previousEndElement;
206-
this.linkedListEnd = newEntry;
210+
// Adjust the references.
211+
const previousLastEntry = this.lastEntry;
212+
previousLastEntry.nextEntry = newEntry;
213+
newEntry.previousEntry = previousLastEntry;
214+
this.lastEntry = newEntry;
207215
}
208216
else {
209217
this.data[key].value = value;
@@ -231,13 +239,17 @@ namespace ts {
231239
}
232240

233241
// When the deleted entry was the last one, we need to
234-
// adust the endElement reference.
235-
if (this.linkedListEnd === entry) {
236-
this.linkedListEnd = previousEntry;
242+
// adust the lastEntry reference.
243+
if (this.lastEntry === entry) {
244+
this.lastEntry = previousEntry;
237245
}
238246

239-
// Adjust the forward reference of the deleted element
240-
// in case an iterator still references it.
247+
// Adjust the forward reference of the deleted entry
248+
// in case an iterator still references it. This allows us
249+
// to throw away the entry, but when an active iterator
250+
// (which points to the current entry) continues, it will
251+
// navigate to the entry that originally came before the
252+
// current one and skip it.
241253
entry.previousEntry = undefined;
242254
entry.nextEntry = previousEntry;
243255
entry.skipNext = true;
@@ -256,30 +268,30 @@ namespace ts {
256268
// in the middle of the list don't continue with deleted entries,
257269
// but can continue with new entries added after the clear()
258270
// operation.
259-
const startEntry = this.linkedListStart;
260-
let currentEntry = startEntry.nextEntry;
271+
const firstEntry = this.firstEntry;
272+
let currentEntry = firstEntry.nextEntry;
261273
while (currentEntry) {
262274
const nextEntry = currentEntry.nextEntry;
263275
currentEntry.previousEntry = undefined;
264-
currentEntry.nextEntry = startEntry;
276+
currentEntry.nextEntry = firstEntry;
265277
currentEntry.skipNext = true;
266278

267279
currentEntry = nextEntry;
268280
}
269-
this.linkedListStart.nextEntry = undefined;
270-
this.linkedListEnd = this.linkedListStart;
281+
firstEntry.nextEntry = undefined;
282+
this.lastEntry = firstEntry;
271283
}
272284

273285
keys(): Iterator<string> {
274-
return new MapIterator(this.linkedListStart, key => key);
286+
return new MapIterator(this.firstEntry, key => key);
275287
}
276288

277289
values(): Iterator<T> {
278-
return new MapIterator(this.linkedListStart, (_key, value) => value);
290+
return new MapIterator(this.firstEntry, (_key, value) => value);
279291
}
280292

281293
entries(): Iterator<[string, T]> {
282-
return new MapIterator(this.linkedListStart, (key, value) => [key, value] as [string, T]);
294+
return new MapIterator(this.firstEntry, (key, value) => [key, value] as [string, T]);
283295
}
284296

285297
forEach(action: (value: T, key: string) => void): void {

0 commit comments

Comments
 (0)