forked from pugjs/babel-plugin-transform-react-pug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.js
184 lines (156 loc) · 4.28 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// @flow
import {readFileSync, existsSync} from 'fs';
import error from 'pug-error';
import type {Key} from './block-key';
import {getCurrentLocation} from './babel-types';
import {BaseKey, StaticBlock, DynamicBlock} from './block-key';
export type VariableKind = 'var' | 'let' | 'const';
type Variable = {
kind: VariableKind,
id: Identifier,
};
class Context {
key: Key;
file: Object;
path: Object;
_variables: Map<string, Variable> = new Map();
variablesToDeclare: Array<Identifier> = [];
_nextBlockID: number = 0;
_parent: ?Context;
_interpolations: ?Map<string, Expression>;
constructor(
definesScope: boolean,
key: Key,
parent: ?Context,
file: Object,
path: Object,
interpolations: ?Map<string, Expression>,
) {
if (!definesScope && parent) {
this.variablesToDeclare = parent.variablesToDeclare;
}
this._parent = parent;
this.key = key;
this.file = file;
this.path = path;
this._interpolations = interpolations;
}
error(code: string, message: string): Error {
const options: Object = {
filename: this.file.opts.filename,
line: getCurrentLocation().start.line - 1,
src: null,
};
if (existsSync(options.filename)) {
options.src = readFileSync(this.file.opts.filename, 'utf8');
}
return error(code, message, options);
}
noKey<T>(fn: (context: Context) => T): T {
const childContext = new Context(
false,
new BaseKey(),
this,
this.file,
this.path,
);
const result = fn(childContext);
childContext.end();
return result;
}
staticBlock<T>(fn: (context: Context) => T): T {
const childContext = new Context(
false,
new StaticBlock(this.key, this._nextBlockID++),
this,
this.file,
this.path,
);
const result = fn(childContext);
childContext.end();
return result;
}
dynamicBlock<T>(
fn: (context: Context) => T,
): {result: T, variables: Array<Identifier>} {
const childContext = new Context(
true,
new DynamicBlock(this.key, 'src', 0),
this,
this.file,
this.path,
);
const result = fn(childContext);
childContext.end();
return {result, variables: childContext.variablesToDeclare};
}
end(): void {
this.key.end();
}
getVariable(name: string): ?Variable {
const variable = this._variables.get(name);
if (variable) {
return variable;
}
if (this._parent) {
return this._parent.getVariable(name);
}
// TODO: maybe actually verify existance/non-const in parent scope?
return null;
}
declareVariable(kind: 'var' | 'let' | 'const', name: string): Variable {
if (typeof name !== 'string') {
throw new Error('variables may only be declared with strings');
}
const oldVariable = this._variables.get(name);
if (oldVariable) {
if (oldVariable.kind !== 'var' || kind !== 'var') {
const err = this.error(
'DUPLICATE_VARIABLE',
`Duplicate variable ${name}.`,
);
throw err;
}
return oldVariable;
}
const variable = {
kind,
id: this.generateUidIdentifier(name),
};
this.variablesToDeclare.push(variable.id);
this._variables.set(name, variable);
return variable;
}
generateUidIdentifier(name: string): Identifier {
return this.path.scope.generateUidIdentifier(name);
}
getBaseLine(): number {
return this.path.node.loc.start.line;
}
/**
* Check whether interpolations exist for the context, if not,
* recursively check the parent context for the interpolation.
* @param { String } reference - The interpolation reference
* @returns { ?Expression } The interpolation or nothing.
*/
getInterpolationByRef(reference: string): ?Expression {
let interpolation = null;
if (
this._interpolations &&
(interpolation = this._interpolations.get(reference))
) {
return interpolation;
} else if (this._parent) {
return this._parent.getInterpolationByRef(reference);
}
return this.getInterpolationByRef(reference);
}
static create(
file: Object,
path: Object,
interpolations?: Map<string, Expression>,
) {
return new Context(true, new BaseKey(), null, file, path, interpolations);
}
}
export default Context;