| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
-
- var _constants = require("../constants");
-
- var _sourceUtils = require("./source-utils");
-
- var _Range = _interopRequireDefault(require("./Range"));
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- /** Root class of all nodes */
- class Node {
- static addStringTerminator(src, offset, str) {
- if (str[str.length - 1] === '\n') return str;
- const next = Node.endOfWhiteSpace(src, offset);
- return next >= src.length || src[next] === '\n' ? str + '\n' : str;
- } // ^(---|...)
-
-
- static atDocumentBoundary(src, offset, sep) {
- const ch0 = src[offset];
- if (!ch0) return true;
- const prev = src[offset - 1];
- if (prev && prev !== '\n') return false;
-
- if (sep) {
- if (ch0 !== sep) return false;
- } else {
- if (ch0 !== _constants.Char.DIRECTIVES_END && ch0 !== _constants.Char.DOCUMENT_END) return false;
- }
-
- const ch1 = src[offset + 1];
- const ch2 = src[offset + 2];
- if (ch1 !== ch0 || ch2 !== ch0) return false;
- const ch3 = src[offset + 3];
- return !ch3 || ch3 === '\n' || ch3 === '\t' || ch3 === ' ';
- }
-
- static endOfIdentifier(src, offset) {
- let ch = src[offset];
- const isVerbatim = ch === '<';
- const notOk = isVerbatim ? ['\n', '\t', ' ', '>'] : ['\n', '\t', ' ', '[', ']', '{', '}', ','];
-
- while (ch && notOk.indexOf(ch) === -1) ch = src[offset += 1];
-
- if (isVerbatim && ch === '>') offset += 1;
- return offset;
- }
-
- static endOfIndent(src, offset) {
- let ch = src[offset];
-
- while (ch === ' ') ch = src[offset += 1];
-
- return offset;
- }
-
- static endOfLine(src, offset) {
- let ch = src[offset];
-
- while (ch && ch !== '\n') ch = src[offset += 1];
-
- return offset;
- }
-
- static endOfWhiteSpace(src, offset) {
- let ch = src[offset];
-
- while (ch === '\t' || ch === ' ') ch = src[offset += 1];
-
- return offset;
- }
-
- static startOfLine(src, offset) {
- let ch = src[offset - 1];
- if (ch === '\n') return offset;
-
- while (ch && ch !== '\n') ch = src[offset -= 1];
-
- return offset + 1;
- }
- /**
- * End of indentation, or null if the line's indent level is not more
- * than `indent`
- *
- * @param {string} src
- * @param {number} indent
- * @param {number} lineStart
- * @returns {?number}
- */
-
-
- static endOfBlockIndent(src, indent, lineStart) {
- const inEnd = Node.endOfIndent(src, lineStart);
-
- if (inEnd > lineStart + indent) {
- return inEnd;
- } else {
- const wsEnd = Node.endOfWhiteSpace(src, inEnd);
- const ch = src[wsEnd];
- if (!ch || ch === '\n') return wsEnd;
- }
-
- return null;
- }
-
- static atBlank(src, offset, endAsBlank) {
- const ch = src[offset];
- return ch === '\n' || ch === '\t' || ch === ' ' || endAsBlank && !ch;
- }
-
- static atCollectionItem(src, offset) {
- const ch = src[offset];
- return (ch === '?' || ch === ':' || ch === '-') && Node.atBlank(src, offset + 1, true);
- }
-
- static nextNodeIsIndented(ch, indentDiff, indicatorAsIndent) {
- if (!ch || indentDiff < 0) return false;
- if (indentDiff > 0) return true;
- return indicatorAsIndent && ch === '-';
- } // should be at line or string end, or at next non-whitespace char
-
-
- static normalizeOffset(src, offset) {
- const ch = src[offset];
- return !ch ? offset : ch !== '\n' && src[offset - 1] === '\n' ? offset - 1 : Node.endOfWhiteSpace(src, offset);
- } // fold single newline into space, multiple newlines to N - 1 newlines
- // presumes src[offset] === '\n'
-
-
- static foldNewline(src, offset, indent) {
- let inCount = 0;
- let error = false;
- let fold = '';
- let ch = src[offset + 1];
-
- while (ch === ' ' || ch === '\t' || ch === '\n') {
- switch (ch) {
- case '\n':
- inCount = 0;
- offset += 1;
- fold += '\n';
- break;
-
- case '\t':
- if (inCount <= indent) error = true;
- offset = Node.endOfWhiteSpace(src, offset + 2) - 1;
- break;
-
- case ' ':
- inCount += 1;
- offset += 1;
- break;
- }
-
- ch = src[offset + 1];
- }
-
- if (!fold) fold = ' ';
- if (ch && inCount <= indent) error = true;
- return {
- fold,
- offset,
- error
- };
- }
-
- constructor(type, props, context) {
- Object.defineProperty(this, 'context', {
- value: context || null,
- writable: true
- });
- this.error = null;
- this.range = null;
- this.valueRange = null;
- this.props = props || [];
- this.type = type;
- this.value = null;
- }
-
- getPropValue(idx, key, skipKey) {
- if (!this.context) return null;
- const {
- src
- } = this.context;
- const prop = this.props[idx];
- return prop && src[prop.start] === key ? src.slice(prop.start + (skipKey ? 1 : 0), prop.end) : null;
- }
-
- get anchor() {
- for (let i = 0; i < this.props.length; ++i) {
- const anchor = this.getPropValue(i, _constants.Char.ANCHOR, true);
- if (anchor != null) return anchor;
- }
-
- return null;
- }
-
- get comment() {
- const comments = [];
-
- for (let i = 0; i < this.props.length; ++i) {
- const comment = this.getPropValue(i, _constants.Char.COMMENT, true);
- if (comment != null) comments.push(comment);
- }
-
- return comments.length > 0 ? comments.join('\n') : null;
- }
-
- commentHasRequiredWhitespace(start) {
- const {
- src
- } = this.context;
- if (this.header && start === this.header.end) return false;
- if (!this.valueRange) return false;
- const {
- end
- } = this.valueRange;
- return start !== end || Node.atBlank(src, end - 1);
- }
-
- get hasComment() {
- if (this.context) {
- const {
- src
- } = this.context;
-
- for (let i = 0; i < this.props.length; ++i) {
- if (src[this.props[i].start] === _constants.Char.COMMENT) return true;
- }
- }
-
- return false;
- }
-
- get hasProps() {
- if (this.context) {
- const {
- src
- } = this.context;
-
- for (let i = 0; i < this.props.length; ++i) {
- if (src[this.props[i].start] !== _constants.Char.COMMENT) return true;
- }
- }
-
- return false;
- }
-
- get includesTrailingLines() {
- return false;
- }
-
- get jsonLike() {
- const jsonLikeTypes = [_constants.Type.FLOW_MAP, _constants.Type.FLOW_SEQ, _constants.Type.QUOTE_DOUBLE, _constants.Type.QUOTE_SINGLE];
- return jsonLikeTypes.indexOf(this.type) !== -1;
- }
-
- get rangeAsLinePos() {
- if (!this.range || !this.context) return undefined;
- const start = (0, _sourceUtils.getLinePos)(this.range.start, this.context.root);
- if (!start) return undefined;
- const end = (0, _sourceUtils.getLinePos)(this.range.end, this.context.root);
- return {
- start,
- end
- };
- }
-
- get rawValue() {
- if (!this.valueRange || !this.context) return null;
- const {
- start,
- end
- } = this.valueRange;
- return this.context.src.slice(start, end);
- }
-
- get tag() {
- for (let i = 0; i < this.props.length; ++i) {
- const tag = this.getPropValue(i, _constants.Char.TAG, false);
-
- if (tag != null) {
- if (tag[1] === '<') {
- return {
- verbatim: tag.slice(2, -1)
- };
- } else {
- // eslint-disable-next-line no-unused-vars
- const [_, handle, suffix] = tag.match(/^(.*!)([^!]*)$/);
- return {
- handle,
- suffix
- };
- }
- }
- }
-
- return null;
- }
-
- get valueRangeContainsNewline() {
- if (!this.valueRange || !this.context) return false;
- const {
- start,
- end
- } = this.valueRange;
- const {
- src
- } = this.context;
-
- for (let i = start; i < end; ++i) {
- if (src[i] === '\n') return true;
- }
-
- return false;
- }
-
- parseComment(start) {
- const {
- src
- } = this.context;
-
- if (src[start] === _constants.Char.COMMENT) {
- const end = Node.endOfLine(src, start + 1);
- const commentRange = new _Range.default(start, end);
- this.props.push(commentRange);
- return end;
- }
-
- return start;
- }
- /**
- * Populates the `origStart` and `origEnd` values of all ranges for this
- * node. Extended by child classes to handle descendant nodes.
- *
- * @param {number[]} cr - Positions of dropped CR characters
- * @param {number} offset - Starting index of `cr` from the last call
- * @returns {number} - The next offset, matching the one found for `origStart`
- */
-
-
- setOrigRanges(cr, offset) {
- if (this.range) offset = this.range.setOrigRange(cr, offset);
- if (this.valueRange) this.valueRange.setOrigRange(cr, offset);
- this.props.forEach(prop => prop.setOrigRange(cr, offset));
- return offset;
- }
-
- toString() {
- const {
- context: {
- src
- },
- range,
- value
- } = this;
- if (value != null) return value;
- const str = src.slice(range.start, range.end);
- return Node.addStringTerminator(src, range.end, str);
- }
-
- }
-
- exports.default = Node;
|