@html-validate/plugin-utils 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,14 +25,16 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
25
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
26
  mod
27
27
  ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
29
 
29
- // src/index.ts
30
- var src_exports = {};
31
- __export(src_exports, {
30
+ // src/entry-cjs.ts
31
+ var entry_cjs_exports = {};
32
+ __export(entry_cjs_exports, {
32
33
  TemplateExtractor: () => TemplateExtractor,
33
34
  positionFromOffset: () => positionFromOffset,
34
35
  positionToOffset: () => positionToOffset
35
36
  });
37
+ module.exports = __toCommonJS(entry_cjs_exports);
36
38
 
37
39
  // src/position-from-offset.ts
38
40
  function positionFromOffset(text, offset) {
@@ -210,7 +212,10 @@ var TemplateExtractor = class _TemplateExtractor {
210
212
  return result;
211
213
  }
212
214
  };
213
-
214
- // src/entry-cjs.ts
215
- module.exports = { ...src_exports };
215
+ // Annotate the CommonJS export names for ESM import in node:
216
+ 0 && (module.exports = {
217
+ TemplateExtractor,
218
+ positionFromOffset,
219
+ positionToOffset
220
+ });
216
221
  //# sourceMappingURL=index.cjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/index.ts", "../../src/position-from-offset.ts", "../../src/position-to-offset.ts", "../../src/template-extractor.ts", "../../src/entry-cjs.ts"],
4
- "sourcesContent": ["export { type Position } from \"./position\";\nexport { positionFromOffset } from \"./position-from-offset\";\nexport { positionToOffset } from \"./position-to-offset\";\nexport { TemplateExtractor } from \"./template-extractor\";\n", "/**\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\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\nimport * as espree from \"espree\";\nimport * as walk from \"acorn-walk\";\n\n/* eslint-disable-next-line n/no-extraneous-import -- type is pulled via acorn-walk */\nimport { type Node } from \"acorn\";\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.toString(),\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", "/* istanbul ignore file */\n\nimport * as utils from \"./index\";\n\nexport = { ...utils };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,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;A;;ACZO,SAAS,iBAAiB,UAAoB,MAAsB;AAC1E,MAAI,OAAO,SAAS;AACpB,MAAI,SAAS,SAAS,SAAS;AAC/B,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACrC,QAAI,OAAO,GAAG;AAEb,UAAI,KAAK,CAAC,MAAM,MAAM;AACrB;AAAA,MACD;AAAA,I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,QAAQ;AAAA,MAChC,aAAa;AAAA,MACb,YAAY;AAAA,MACZ,KAAK;AAAA,IACN,CAAC;AACD,WAAO,IAAI,mBAAkB,KAAK,YAAY,UAAU,MAAM;AAAA,EAC/D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBO,sBAAsB,KAAuB;AACnD,UAAM,SAAmB,CAAC;AAC1B,UAAM,EAAE,UAAU,KAAK,IAAI;AAC3B,UAAM,OAAa,KAAK;AACxB,IAAK,YAAO,MAAM;AAAA,MACjB,SAASA,OAAuB;AAC/B,YAAI,WAAWA,MAAK,KAAK,KAAK,QAAQ,GAAG;AACxC,gBAAM,SAAS,eAAeA,MAAK,OAAO,UAAU,IAAI;AACxD,cAAI,QAAQ;AACX,mBAAO,WAAW;AAClB,mBAAO,KAAK,MAAM;AAAA,UACnB;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAA4C;AAC5C,WAAO;AAAA,EACR;AACD;A;;ACpLA,iBAAS,EAAE,GAAG,YAAM;",
3
+ "sources": ["../../src/entry-cjs.ts", "../../src/position-from-offset.ts", "../../src/position-to-offset.ts", "../../src/template-extractor.ts"],
4
+ "sourcesContent": ["/* istanbul ignore file */\n\nexport * from \"./index\";\n", "/**\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\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\nimport * as espree from \"espree\";\nimport * as walk from \"acorn-walk\";\n\n/* eslint-disable-next-line n/no-extraneous-import -- type is pulled via acorn-walk */\nimport { type Node } from \"acorn\";\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.toString(),\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"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,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;A;;ACZO,SAAS,iBAAiB,UAAoB,MAAsB;AAC1E,MAAI,OAAO,SAAS;AACpB,MAAI,SAAS,SAAS,SAAS;AAC/B,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACrC,QAAI,OAAO,GAAG;AAEb,UAAI,KAAK,CAAC,MAAM,MAAM;AACrB;AAAA,MACD;AAAA,IACD,WAAW,SAAS,GAAG;AAEtB;AAAA,IACD,OAAO;AAEN,aAAO;AAAA,IACR;AAAA,EACD;AAGA,QAAM,IAAI,MAAM,iDAAiD;AAClE;A;;AC5BA,aAAwB;AACxB,WAAsB;AAgBtB,SAAS,oBAAoB,OAAyC;AACrE,MAAI,SAAS,MAAM,CAAC,EAAE,QAAQ;AAC9B,MAAI,SAAS;AACb,aAAW,QAAQ,OAAO;AACzB,cAAU,IAAI,OAAO,KAAK,QAAQ,IAAI,MAAM;AAC5C,cAAU,KAAK,MAAM;AACrB,aAAS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K;AACxB,IAAK,YAAO,MAAM;AAAA,MACjB,SAASA,OAAuB;AAC/B,YAAI,WAAWA,MAAK,KAAK,KAAK,QAAQ,GAAG;AACxC,gBAAM,SAAS,eAAeA,MAAK,OAAO,UAAU,IAAI;AACxD,cAAI,QAAQ;AACX,mBAAO,WAAW;AAClB,mBAAO,KAAK,MAAM;AAAA,UACnB;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAA4C;AAC5C,WAAO;AAAA,EACR;AACD;A",
6
6
  "names": ["node"]
7
7
  }
@@ -1 +1,75 @@
1
+ import { Source } from 'html-validate';
2
+
3
+ /**
4
+ * Represents line and column.
5
+ *
6
+ * @public
7
+ * @since 1.0.0
8
+ */
9
+ export declare interface Position {
10
+ /** Line number (First line is 1) */
11
+ line: number;
12
+ /** Column number (first column is 1) */
13
+ column: number;
14
+ }
15
+
16
+ /**
17
+ * Given an offset into a source, calculate the corresponding line and column.
18
+ *
19
+ * @public
20
+ * @since 1.0.0
21
+ */
22
+ export declare function positionFromOffset(text: string, offset: number): [line: number, column: number];
23
+
24
+ /**
25
+ * Compute source offset from line and column and the given markup.
26
+ *
27
+ * @public
28
+ * @since 1.0.0
29
+ * @param position - Line and column.
30
+ * @param data - Source markup.
31
+ * @returns The byte offset into the markup which line and column corresponds to.
32
+ */
33
+ export declare function positionToOffset(position: Position, data: string): number;
34
+
35
+ /**
36
+ * @public
37
+ * @since 1.0.0
38
+ */
39
+ export declare class TemplateExtractor {
40
+ private ast;
41
+ private filename;
42
+ private data;
43
+ private constructor();
44
+ /**
45
+ * Create a new [[TemplateExtractor]] from javascript source code.
46
+ *
47
+ * `Source` offsets will be relative to the string, i.e. offset 0 is the first
48
+ * character of the string. If the string is only a subset of a larger string
49
+ * the offsets must be adjusted manually.
50
+ *
51
+ * @param source - Source code.
52
+ * @param filename - Optional filename to set in the resulting
53
+ * `Source`. Defauls to `"inline"`.
54
+ */
55
+ static fromString(source: string, filename?: string): TemplateExtractor;
56
+ /**
57
+ * Extract object properties.
58
+ *
59
+ * Given a key `"template"` this method finds all objects literals with a
60
+ * `"template"` property and creates a [[Source]] instance with proper offsets
61
+ * with the value of the property. For instance:
62
+ *
63
+ * ```
64
+ * const myObj = {
65
+ * foo: 'bar',
66
+ * };
67
+ * ```
68
+ *
69
+ * The above snippet would yield a `Source` with the content `bar`.
70
+ *
71
+ */
72
+ extractObjectProperty(key: string): Source[];
73
+ }
74
+
1
75
  export { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@html-validate/plugin-utils",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Plugin utilities and helpers for writing plugins to HTML-Validate",
5
5
  "keywords": [
6
6
  "html-validate"