@html-validate/plugin-utils
Version:
Plugin utilities and helpers for writing plugins to HTML-Validate
8 lines (7 loc) • 10.6 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/position-from-offset.ts", "../../src/position-to-offset.ts", "../../src/template-extractor.ts"],
"sourcesContent": ["/**\n * Given an offset into a source, calculate the corresponding line and column.\n *\n * @public\n * @since 1.0.0\n */\nexport function positionFromOffset(text: string, offset: number): [line: number, column: number] {\n\tlet line = 1;\n\tlet prev = 0;\n\tlet pos = text.indexOf(\"\\n\");\n\n\t/* step one line at a time until newline position is beyond wanted offset */\n\twhile (pos !== -1) {\n\t\tif (pos >= offset) {\n\t\t\treturn [line, offset - prev + 1];\n\t\t}\n\t\tline++;\n\t\tprev = pos + 1;\n\t\tpos = text.indexOf(\"\\n\", pos + 1);\n\t}\n\n\t/* missing final newline */\n\treturn [line, offset - prev + 1];\n}\n", "import { type Position } from \"./position\";\n\n/**\n * Compute source offset from line and column and the given markup.\n *\n * @public\n * @since 1.0.0\n * @param position - Line and column.\n * @param data - Source markup.\n * @returns The byte offset into the markup which line and column corresponds to.\n */\nexport function positionToOffset(position: Position, data: string): number {\n\tlet line = position.line;\n\tlet column = position.column + 1;\n\t/* eslint-disable-next-line unicorn/no-for-loop -- need to access index */\n\tfor (let i = 0; i < data.length; i++) {\n\t\tif (line > 1) {\n\t\t\t/* not yet on the correct line */\n\t\t\tif (data[i] === \"\\n\") {\n\t\t\t\tline--;\n\t\t\t}\n\t\t} else if (column > 1) {\n\t\t\t/* not yet on the correct column */\n\t\t\tcolumn--;\n\t\t} else {\n\t\t\t/* line/column found, return current position */\n\t\t\treturn i;\n\t\t}\n\t}\n\t/* istanbul ignore next: should never reach this line unless espree passes bad\n\t * positions, no sane way to test */\n\tthrow new Error(\"Failed to compute location offset from position\");\n}\n", "/* eslint-disable @typescript-eslint/no-non-null-assertion -- declarations say\n * location fields are optional but they are always present when `{loc: true}` */\n\n/* eslint-disable-next-line n/no-extraneous-import -- type is pulled via acorn-walk */\nimport { type Node } from \"acorn\";\nimport * as walk from \"acorn-walk\";\nimport * as espree from \"espree\";\n\nimport { type Source } from \"html-validate\";\nimport { positionToOffset } from \"./position-to-offset\";\n\n/* espree puts location information a bit different than estree */\ndeclare module \"estree\" {\n\tinterface TemplateElement {\n\t\tstart: number;\n\t\tend: number;\n\t}\n}\n\nfunction joinTemplateLiteral(nodes: espree.TemplateElement[]): string {\n\tlet offset = nodes[0].start + 1;\n\tlet output = \"\";\n\tfor (const node of nodes) {\n\t\toutput += \" \".repeat(node.start + 1 - offset);\n\t\toutput += node.value.raw;\n\t\toffset = node.end - 2;\n\t}\n\treturn output;\n}\n\nfunction extractLiteral(\n\tnode: espree.Expression | espree.Pattern | espree.Literal | espree.BlockStatement,\n\tfilename: string,\n\tdata: string,\n): Source | null {\n\tswitch (node.type) {\n\t\t/* ignored nodes */\n\t\tcase \"FunctionExpression\":\n\t\tcase \"Identifier\":\n\t\t\treturn null;\n\n\t\tcase \"Literal\":\n\t\t\tif (typeof node.value !== \"string\") {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tdata: node.value,\n\t\t\t\tfilename,\n\t\t\t\tline: node.loc!.start.line,\n\t\t\t\tcolumn: node.loc!.start.column + 1,\n\t\t\t\toffset: positionToOffset(node.loc!.start, data) + 1,\n\t\t\t};\n\n\t\tcase \"TemplateLiteral\":\n\t\t\treturn {\n\t\t\t\tdata: joinTemplateLiteral(node.quasis),\n\t\t\t\tfilename,\n\t\t\t\tline: node.loc!.start.line,\n\t\t\t\tcolumn: node.loc!.start.column + 1,\n\t\t\t\toffset: positionToOffset(node.loc!.start, data) + 1,\n\t\t\t};\n\n\t\tcase \"TaggedTemplateExpression\":\n\t\t\treturn {\n\t\t\t\tdata: joinTemplateLiteral(node.quasi.quasis),\n\t\t\t\tfilename,\n\t\t\t\tline: node.quasi.loc!.start.line,\n\t\t\t\tcolumn: node.quasi.loc!.start.column + 1,\n\t\t\t\toffset: positionToOffset(node.quasi.loc!.start, data) + 1,\n\t\t\t};\n\n\t\tcase \"ArrowFunctionExpression\": {\n\t\t\tconst whitelist = [\"Literal\", \"TemplateLiteral\"];\n\t\t\tif (whitelist.includes(node.body.type)) {\n\t\t\t\treturn extractLiteral(node.body, filename, data);\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\n\t\t/* istanbul ignore next: this only provides a better error, all currently known nodes are tested */\n\t\tdefault: {\n\t\t\tconst loc = node.loc!.start;\n\t\t\tconst line = String(loc.line);\n\t\t\tconst column = String(loc.column);\n\t\t\tconst context = `${filename}:${line}:${column}`;\n\t\t\tthrow new Error(`Unhandled node type \"${node.type}\" at \"${context}\" in extractLiteral`);\n\t\t}\n\t}\n}\n\nfunction compareKey(\n\tnode: espree.Expression | espree.PrivateIdentifier,\n\tkey: string,\n\tfilename: string,\n): boolean {\n\tswitch (node.type) {\n\t\tcase \"Identifier\":\n\t\t\treturn node.name === key;\n\n\t\tcase \"Literal\":\n\t\t\treturn node.value === key;\n\n\t\t/* istanbul ignore next: this only provides a better error, all currently known nodes are tested */\n\t\tdefault: {\n\t\t\tconst loc = node.loc!.start;\n\t\t\tconst line = String(loc.line);\n\t\t\tconst column = String(loc.column);\n\t\t\tconst context = `${filename}:${line}:${column}`;\n\t\t\tthrow new Error(`Unhandled node type \"${node.type}\" at \"${context}\" in compareKey`);\n\t\t}\n\t}\n}\n\n/**\n * @public\n * @since 1.0.0\n */\nexport class TemplateExtractor {\n\tprivate ast: espree.Program;\n\n\tprivate filename: string;\n\tprivate data: string;\n\n\tprivate constructor(ast: espree.Program, filename: string, data: string) {\n\t\tthis.ast = ast;\n\t\tthis.filename = filename;\n\t\tthis.data = data;\n\t}\n\n\t/**\n\t * Create a new [[TemplateExtractor]] from javascript source code.\n\t *\n\t * `Source` offsets will be relative to the string, i.e. offset 0 is the first\n\t * character of the string. If the string is only a subset of a larger string\n\t * the offsets must be adjusted manually.\n\t *\n\t * @param source - Source code.\n\t * @param filename - Optional filename to set in the resulting\n\t * `Source`. Defauls to `\"inline\"`.\n\t */\n\tpublic static fromString(source: string, filename?: string): TemplateExtractor {\n\t\tconst ast = espree.parse(source, {\n\t\t\tecmaVersion: \"latest\",\n\t\t\tsourceType: \"module\",\n\t\t\tloc: true,\n\t\t});\n\t\treturn new TemplateExtractor(ast, filename ?? \"inline\", source);\n\t}\n\n\t/**\n\t * Extract object properties.\n\t *\n\t * Given a key `\"template\"` this method finds all objects literals with a\n\t * `\"template\"` property and creates a [[Source]] instance with proper offsets\n\t * with the value of the property. For instance:\n\t *\n\t * ```\n\t * const myObj = {\n\t * foo: 'bar',\n\t * };\n\t * ```\n\t *\n\t * The above snippet would yield a `Source` with the content `bar`.\n\t *\n\t */\n\tpublic extractObjectProperty(key: string): Source[] {\n\t\tconst result: Source[] = [];\n\t\tconst { filename, data } = this;\n\t\tconst node: Node = this.ast as unknown as Node;\n\t\twalk.simple(node, {\n\t\t\tProperty(node: espree.Property) {\n\t\t\t\tif (compareKey(node.key, key, filename)) {\n\t\t\t\t\tconst source = extractLiteral(node.value, filename, data);\n\t\t\t\t\tif (source) {\n\t\t\t\t\t\tsource.filename = filename;\n\t\t\t\t\t\tresult.push(source);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t} as unknown as walk.SimpleVisitors<unknown>);\n\t\treturn result;\n\t}\n}\n"],
"mappings": ";AAMO,SAAS,mBAAmB,MAAc,QAAgD;AAChG,MAAI,OAAO;AACX,MAAI,OAAO;AACX,MAAI,MAAM,KAAK,QAAQ,IAAI;AAG3B,SAAO,QAAQ,IAAI;AAClB,QAAI,OAAO,QAAQ;AAClB,aAAO,CAAC,MAAM,SAAS,OAAO,CAAC;AAAA,IAChC;AACA;AACA,WAAO,MAAM;AACb,UAAM,KAAK,QAAQ,MAAM,MAAM,CAAC;AAAA,EACjC;AAGA,SAAO,CAAC,MAAM,SAAS,OAAO,CAAC;AAChC;;;ACZO,SAAS,iBAAiB,UAAoB,MAAsB;AAC1E,MAAI,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