UNPKG

@jiasuyun/apier-parser-json5

Version:
164 lines 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const helper_1 = require("./helper"); const apier_parser_base_1 = require("@jiasuyun/apier-parser-base"); const RE_META_COMMENT = /^\s*\/\/\s*@/; function visit(args) { while (args.kind !== "break") { if (args.kind === "scopeArray") { args = scopeArray(args); } else if (args.kind === "scopeObject") { args = scopeObject(args); } else if (args.kind === "enterScope") { args = enterScope(args); } else if (args.kind === "exitScope") { args = exitScope(args); } } } exports.visit = visit; function scopeArray(args) { const { lineIndex } = args; const lineValue = getLineValue(args); switch (lineValue.kind) { case helper_1.LineKind.OBJECT: return getNextArgs(args, { kind: "enterScope", canCollectMetaComment: false }); case helper_1.LineKind.EXIT: return getNextArgs(args, { kind: "exitScope", lineIndex: lineIndex + 1, canCollectMetaComment: false }); default: if (args.canCollectMetaComment) { collectMetaComment(args); } return getNextArgs(args, { kind: "scopeArray", lineIndex: lineIndex + 1 }); } } function scopeObject(args) { const { lineIndex } = args; const lineValue = getLineValue(args); switch (lineValue.kind) { case helper_1.LineKind.ARRAY: case helper_1.LineKind.OBJECT: return getNextArgs(args, { kind: "enterScope", canCollectMetaComment: false }); case helper_1.LineKind.KV: const paths = [...args.paths, lineValue.key]; collectComment(args, paths); return getNextArgs(args, { kind: "scopeObject", lineIndex: lineIndex + 1, canCollectMetaComment: false }); case helper_1.LineKind.EXIT: return getNextArgs(args, { kind: "exitScope", lineIndex: lineIndex + 1, canCollectMetaComment: false }); default: if (args.canCollectMetaComment) { collectMetaComment(args); } return getNextArgs(args, { kind: "scopeObject", lineIndex: lineIndex + 1 }); } } function enterScope(args) { const { lineIndex } = args; const lineValue = getLineValue(args); const paths = [...args.paths, lineValue.key]; collectComment(args, paths); if (lineValue.kind === helper_1.LineKind.ARRAY) { return getNextArgs(args, { kind: "scopeArray", numChild: 0, lineIndex: lineIndex + 1, canCollectMetaComment: true, paths, root: args }); } else { return getNextArgs(args, { kind: "scopeObject", lineIndex: lineIndex + 1, canCollectMetaComment: true, paths, root: args }); } } function exitScope(args) { if (!args.root) { return getNextArgs(args, { kind: "break" }); } const parnetArgs = args.root; const kind = parnetArgs.root ? getLineValue(parnetArgs.root).kind : helper_1.LineKind.OBJECT; if (kind === helper_1.LineKind.ARRAY) { parnetArgs.numChild++; return getNextArgs(parnetArgs, { kind: "scopeArray", lineIndex: args.lineIndex, canCollectMetaComment: true, root: parnetArgs.root }); } else { return getNextArgs(parnetArgs, { kind: "scopeObject", numChild: 0, lineIndex: args.lineIndex, canCollectMetaComment: true, root: parnetArgs.root }); } } function collectMetaComment(args) { const { comment, paths, lines, lineIndex } = args; const line = lines[lineIndex]; const match = RE_META_COMMENT.exec(line); if (match) { comment.appendMeta(paths, line.slice(match[0].length)); } } function collectComment(args, paths) { const { comment, lines, lineIndex } = args; const line = lines[lineIndex]; const commentText = helper_1.getLineComment(line); if (commentText) comment.append(paths, commentText); } function getNextArgs(args, changes) { return Object.assign({ numChild: 0 }, args, changes); } function getLineValue(args) { const { lines, lineIndex } = args; let line = lines[lineIndex]; let lineValue; try { lineValue = helper_1.valueOfLine(line); } catch (err) { throw new apier_parser_base_1.CommentParserError(lineIndex + 1, line); } if (!lineValue.key) lineValue.key = String(args.numChild || 0); return lineValue; } //# sourceMappingURL=Visitor.js.map