comment-parser
Version:
Generic JSDoc-like comment parser
1 lines • 5.99 kB
Source Map (JSON)
{"version":3,"file":"type.cjs","names":["Object","defineProperty","exports","value","default","typeTokenizer","util_js_1","require","spacing","join","getJoiner","spec","curlies","lines","descriptionBegun","firstTypeIteration","tokens","source","values","type","description","trim","ch","push","problems","code","message","line","number","critical","parts","offset","postDelimiter","length","i","entries","slice","postType","splitSpace","x","t","map"],"sources":["type.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = typeTokenizer;\nconst util_js_1 = require(\"../../util.js\");\n/**\n * Sets splits remaining `Spec.lines[].tokens.description` into `type` and `description`\n * tokens and populates Spec.type`\n *\n * @param {Spacing} spacing tells how to deal with a whitespace\n * for type values going over multiple lines\n */\nfunction typeTokenizer(spacing = 'compact') {\n const join = getJoiner(spacing);\n return (spec) => {\n let curlies = 0;\n let lines = [];\n let descriptionBegun = false;\n let firstTypeIteration = true;\n for (const { tokens } of spec.source.values()) {\n let type = '';\n if (!descriptionBegun && tokens.description.trim()) {\n descriptionBegun = true;\n }\n else if (!descriptionBegun) {\n continue;\n }\n if (firstTypeIteration && tokens.description[0] !== '{')\n return spec;\n firstTypeIteration = false;\n for (const ch of tokens.description) {\n if (ch === '{')\n curlies++;\n if (ch === '}')\n curlies--;\n type += ch;\n if (curlies === 0)\n break;\n }\n lines.push([tokens, type]);\n if (curlies === 0)\n break;\n }\n if (!descriptionBegun) {\n return spec;\n }\n if (curlies !== 0) {\n spec.problems.push({\n code: 'spec:type:unpaired-curlies',\n message: 'unpaired curlies',\n line: spec.source[0].number,\n critical: true,\n });\n return spec;\n }\n const parts = [];\n const offset = lines[0][0].postDelimiter.length;\n for (const [i, [tokens, type]] of lines.entries()) {\n tokens.type = type;\n if (i > 0) {\n tokens.type = tokens.postDelimiter.slice(offset) + type;\n tokens.postDelimiter = tokens.postDelimiter.slice(0, offset);\n }\n [tokens.postType, tokens.description] = (0, util_js_1.splitSpace)(tokens.description.slice(type.length));\n parts.push(tokens.type);\n }\n parts[0] = parts[0].slice(1);\n parts[parts.length - 1] = parts[parts.length - 1].slice(0, -1);\n spec.type = join(parts);\n return spec;\n };\n}\nconst trim = (x) => x.trim();\nfunction getJoiner(spacing) {\n if (spacing === 'compact')\n return (t) => t.map(trim).join('');\n else if (spacing === 'preserve')\n return (t) => t.join('\\n');\n else\n return spacing;\n}\n"],"mappings":"AAAA,YAAY;;AACZA,MAAM,CAACC,cAAc,CAACC,OAAO,EAAE,YAAY,EAAE;EAAEC,KAAK,EAAE;AAAK,CAAC,CAAC;AAC7DD,OAAO,CAACE,OAAO,GAAGC,aAAa;AAC/B,MAAMC,SAAS,GAAGC,OAAO,kBAAiB;AAC1C;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASF,aAAaA,CAACG,OAAO,GAAG,SAAS,EAAE;EACxC,MAAMC,IAAI,GAAGC,SAAS,CAACF,OAAO,CAAC;EAC/B,OAAQG,IAAI,IAAK;IACb,IAAIC,OAAO,GAAG,CAAC;IACf,IAAIC,KAAK,GAAG,EAAE;IACd,IAAIC,gBAAgB,GAAG,KAAK;IAC5B,IAAIC,kBAAkB,GAAG,IAAI;IAC7B,KAAK,MAAM;MAAEC;IAAO,CAAC,IAAIL,IAAI,CAACM,MAAM,CAACC,MAAM,CAAC,CAAC,EAAE;MAC3C,IAAIC,IAAI,GAAG,EAAE;MACb,IAAI,CAACL,gBAAgB,IAAIE,MAAM,CAACI,WAAW,CAACC,IAAI,CAAC,CAAC,EAAE;QAChDP,gBAAgB,GAAG,IAAI;MAC3B,CAAC,MACI,IAAI,CAACA,gBAAgB,EAAE;QACxB;MACJ;MACA,IAAIC,kBAAkB,IAAIC,MAAM,CAACI,WAAW,CAAC,CAAC,CAAC,KAAK,GAAG,EACnD,OAAOT,IAAI;MACfI,kBAAkB,GAAG,KAAK;MAC1B,KAAK,MAAMO,EAAE,IAAIN,MAAM,CAACI,WAAW,EAAE;QACjC,IAAIE,EAAE,KAAK,GAAG,EACV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