parsing-error.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var parserPackage = require('../package.json');
  2. /**
  3. * @param {Error} e
  4. * @param {String} css
  5. */
  6. function ParsingError(e, css) {
  7. this.line = e.line;
  8. this.syntax = e.syntax;
  9. this.css_ = css;
  10. }
  11. ParsingError.prototype = {
  12. /**
  13. * @type {Number}
  14. */
  15. line: null,
  16. /**
  17. * @type {String}
  18. */
  19. name: 'Parsing error',
  20. /**
  21. * @type {String}
  22. */
  23. syntax: null,
  24. /**
  25. * @type {String}
  26. */
  27. version: parserPackage.version,
  28. /**
  29. * @return {String}
  30. */
  31. toString: function() {
  32. return this.name + ': ' + this.message;
  33. },
  34. /**
  35. * @type {String}
  36. */
  37. get message() {
  38. return [
  39. 'Please check the validity of the block starting from line #' + this.line,
  40. '',
  41. this.codeFragment_,
  42. '',
  43. 'Gonzales PE version: ' + this.version,
  44. 'Syntax: ' + this.syntax
  45. ].join('\n');
  46. },
  47. /**
  48. * @type {String}
  49. */
  50. get codeFragment_() {
  51. var LINES_AROUND = 2;
  52. var result = [];
  53. var currentLineNumber = this.line;
  54. var start = currentLineNumber - 1 - LINES_AROUND;
  55. var end = currentLineNumber + LINES_AROUND;
  56. var lines = this.css_.split(/\r\n|\r|\n/);
  57. for (var i = start; i < end; i++) {
  58. var line = lines[i];
  59. if (!line) continue;
  60. var ln = i + 1;
  61. var mark = ln === currentLineNumber ? '*' : ' ';
  62. result.push(ln + mark + '| ' + line);
  63. }
  64. return result.join('\n');
  65. }
  66. };
  67. module.exports = ParsingError;