You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
24 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _supportsColor = _interopRequireDefault(require("supports-color"));
  5. var _chalk = _interopRequireDefault(require("chalk"));
  6. var _terminalHighlight = _interopRequireDefault(require("./terminal-highlight"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  9. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10. function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
  11. function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  12. function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
  13. function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
  14. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  15. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  16. /**
  17. * The CSS parser throws this error for broken CSS.
  18. *
  19. * Custom parsers can throw this error for broken custom syntax using
  20. * the {@link Node#error} method.
  21. *
  22. * PostCSS will use the input source map to detect the original error location.
  23. * If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS,
  24. * PostCSS will show the original position in the Sass file.
  25. *
  26. * If you need the position in the PostCSS input
  27. * (e.g., to debug the previous compiler), use `error.input.file`.
  28. *
  29. * @example
  30. * // Catching and checking syntax error
  31. * try {
  32. * postcss.parse('a{')
  33. * } catch (error) {
  34. * if (error.name === 'CssSyntaxError') {
  35. * error //=> CssSyntaxError
  36. * }
  37. * }
  38. *
  39. * @example
  40. * // Raising error from plugin
  41. * throw node.error('Unknown variable', { plugin: 'postcss-vars' })
  42. */
  43. var CssSyntaxError =
  44. /*#__PURE__*/
  45. function (_Error) {
  46. _inheritsLoose(CssSyntaxError, _Error);
  47. /**
  48. * @param {string} message Error message.
  49. * @param {number} [line] Source line of the error.
  50. * @param {number} [column] Source column of the error.
  51. * @param {string} [source] Source code of the broken file.
  52. * @param {string} [file] Absolute path to the broken file.
  53. * @param {string} [plugin] PostCSS plugin name, if error came from plugin.
  54. */
  55. function CssSyntaxError(message, line, column, source, file, plugin) {
  56. var _this;
  57. _this = _Error.call(this, message) || this;
  58. /**
  59. * Always equal to `'CssSyntaxError'`. You should always check error type
  60. * by `error.name === 'CssSyntaxError'`
  61. * instead of `error instanceof CssSyntaxError`,
  62. * because npm could have several PostCSS versions.
  63. *
  64. * @type {string}
  65. *
  66. * @example
  67. * if (error.name === 'CssSyntaxError') {
  68. * error //=> CssSyntaxError
  69. * }
  70. */
  71. _this.name = 'CssSyntaxError';
  72. /**
  73. * Error message.
  74. *
  75. * @type {string}
  76. *
  77. * @example
  78. * error.message //=> 'Unclosed block'
  79. */
  80. _this.reason = message;
  81. if (file) {
  82. /**
  83. * Absolute path to the broken file.
  84. *
  85. * @type {string}
  86. *
  87. * @example
  88. * error.file //=> 'a.sass'
  89. * error.input.file //=> 'a.css'
  90. */
  91. _this.file = file;
  92. }
  93. if (source) {
  94. /**
  95. * Source code of the broken file.
  96. *
  97. * @type {string}
  98. *
  99. * @example
  100. * error.source //=> 'a { b {} }'
  101. * error.input.column //=> 'a b { }'
  102. */
  103. _this.source = source;
  104. }
  105. if (plugin) {
  106. /**
  107. * Plugin name, if error came from plugin.
  108. *
  109. * @type {string}
  110. *
  111. * @example
  112. * error.plugin //=> 'postcss-vars'
  113. */
  114. _this.plugin = plugin;
  115. }
  116. if (typeof line !== 'undefined' && typeof column !== 'undefined') {
  117. /**
  118. * Source line of the error.
  119. *
  120. * @type {number}
  121. *
  122. * @example
  123. * error.line //=> 2
  124. * error.input.line //=> 4
  125. */
  126. _this.line = line;
  127. /**
  128. * Source column of the error.
  129. *
  130. * @type {number}
  131. *
  132. * @example
  133. * error.column //=> 1
  134. * error.input.column //=> 4
  135. */
  136. _this.column = column;
  137. }
  138. _this.setMessage();
  139. if (Error.captureStackTrace) {
  140. Error.captureStackTrace(_assertThisInitialized(_this), CssSyntaxError);
  141. }
  142. return _this;
  143. }
  144. var _proto = CssSyntaxError.prototype;
  145. _proto.setMessage = function setMessage() {
  146. /**
  147. * Full error text in the GNU error format
  148. * with plugin, file, line and column.
  149. *
  150. * @type {string}
  151. *
  152. * @example
  153. * error.message //=> 'a.css:1:1: Unclosed block'
  154. */
  155. this.message = this.plugin ? this.plugin + ': ' : '';
  156. this.message += this.file ? this.file : '<css input>';
  157. if (typeof this.line !== 'undefined') {
  158. this.message += ':' + this.line + ':' + this.column;
  159. }
  160. this.message += ': ' + this.reason;
  161. }
  162. /**
  163. * Returns a few lines of CSS source that caused the error.
  164. *
  165. * If the CSS has an input source map without `sourceContent`,
  166. * this method will return an empty string.
  167. *
  168. * @param {boolean} [color] Whether arrow will be colored red by terminal
  169. * color codes. By default, PostCSS will detect
  170. * color support by `process.stdout.isTTY`
  171. * and `process.env.NODE_DISABLE_COLORS`.
  172. *
  173. * @example
  174. * error.showSourceCode() //=> " 4 | }
  175. * // 5 | a {
  176. * // > 6 | bad
  177. * // | ^
  178. * // 7 | }
  179. * // 8 | b {"
  180. *
  181. * @return {string} Few lines of CSS source that caused the error.
  182. */
  183. ;
  184. _proto.showSourceCode = function showSourceCode(color) {
  185. var _this2 = this;
  186. if (!this.source) return '';
  187. var css = this.source;
  188. if (_terminalHighlight.default) {
  189. if (typeof color === 'undefined') color = _supportsColor.default.stdout;
  190. if (color) css = (0, _terminalHighlight.default)(css);
  191. }
  192. var lines = css.split(/\r?\n/);
  193. var start = Math.max(this.line - 3, 0);
  194. var end = Math.min(this.line + 2, lines.length);
  195. var maxWidth = String(end).length;
  196. function mark(text) {
  197. if (color && _chalk.default.red) {
  198. return _chalk.default.red.bold(text);
  199. }
  200. return text;
  201. }
  202. function aside(text) {
  203. if (color && _chalk.default.gray) {
  204. return _chalk.default.gray(text);
  205. }
  206. return text;
  207. }
  208. return lines.slice(start, end).map(function (line, index) {
  209. var number = start + 1 + index;
  210. var gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | ';
  211. if (number === _this2.line) {
  212. var spacing = aside(gutter.replace(/\d/g, ' ')) + line.slice(0, _this2.column - 1).replace(/[^\t]/g, ' ');
  213. return mark('>') + aside(gutter) + line + '\n ' + spacing + mark('^');
  214. }
  215. return ' ' + aside(gutter) + line;
  216. }).join('\n');
  217. }
  218. /**
  219. * Returns error position, message and source code of the broken part.
  220. *
  221. * @example
  222. * error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
  223. * // > 1 | a {
  224. * // | ^"
  225. *
  226. * @return {string} Error position, message and source code.
  227. */
  228. ;
  229. _proto.toString = function toString() {
  230. var code = this.showSourceCode();
  231. if (code) {
  232. code = '\n\n' + code + '\n';
  233. }
  234. return this.name + ': ' + this.message + code;
  235. }
  236. /**
  237. * @memberof CssSyntaxError#
  238. * @member {Input} input Input object with PostCSS internal information
  239. * about input file. If input has source map
  240. * from previous tool, PostCSS will use origin
  241. * (for example, Sass) source. You can use this
  242. * object to get PostCSS input source.
  243. *
  244. * @example
  245. * error.input.file //=> 'a.css'
  246. * error.file //=> 'a.sass'
  247. */
  248. ;
  249. return CssSyntaxError;
  250. }(_wrapNativeSuper(Error));
  251. var _default = CssSyntaxError;
  252. exports.default = _default;
  253. module.exports = exports.default;
  254. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1zeW50YXgtZXJyb3IuZXM2Il0sIm5hbWVzIjpbIkNzc1N5bnRheEVycm9yIiwibWVzc2FnZSIsImxpbmUiLCJjb2x1bW4iLCJzb3VyY2UiLCJmaWxlIiwicGx1Z2luIiwibmFtZSIsInJlYXNvbiIsInNldE1lc3NhZ2UiLCJFcnJvciIsImNhcHR1cmVTdGFja1RyYWNlIiwic2hvd1NvdXJjZUNvZGUiLCJjb2xvciIsImNzcyIsInRlcm1pbmFsSGlnaGxpZ2h0Iiwic3VwcG9ydHNDb2xvciIsInN0ZG91dCIsImxpbmVzIiwic3BsaXQiLCJzdGFydCIsIk1hdGgiLCJtYXgiLCJlbmQiLCJtaW4iLCJsZW5ndGgiLCJtYXhXaWR0aCIsIlN0cmluZyIsIm1hcmsiLCJ0ZXh0IiwiY2hhbGsiLCJyZWQiLCJib2xkIiwiYXNpZGUiLCJncmF5Iiwic2xpY2UiLCJtYXAiLCJpbmRleCIsIm51bWJlciIsImd1dHRlciIsInNwYWNpbmciLCJyZXBsYWNlIiwiam9pbiIsInRvU3RyaW5nIiwiY29kZSJdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQTs7QUFDQTs7QUFFQTs7Ozs7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