|
@@ -1,7 +1,12 @@
|
1
|
1
|
import * as AST from '../ast';
|
2
|
|
-import Env from '../env';
|
|
2
|
+import Env, { EnvError } from '../env';
|
3
|
3
|
import Token from '../token';
|
4
|
4
|
|
|
5
|
+interface Expansion {
|
|
6
|
+ rules: (String | EnvError)[];
|
|
7
|
+ children: (String | EnvError)[];
|
|
8
|
+}
|
|
9
|
+
|
5
|
10
|
export class Mixin {
|
6
|
11
|
public name: Token;
|
7
|
12
|
public parameters: AST.Identifier[];
|
|
@@ -16,8 +21,45 @@ export class Mixin {
|
16
|
21
|
this.parameters = parameters;
|
17
|
22
|
this.children = children;
|
18
|
23
|
}
|
|
24
|
+
|
19
|
25
|
public compile(env: Env, _opts: AST.Opts) {
|
20
|
26
|
env.set(this.name.value, this);
|
21
|
27
|
return '';
|
22
|
28
|
}
|
|
29
|
+
|
|
30
|
+ public expand(
|
|
31
|
+ env: Env,
|
|
32
|
+ opts: AST.Opts,
|
|
33
|
+ parents: AST.Selector[],
|
|
34
|
+ args: AST.Node[]
|
|
35
|
+ ): Expansion | EnvError {
|
|
36
|
+ if (this.parameters.length !== args.length) {
|
|
37
|
+ return new EnvError(
|
|
38
|
+ this.name.line,
|
|
39
|
+ `Expected ${this.parameters.length} arguments but received ${
|
|
40
|
+ args.length
|
|
41
|
+ }`
|
|
42
|
+ );
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ const mixinEnv = new Env(env);
|
|
46
|
+ this.parameters.forEach((param, index) => {
|
|
47
|
+ mixinEnv.set(param.name.value, args[index]);
|
|
48
|
+ });
|
|
49
|
+
|
|
50
|
+ const rules = this.children
|
|
51
|
+ .filter((child: AST.Node): child is AST.Rule => child instanceof AST.Rule)
|
|
52
|
+ .map((child: AST.Rule) => child.compile(mixinEnv, opts));
|
|
53
|
+
|
|
54
|
+ const children = this.children
|
|
55
|
+ .filter(
|
|
56
|
+ (child: AST.Node): child is AST.RuleSet => child instanceof AST.RuleSet
|
|
57
|
+ )
|
|
58
|
+ .map((child: AST.RuleSet) => {
|
|
59
|
+ child.selectors.forEach((sel) => (sel.parents = parents));
|
|
60
|
+ return child.compile(mixinEnv, opts);
|
|
61
|
+ });
|
|
62
|
+
|
|
63
|
+ return { rules, children };
|
|
64
|
+ }
|
23
|
65
|
}
|