scope.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*!
  2. * Stylus - stack - Scope
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Initialize a new `Scope`.
  8. *
  9. * @api private
  10. */
  11. var Scope = module.exports = function Scope() {
  12. this.locals = {};
  13. };
  14. /**
  15. * Add `ident` node to the current scope.
  16. *
  17. * @param {Ident} ident
  18. * @api private
  19. */
  20. Scope.prototype.add = function(ident){
  21. this.locals[ident.name] = ident.val;
  22. };
  23. /**
  24. * Lookup the given local variable `name`.
  25. *
  26. * @param {String} name
  27. * @return {Node}
  28. * @api private
  29. */
  30. Scope.prototype.lookup = function(name){
  31. return hasOwnProperty(this.locals, name) ? this.locals[name] : undefined;
  32. };
  33. /**
  34. * Custom inspect.
  35. *
  36. * @return {String}
  37. * @api public
  38. */
  39. Scope.prototype.inspect = function(){
  40. var keys = Object.keys(this.locals).map(function(key){ return '@' + key; });
  41. return '[Scope'
  42. + (keys.length ? ' ' + keys.join(', ') : '')
  43. + ']';
  44. };
  45. /**
  46. * @param {Object} obj
  47. * @param {String} propName
  48. * @returns {Boolean}
  49. */
  50. function hasOwnProperty(obj, propName) {
  51. return Object.prototype.hasOwnProperty.call(obj, propName);
  52. }