A stylesheet language written in TypeScript that compiles to CSS
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mixin.ts 1.6KB

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