Skip to content

Commit 2580547

Browse files
committed
move StackedSetMap into separate file
1 parent 25b73bb commit 2580547

File tree

2 files changed

+81
-72
lines changed

2 files changed

+81
-72
lines changed

lib/Parser.js

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
88

9-
const util = require("util");
109
const acorn = require("acorn-dynamic-import").default;
1110
const Tapable = require("tapable");
1211
const json5 = require("json5");
1312
const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
13+
const StackedSetMap = require("./util/StackedSetMap");
1414

1515
function joinRanges(startRange, endRange) {
1616
if(!endRange) return startRange;
@@ -38,77 +38,6 @@ const POSSIBLE_AST_OPTIONS = [{
3838
}
3939
}];
4040

41-
class StackedSetMap {
42-
constructor(defaultValue, parentStack) {
43-
this.defaultValue = defaultValue;
44-
this.stack = parentStack === undefined ? [] : parentStack.slice();
45-
this.map = new Map();
46-
this.stack.push(this.map);
47-
}
48-
49-
add(item) {
50-
this.map.set(item, true);
51-
}
52-
53-
set(item, value) {
54-
this.map.set(item, value);
55-
}
56-
57-
delete(item) {
58-
this.map.set(item, false);
59-
}
60-
61-
has(item) {
62-
return this.get(item, false);
63-
}
64-
65-
get(item) {
66-
const topValue = this.map.get(item);
67-
if(typeof topValue !== "undefined")
68-
return topValue;
69-
for(var i = this.stack.length - 2; i >= 0; i--) {
70-
const value = this.stack[i].get(item);
71-
if(typeof value !== "undefined") {
72-
this.map.set(item, value);
73-
return value;
74-
}
75-
}
76-
this.map.set(item, this.defaultValue);
77-
return this.defaultValue;
78-
}
79-
80-
_compress() {
81-
this.map = new Map();
82-
for(const data of this.stack) {
83-
for(const pair of data) {
84-
this.map.set(pair[0], pair[1]);
85-
}
86-
}
87-
this.stack = [this.map];
88-
}
89-
90-
asSet() {
91-
this._compress();
92-
return new Set(Array.from(this.map.entries()).filter(pair => pair[1]).map(pair => pair[0]));
93-
}
94-
95-
createChild() {
96-
return new StackedSetMap(this.defaultValue, this.stack);
97-
}
98-
99-
get length() {
100-
throw new Error("Parser.definitions is no longer an Array");
101-
}
102-
103-
set length(value) {
104-
throw new Error("Parser.definitions is no longer an Array");
105-
}
106-
}
107-
108-
StackedSetMap.prototype.push = util.deprecate(function(item) {
109-
this.add(item);
110-
}, "Parser.definitions is no longer an Array: Use add instead.");
111-
11241
class TrackingSet {
11342
constructor(set) {
11443
this.set = set;

lib/util/StackedSetMap.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const util = require("util");
8+
9+
class StackedSetMap {
10+
constructor(defaultValue, parentStack) {
11+
this.defaultValue = defaultValue;
12+
this.stack = parentStack === undefined ? [] : parentStack.slice();
13+
this.map = new Map();
14+
this.stack.push(this.map);
15+
}
16+
17+
add(item) {
18+
this.map.set(item, true);
19+
}
20+
21+
set(item, value) {
22+
this.map.set(item, value);
23+
}
24+
25+
delete(item) {
26+
this.map.set(item, false);
27+
}
28+
29+
has(item) {
30+
return this.get(item, false);
31+
}
32+
33+
get(item) {
34+
const topValue = this.map.get(item);
35+
if(typeof topValue !== "undefined")
36+
return topValue;
37+
for(var i = this.stack.length - 2; i >= 0; i--) {
38+
const value = this.stack[i].get(item);
39+
if(typeof value !== "undefined") {
40+
this.map.set(item, value);
41+
return value;
42+
}
43+
}
44+
this.map.set(item, this.defaultValue);
45+
return this.defaultValue;
46+
}
47+
48+
_compress() {
49+
this.map = new Map();
50+
for(const data of this.stack) {
51+
for(const pair of data) {
52+
this.map.set(pair[0], pair[1]);
53+
}
54+
}
55+
this.stack = [this.map];
56+
}
57+
58+
asSet() {
59+
this._compress();
60+
return new Set(Array.from(this.map.entries()).filter(pair => pair[1]).map(pair => pair[0]));
61+
}
62+
63+
createChild() {
64+
return new StackedSetMap(this.defaultValue, this.stack);
65+
}
66+
67+
get length() {
68+
throw new Error("This is no longer an Array");
69+
}
70+
71+
set length(value) {
72+
throw new Error("This is no longer an Array");
73+
}
74+
}
75+
76+
StackedSetMap.prototype.push = util.deprecate(function(item) {
77+
this.add(item);
78+
}, "This is no longer an Array: Use add instead.");
79+
80+
module.exports = StackedSetMap;

0 commit comments

Comments
 (0)