@html-validate/plugin-utils 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 David Sveningsson <ext@sidvind.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @html-validate/plugin-utils`
2
+
3
+ Plugin utilities and helpers for writing plugins to HTML-Validate
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ TemplateExtractor: () => TemplateExtractor,
34
+ positionFromOffset: () => positionFromOffset,
35
+ positionToOffset: () => positionToOffset
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
38
+
39
+ // src/position-from-offset.ts
40
+ function positionFromOffset(text, offset) {
41
+ let line = 1;
42
+ let prev = 0;
43
+ let pos = text.indexOf("\n");
44
+ while (pos !== -1) {
45
+ if (pos >= offset) {
46
+ return [line, offset - prev + 1];
47
+ }
48
+ line++;
49
+ prev = pos + 1;
50
+ pos = text.indexOf("\n", pos + 1);
51
+ }
52
+ return [line, offset - prev + 1];
53
+ }
54
+
55
+ // src/position-to-offset.ts
56
+ function positionToOffset(position, data) {
57
+ let line = position.line;
58
+ let column = position.column + 1;
59
+ for (let i = 0; i < data.length; i++) {
60
+ if (line > 1) {
61
+ if (data[i] === "\n") {
62
+ line--;
63
+ }
64
+ } else if (column > 1) {
65
+ column--;
66
+ } else {
67
+ return i;
68
+ }
69
+ }
70
+ throw new Error("Failed to compute location offset from position");
71
+ }
72
+
73
+ // src/template-extractor.ts
74
+ var espree = __toESM(require("espree"));
75
+ var walk = __toESM(require("acorn-walk"));
76
+ function joinTemplateLiteral(nodes) {
77
+ let offset = nodes[0].start + 1;
78
+ let output = "";
79
+ for (const node of nodes) {
80
+ output += " ".repeat(node.start + 1 - offset);
81
+ output += node.value.raw;
82
+ offset = node.end - 2;
83
+ }
84
+ return output;
85
+ }
86
+ function extractLiteral(node, filename, data) {
87
+ switch (node.type) {
88
+ case "FunctionExpression":
89
+ case "Identifier":
90
+ return null;
91
+ case "Literal":
92
+ if (typeof node.value !== "string") {
93
+ return null;
94
+ }
95
+ return {
96
+ data: node.value.toString(),
97
+ filename,
98
+ line: node.loc.start.line,
99
+ column: node.loc.start.column + 1,
100
+ offset: positionToOffset(node.loc.start, data) + 1
101
+ };
102
+ case "TemplateLiteral":
103
+ return {
104
+ data: joinTemplateLiteral(node.quasis),
105
+ filename,
106
+ line: node.loc.start.line,
107
+ column: node.loc.start.column + 1,
108
+ offset: positionToOffset(node.loc.start, data) + 1
109
+ };
110
+ case "TaggedTemplateExpression":
111
+ return {
112
+ data: joinTemplateLiteral(node.quasi.quasis),
113
+ filename,
114
+ line: node.quasi.loc.start.line,
115
+ column: node.quasi.loc.start.column + 1,
116
+ offset: positionToOffset(node.quasi.loc.start, data) + 1
117
+ };
118
+ case "ArrowFunctionExpression": {
119
+ const whitelist = ["Literal", "TemplateLiteral"];
120
+ if (whitelist.includes(node.body.type)) {
121
+ return extractLiteral(node.body, filename, data);
122
+ } else {
123
+ return null;
124
+ }
125
+ }
126
+ default: {
127
+ const loc = node.loc.start;
128
+ const context = `${filename}:${loc.line}:${loc.column}`;
129
+ throw new Error(`Unhandled node type "${node.type}" at "${context}" in extractLiteral`);
130
+ }
131
+ }
132
+ }
133
+ function compareKey(node, key, filename) {
134
+ switch (node.type) {
135
+ case "Identifier":
136
+ return node.name === key;
137
+ case "Literal":
138
+ return node.value === key;
139
+ default: {
140
+ const loc = node.loc.start;
141
+ const context = `${filename}:${loc.line}:${loc.column}`;
142
+ throw new Error(`Unhandled node type "${node.type}" at "${context}" in compareKey`);
143
+ }
144
+ }
145
+ }
146
+ var TemplateExtractor = class {
147
+ constructor(ast, filename, data) {
148
+ this.ast = ast;
149
+ this.filename = filename;
150
+ this.data = data;
151
+ }
152
+ /**
153
+ * Create a new [[TemplateExtractor]] from javascript source code.
154
+ *
155
+ * `Source` offsets will be relative to the string, i.e. offset 0 is the first
156
+ * character of the string. If the string is only a subset of a larger string
157
+ * the offsets must be adjusted manually.
158
+ *
159
+ * @param source - Source code.
160
+ * @param filename - Optional filename to set in the resulting
161
+ * `Source`. Defauls to `"inline"`.
162
+ */
163
+ static fromString(source, filename) {
164
+ const ast = espree.parse(source, {
165
+ ecmaVersion: "latest",
166
+ sourceType: "module",
167
+ loc: true
168
+ });
169
+ return new TemplateExtractor(ast, filename || "inline", source);
170
+ }
171
+ /**
172
+ * Extract object properties.
173
+ *
174
+ * Given a key `"template"` this method finds all objects literals with a
175
+ * `"template"` property and creates a [[Source]] instance with proper offsets
176
+ * with the value of the property. For instance:
177
+ *
178
+ * ```
179
+ * const myObj = {
180
+ * foo: 'bar',
181
+ * };
182
+ * ```
183
+ *
184
+ * The above snippet would yield a `Source` with the content `bar`.
185
+ *
186
+ */
187
+ extractObjectProperty(key) {
188
+ const result = [];
189
+ const { filename, data } = this;
190
+ const node = this.ast;
191
+ walk.simple(node, {
192
+ Property(node2) {
193
+ if (compareKey(node2.key, key, filename)) {
194
+ const source = extractLiteral(node2.value, filename, data);
195
+ if (source) {
196
+ source.filename = filename;
197
+ result.push(source);
198
+ }
199
+ }
200
+ }
201
+ });
202
+ return result;
203
+ }
204
+ };
205
+ // Annotate the CommonJS export names for ESM import in node:
206
+ 0 && (module.exports = {
207
+ TemplateExtractor,
208
+ positionFromOffset,
209
+ positionToOffset
210
+ });
@@ -0,0 +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
+
75
+ export { }
@@ -0,0 +1,171 @@
1
+ // src/position-from-offset.ts
2
+ function positionFromOffset(text, offset) {
3
+ let line = 1;
4
+ let prev = 0;
5
+ let pos = text.indexOf("\n");
6
+ while (pos !== -1) {
7
+ if (pos >= offset) {
8
+ return [line, offset - prev + 1];
9
+ }
10
+ line++;
11
+ prev = pos + 1;
12
+ pos = text.indexOf("\n", pos + 1);
13
+ }
14
+ return [line, offset - prev + 1];
15
+ }
16
+
17
+ // src/position-to-offset.ts
18
+ function positionToOffset(position, data) {
19
+ let line = position.line;
20
+ let column = position.column + 1;
21
+ for (let i = 0; i < data.length; i++) {
22
+ if (line > 1) {
23
+ if (data[i] === "\n") {
24
+ line--;
25
+ }
26
+ } else if (column > 1) {
27
+ column--;
28
+ } else {
29
+ return i;
30
+ }
31
+ }
32
+ throw new Error("Failed to compute location offset from position");
33
+ }
34
+
35
+ // src/template-extractor.ts
36
+ import * as espree from "espree";
37
+ import * as walk from "acorn-walk";
38
+ function joinTemplateLiteral(nodes) {
39
+ let offset = nodes[0].start + 1;
40
+ let output = "";
41
+ for (const node of nodes) {
42
+ output += " ".repeat(node.start + 1 - offset);
43
+ output += node.value.raw;
44
+ offset = node.end - 2;
45
+ }
46
+ return output;
47
+ }
48
+ function extractLiteral(node, filename, data) {
49
+ switch (node.type) {
50
+ case "FunctionExpression":
51
+ case "Identifier":
52
+ return null;
53
+ case "Literal":
54
+ if (typeof node.value !== "string") {
55
+ return null;
56
+ }
57
+ return {
58
+ data: node.value.toString(),
59
+ filename,
60
+ line: node.loc.start.line,
61
+ column: node.loc.start.column + 1,
62
+ offset: positionToOffset(node.loc.start, data) + 1
63
+ };
64
+ case "TemplateLiteral":
65
+ return {
66
+ data: joinTemplateLiteral(node.quasis),
67
+ filename,
68
+ line: node.loc.start.line,
69
+ column: node.loc.start.column + 1,
70
+ offset: positionToOffset(node.loc.start, data) + 1
71
+ };
72
+ case "TaggedTemplateExpression":
73
+ return {
74
+ data: joinTemplateLiteral(node.quasi.quasis),
75
+ filename,
76
+ line: node.quasi.loc.start.line,
77
+ column: node.quasi.loc.start.column + 1,
78
+ offset: positionToOffset(node.quasi.loc.start, data) + 1
79
+ };
80
+ case "ArrowFunctionExpression": {
81
+ const whitelist = ["Literal", "TemplateLiteral"];
82
+ if (whitelist.includes(node.body.type)) {
83
+ return extractLiteral(node.body, filename, data);
84
+ } else {
85
+ return null;
86
+ }
87
+ }
88
+ default: {
89
+ const loc = node.loc.start;
90
+ const context = `${filename}:${loc.line}:${loc.column}`;
91
+ throw new Error(`Unhandled node type "${node.type}" at "${context}" in extractLiteral`);
92
+ }
93
+ }
94
+ }
95
+ function compareKey(node, key, filename) {
96
+ switch (node.type) {
97
+ case "Identifier":
98
+ return node.name === key;
99
+ case "Literal":
100
+ return node.value === key;
101
+ default: {
102
+ const loc = node.loc.start;
103
+ const context = `${filename}:${loc.line}:${loc.column}`;
104
+ throw new Error(`Unhandled node type "${node.type}" at "${context}" in compareKey`);
105
+ }
106
+ }
107
+ }
108
+ var TemplateExtractor = class {
109
+ constructor(ast, filename, data) {
110
+ this.ast = ast;
111
+ this.filename = filename;
112
+ this.data = data;
113
+ }
114
+ /**
115
+ * Create a new [[TemplateExtractor]] from javascript source code.
116
+ *
117
+ * `Source` offsets will be relative to the string, i.e. offset 0 is the first
118
+ * character of the string. If the string is only a subset of a larger string
119
+ * the offsets must be adjusted manually.
120
+ *
121
+ * @param source - Source code.
122
+ * @param filename - Optional filename to set in the resulting
123
+ * `Source`. Defauls to `"inline"`.
124
+ */
125
+ static fromString(source, filename) {
126
+ const ast = espree.parse(source, {
127
+ ecmaVersion: "latest",
128
+ sourceType: "module",
129
+ loc: true
130
+ });
131
+ return new TemplateExtractor(ast, filename || "inline", source);
132
+ }
133
+ /**
134
+ * Extract object properties.
135
+ *
136
+ * Given a key `"template"` this method finds all objects literals with a
137
+ * `"template"` property and creates a [[Source]] instance with proper offsets
138
+ * with the value of the property. For instance:
139
+ *
140
+ * ```
141
+ * const myObj = {
142
+ * foo: 'bar',
143
+ * };
144
+ * ```
145
+ *
146
+ * The above snippet would yield a `Source` with the content `bar`.
147
+ *
148
+ */
149
+ extractObjectProperty(key) {
150
+ const result = [];
151
+ const { filename, data } = this;
152
+ const node = this.ast;
153
+ walk.simple(node, {
154
+ Property(node2) {
155
+ if (compareKey(node2.key, key, filename)) {
156
+ const source = extractLiteral(node2.value, filename, data);
157
+ if (source) {
158
+ source.filename = filename;
159
+ result.push(source);
160
+ }
161
+ }
162
+ }
163
+ });
164
+ return result;
165
+ }
166
+ };
167
+ export {
168
+ TemplateExtractor,
169
+ positionFromOffset,
170
+ positionToOffset
171
+ };
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.34.9"
9
+ }
10
+ ]
11
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@html-validate/plugin-utils",
3
+ "version": "1.0.0",
4
+ "description": "Plugin utilities and helpers for writing plugins to HTML-Validate",
5
+ "keywords": [
6
+ "html-validate"
7
+ ],
8
+ "homepage": "https://html-validate.org",
9
+ "bugs": {
10
+ "url": "https://gitlab.com/html-validate/plugin-utils/issues/new"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://gitlab.com/html-validate/plugin-utils.git"
15
+ },
16
+ "license": "MIT",
17
+ "author": "David Sveningsson <ext@sidvind.com>",
18
+ "sideEffects": false,
19
+ "type": "commonjs",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "require": "./dist/index.cjs.js",
24
+ "import": "./dist/index.esm.js"
25
+ }
26
+ },
27
+ "main": "dist/index.cjs.js",
28
+ "module": "dist/index.esm.js",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "peerDependencies": {
33
+ "acorn-walk": "^8",
34
+ "espree": "^9",
35
+ "html-validate": "^5 || ^6 || ^7"
36
+ },
37
+ "engines": {
38
+ "node": ">= 16.0"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ }
43
+ }