UNPKG

901 BJavaScriptView Raw
1class Scope {
2 /**
3 * @param {Array.<String>} [data]
4 */
5 constructor(data = []) {
6 this._scopes = data;
7 }
8
9 /**
10 * Push scope for future execution
11 * @param {string} scope
12 * @param {string?} parentScope
13 */
14 push(scope, parentScope = '') {
15 this._scopes.push({ scope, parentScope });
16 }
17
18 /**
19 * Pop scope
20 * @returns {Object}
21 */
22 pop() {
23 return this._scopes.pop();
24 }
25
26 /**
27 * Get current parsing selector
28 * @returns {string}
29 */
30 getSelector() {
31 const scopes = this._scopes;
32 const selector = [];
33 for (let i = scopes.length - 1; i >= 0; i--) {
34 const scope = scopes[i];
35 selector.unshift(scope.scope);
36
37 if (scope.parentScope) {
38 selector.unshift(scope.parentScope);
39 break;
40 }
41 }
42
43 return selector.join(' ');
44 }
45
46 isEmpty() {
47 return this._scopes.length === 0;
48 }
49}
50
51module.exports = Scope;