Dashboard sipadu mbip
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.

Node.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _constants = require("../constants");
  11. var _sourceUtils = require("./source-utils");
  12. var _Range = _interopRequireDefault(require("./Range"));
  13. /** Root class of all nodes */
  14. var Node =
  15. /*#__PURE__*/
  16. function () {
  17. (0, _createClass2.default)(Node, null, [{
  18. key: "addStringTerminator",
  19. value: function addStringTerminator(src, offset, str) {
  20. if (str[str.length - 1] === '\n') return str;
  21. var next = Node.endOfWhiteSpace(src, offset);
  22. return next >= src.length || src[next] === '\n' ? str + '\n' : str;
  23. } // ^(---|...)
  24. }, {
  25. key: "atDocumentBoundary",
  26. value: function atDocumentBoundary(src, offset, sep) {
  27. var ch0 = src[offset];
  28. if (!ch0) return true;
  29. var prev = src[offset - 1];
  30. if (prev && prev !== '\n') return false;
  31. if (sep) {
  32. if (ch0 !== sep) return false;
  33. } else {
  34. if (ch0 !== _constants.Char.DIRECTIVES_END && ch0 !== _constants.Char.DOCUMENT_END) return false;
  35. }
  36. var ch1 = src[offset + 1];
  37. var ch2 = src[offset + 2];
  38. if (ch1 !== ch0 || ch2 !== ch0) return false;
  39. var ch3 = src[offset + 3];
  40. return !ch3 || ch3 === '\n' || ch3 === '\t' || ch3 === ' ';
  41. }
  42. }, {
  43. key: "endOfIdentifier",
  44. value: function endOfIdentifier(src, offset) {
  45. var ch = src[offset];
  46. var isVerbatim = ch === '<';
  47. var notOk = isVerbatim ? ['\n', '\t', ' ', '>'] : ['\n', '\t', ' ', '[', ']', '{', '}', ','];
  48. while (ch && notOk.indexOf(ch) === -1) {
  49. ch = src[offset += 1];
  50. }
  51. if (isVerbatim && ch === '>') offset += 1;
  52. return offset;
  53. }
  54. }, {
  55. key: "endOfIndent",
  56. value: function endOfIndent(src, offset) {
  57. var ch = src[offset];
  58. while (ch === ' ') {
  59. ch = src[offset += 1];
  60. }
  61. return offset;
  62. }
  63. }, {
  64. key: "endOfLine",
  65. value: function endOfLine(src, offset) {
  66. var ch = src[offset];
  67. while (ch && ch !== '\n') {
  68. ch = src[offset += 1];
  69. }
  70. return offset;
  71. }
  72. }, {
  73. key: "endOfWhiteSpace",
  74. value: function endOfWhiteSpace(src, offset) {
  75. var ch = src[offset];
  76. while (ch === '\t' || ch === ' ') {
  77. ch = src[offset += 1];
  78. }
  79. return offset;
  80. }
  81. }, {
  82. key: "startOfLine",
  83. value: function startOfLine(src, offset) {
  84. var ch = src[offset - 1];
  85. if (ch === '\n') return offset;
  86. while (ch && ch !== '\n') {
  87. ch = src[offset -= 1];
  88. }
  89. return offset + 1;
  90. }
  91. /**
  92. * End of indentation, or null if the line's indent level is not more
  93. * than `indent`
  94. *
  95. * @param {string} src
  96. * @param {number} indent
  97. * @param {number} lineStart
  98. * @returns {?number}
  99. */
  100. }, {
  101. key: "endOfBlockIndent",
  102. value: function endOfBlockIndent(src, indent, lineStart) {
  103. var inEnd = Node.endOfIndent(src, lineStart);
  104. if (inEnd > lineStart + indent) {
  105. return inEnd;
  106. } else {
  107. var wsEnd = Node.endOfWhiteSpace(src, inEnd);
  108. var ch = src[wsEnd];
  109. if (!ch || ch === '\n') return wsEnd;
  110. }
  111. return null;
  112. }
  113. }, {
  114. key: "atBlank",
  115. value: function atBlank(src, offset, endAsBlank) {
  116. var ch = src[offset];
  117. return ch === '\n' || ch === '\t' || ch === ' ' || endAsBlank && !ch;
  118. }
  119. }, {
  120. key: "atCollectionItem",
  121. value: function atCollectionItem(src, offset) {
  122. var ch = src[offset];
  123. return (ch === '?' || ch === ':' || ch === '-') && Node.atBlank(src, offset + 1, true);
  124. }
  125. }, {
  126. key: "nextNodeIsIndented",
  127. value: function nextNodeIsIndented(ch, indentDiff, indicatorAsIndent) {
  128. if (!ch || indentDiff < 0) return false;
  129. if (indentDiff > 0) return true;
  130. return indicatorAsIndent && ch === '-';
  131. } // should be at line or string end, or at next non-whitespace char
  132. }, {
  133. key: "normalizeOffset",
  134. value: function normalizeOffset(src, offset) {
  135. var ch = src[offset];
  136. return !ch ? offset : ch !== '\n' && src[offset - 1] === '\n' ? offset - 1 : Node.endOfWhiteSpace(src, offset);
  137. } // fold single newline into space, multiple newlines to N - 1 newlines
  138. // presumes src[offset] === '\n'
  139. }, {
  140. key: "foldNewline",
  141. value: function foldNewline(src, offset, indent) {
  142. var inCount = 0;
  143. var error = false;
  144. var fold = '';
  145. var ch = src[offset + 1];
  146. while (ch === ' ' || ch === '\t' || ch === '\n') {
  147. switch (ch) {
  148. case '\n':
  149. inCount = 0;
  150. offset += 1;
  151. fold += '\n';
  152. break;
  153. case '\t':
  154. if (inCount <= indent) error = true;
  155. offset = Node.endOfWhiteSpace(src, offset + 2) - 1;
  156. break;
  157. case ' ':
  158. inCount += 1;
  159. offset += 1;
  160. break;
  161. }
  162. ch = src[offset + 1];
  163. }
  164. if (!fold) fold = ' ';
  165. if (ch && inCount <= indent) error = true;
  166. return {
  167. fold: fold,
  168. offset: offset,
  169. error: error
  170. };
  171. }
  172. }]);
  173. function Node(type, props, context) {
  174. (0, _classCallCheck2.default)(this, Node);
  175. Object.defineProperty(this, 'context', {
  176. value: context || null,
  177. writable: true
  178. });
  179. this.error = null;
  180. this.range = null;
  181. this.valueRange = null;
  182. this.props = props || [];
  183. this.type = type;
  184. this.value = null;
  185. }
  186. (0, _createClass2.default)(Node, [{
  187. key: "getPropValue",
  188. value: function getPropValue(idx, key, skipKey) {
  189. if (!this.context) return null;
  190. var src = this.context.src;
  191. var prop = this.props[idx];
  192. return prop && src[prop.start] === key ? src.slice(prop.start + (skipKey ? 1 : 0), prop.end) : null;
  193. }
  194. }, {
  195. key: "commentHasRequiredWhitespace",
  196. value: function commentHasRequiredWhitespace(start) {
  197. var src = this.context.src;
  198. if (this.header && start === this.header.end) return false;
  199. if (!this.valueRange) return false;
  200. var end = this.valueRange.end;
  201. return start !== end || Node.atBlank(src, end - 1);
  202. }
  203. }, {
  204. key: "parseComment",
  205. value: function parseComment(start) {
  206. var src = this.context.src;
  207. if (src[start] === _constants.Char.COMMENT) {
  208. var end = Node.endOfLine(src, start + 1);
  209. var commentRange = new _Range.default(start, end);
  210. this.props.push(commentRange);
  211. return end;
  212. }
  213. return start;
  214. }
  215. /**
  216. * Populates the `origStart` and `origEnd` values of all ranges for this
  217. * node. Extended by child classes to handle descendant nodes.
  218. *
  219. * @param {number[]} cr - Positions of dropped CR characters
  220. * @param {number} offset - Starting index of `cr` from the last call
  221. * @returns {number} - The next offset, matching the one found for `origStart`
  222. */
  223. }, {
  224. key: "setOrigRanges",
  225. value: function setOrigRanges(cr, offset) {
  226. if (this.range) offset = this.range.setOrigRange(cr, offset);
  227. if (this.valueRange) this.valueRange.setOrigRange(cr, offset);
  228. this.props.forEach(function (prop) {
  229. return prop.setOrigRange(cr, offset);
  230. });
  231. return offset;
  232. }
  233. }, {
  234. key: "toString",
  235. value: function toString() {
  236. var src = this.context.src,
  237. range = this.range,
  238. value = this.value;
  239. if (value != null) return value;
  240. var str = src.slice(range.start, range.end);
  241. return Node.addStringTerminator(src, range.end, str);
  242. }
  243. }, {
  244. key: "anchor",
  245. get: function get() {
  246. for (var i = 0; i < this.props.length; ++i) {
  247. var anchor = this.getPropValue(i, _constants.Char.ANCHOR, true);
  248. if (anchor != null) return anchor;
  249. }
  250. return null;
  251. }
  252. }, {
  253. key: "comment",
  254. get: function get() {
  255. var comments = [];
  256. for (var i = 0; i < this.props.length; ++i) {
  257. var comment = this.getPropValue(i, _constants.Char.COMMENT, true);
  258. if (comment != null) comments.push(comment);
  259. }
  260. return comments.length > 0 ? comments.join('\n') : null;
  261. }
  262. }, {
  263. key: "hasComment",
  264. get: function get() {
  265. if (this.context) {
  266. var src = this.context.src;
  267. for (var i = 0; i < this.props.length; ++i) {
  268. if (src[this.props[i].start] === _constants.Char.COMMENT) return true;
  269. }
  270. }
  271. return false;
  272. }
  273. }, {
  274. key: "hasProps",
  275. get: function get() {
  276. if (this.context) {
  277. var src = this.context.src;
  278. for (var i = 0; i < this.props.length; ++i) {
  279. if (src[this.props[i].start] !== _constants.Char.COMMENT) return true;
  280. }
  281. }
  282. return false;
  283. }
  284. }, {
  285. key: "includesTrailingLines",
  286. get: function get() {
  287. return false;
  288. }
  289. }, {
  290. key: "jsonLike",
  291. get: function get() {
  292. var jsonLikeTypes = [_constants.Type.FLOW_MAP, _constants.Type.FLOW_SEQ, _constants.Type.QUOTE_DOUBLE, _constants.Type.QUOTE_SINGLE];
  293. return jsonLikeTypes.indexOf(this.type) !== -1;
  294. }
  295. }, {
  296. key: "rangeAsLinePos",
  297. get: function get() {
  298. if (!this.range || !this.context) return undefined;
  299. var start = (0, _sourceUtils.getLinePos)(this.range.start, this.context.root);
  300. if (!start) return undefined;
  301. var end = (0, _sourceUtils.getLinePos)(this.range.end, this.context.root);
  302. return {
  303. start: start,
  304. end: end
  305. };
  306. }
  307. }, {
  308. key: "rawValue",
  309. get: function get() {
  310. if (!this.valueRange || !this.context) return null;
  311. var _this$valueRange = this.valueRange,
  312. start = _this$valueRange.start,
  313. end = _this$valueRange.end;
  314. return this.context.src.slice(start, end);
  315. }
  316. }, {
  317. key: "tag",
  318. get: function get() {
  319. for (var i = 0; i < this.props.length; ++i) {
  320. var tag = this.getPropValue(i, _constants.Char.TAG, false);
  321. if (tag != null) {
  322. if (tag[1] === '<') {
  323. return {
  324. verbatim: tag.slice(2, -1)
  325. };
  326. } else {
  327. // eslint-disable-next-line no-unused-vars
  328. var _tag$match = tag.match(/^(.*!)([^!]*)$/),
  329. _tag$match2 = (0, _slicedToArray2.default)(_tag$match, 3),
  330. _ = _tag$match2[0],
  331. handle = _tag$match2[1],
  332. suffix = _tag$match2[2];
  333. return {
  334. handle: handle,
  335. suffix: suffix
  336. };
  337. }
  338. }
  339. }
  340. return null;
  341. }
  342. }, {
  343. key: "valueRangeContainsNewline",
  344. get: function get() {
  345. if (!this.valueRange || !this.context) return false;
  346. var _this$valueRange2 = this.valueRange,
  347. start = _this$valueRange2.start,
  348. end = _this$valueRange2.end;
  349. var src = this.context.src;
  350. for (var i = start; i < end; ++i) {
  351. if (src[i] === '\n') return true;
  352. }
  353. return false;
  354. }
  355. }]);
  356. return Node;
  357. }();
  358. exports.default = Node;