kitcheck 0.0.0-stage → 0.1.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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +179 -2
- package/bin/kitcheck.js +159 -0
- package/package.json +51 -4
- package/src/config.js +101 -0
- package/src/data/elementor.js +208 -0
- package/src/document.js +127 -0
- package/src/index.d.ts +78 -0
- package/src/index.js +167 -0
- package/src/load.js +153 -0
- package/src/locate.js +115 -0
- package/src/report.js +86 -0
- package/src/rules.js +521 -0
- package/src/version.js +26 -0
- package/src/zip.js +76 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// Reference data about Elementor element types and widgets.
|
|
2
|
+
//
|
|
3
|
+
// These lists are maintained by hand. If kitcheck reports a core widget as
|
|
4
|
+
// unknown, or puts a widget in the wrong group, please open an issue or a
|
|
5
|
+
// pull request that edits this file.
|
|
6
|
+
|
|
7
|
+
/** Element types that hold other elements. */
|
|
8
|
+
export const CONTAINER_EL_TYPES = new Set(['section', 'column', 'container']);
|
|
9
|
+
|
|
10
|
+
/** All classic element types. Atomic (Elementor 4) types start with "e-". */
|
|
11
|
+
export const CLASSIC_EL_TYPES = new Set(['section', 'column', 'container', 'widget']);
|
|
12
|
+
|
|
13
|
+
/** Widgets that ship with the free Elementor plugin. */
|
|
14
|
+
export const FREE_WIDGETS = new Set([
|
|
15
|
+
'heading',
|
|
16
|
+
'image',
|
|
17
|
+
'text-editor',
|
|
18
|
+
'video',
|
|
19
|
+
'button',
|
|
20
|
+
'divider',
|
|
21
|
+
'spacer',
|
|
22
|
+
'google_maps',
|
|
23
|
+
'icon',
|
|
24
|
+
'image-box',
|
|
25
|
+
'icon-box',
|
|
26
|
+
'star-rating',
|
|
27
|
+
'image-carousel',
|
|
28
|
+
'image-gallery',
|
|
29
|
+
'icon-list',
|
|
30
|
+
'counter',
|
|
31
|
+
'progress',
|
|
32
|
+
'testimonial',
|
|
33
|
+
'tabs',
|
|
34
|
+
'accordion',
|
|
35
|
+
'toggle',
|
|
36
|
+
'social-icons',
|
|
37
|
+
'alert',
|
|
38
|
+
'audio',
|
|
39
|
+
'shortcode',
|
|
40
|
+
'html',
|
|
41
|
+
'menu-anchor',
|
|
42
|
+
'sidebar',
|
|
43
|
+
'read-more',
|
|
44
|
+
'text-path',
|
|
45
|
+
'nested-tabs',
|
|
46
|
+
'nested-accordion',
|
|
47
|
+
'link-in-bio',
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
/** Widgets that need Elementor Pro. */
|
|
51
|
+
export const PRO_WIDGETS = new Set([
|
|
52
|
+
'posts',
|
|
53
|
+
'portfolio',
|
|
54
|
+
'gallery',
|
|
55
|
+
'form',
|
|
56
|
+
'login',
|
|
57
|
+
'slides',
|
|
58
|
+
'nav-menu',
|
|
59
|
+
'animated-headline',
|
|
60
|
+
'price-list',
|
|
61
|
+
'price-table',
|
|
62
|
+
'flip-box',
|
|
63
|
+
'call-to-action',
|
|
64
|
+
'media-carousel',
|
|
65
|
+
'testimonial-carousel',
|
|
66
|
+
'reviews',
|
|
67
|
+
'table-of-contents',
|
|
68
|
+
'countdown',
|
|
69
|
+
'share-buttons',
|
|
70
|
+
'blockquote',
|
|
71
|
+
'facebook-button',
|
|
72
|
+
'facebook-comments',
|
|
73
|
+
'facebook-embed',
|
|
74
|
+
'facebook-page',
|
|
75
|
+
'template',
|
|
76
|
+
'global',
|
|
77
|
+
'lottie',
|
|
78
|
+
'hotspot',
|
|
79
|
+
'paypal-button',
|
|
80
|
+
'stripe-button',
|
|
81
|
+
'progress-tracker',
|
|
82
|
+
'code-highlight',
|
|
83
|
+
'video-playlist',
|
|
84
|
+
'sitemap',
|
|
85
|
+
'search-form',
|
|
86
|
+
'search',
|
|
87
|
+
'author-box',
|
|
88
|
+
'post-comments',
|
|
89
|
+
'post-navigation',
|
|
90
|
+
'post-info',
|
|
91
|
+
'archive-posts',
|
|
92
|
+
'theme-site-logo',
|
|
93
|
+
'theme-site-title',
|
|
94
|
+
'theme-page-title',
|
|
95
|
+
'theme-post-title',
|
|
96
|
+
'theme-post-excerpt',
|
|
97
|
+
'theme-post-content',
|
|
98
|
+
'theme-post-featured-image',
|
|
99
|
+
'theme-archive-title',
|
|
100
|
+
'loop-grid',
|
|
101
|
+
'loop-carousel',
|
|
102
|
+
'mega-menu',
|
|
103
|
+
'nested-carousel',
|
|
104
|
+
'off-canvas',
|
|
105
|
+
'taxonomy-filter',
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
/** Widget type prefixes that belong to free Elementor. */
|
|
109
|
+
export const FREE_WIDGET_PREFIXES = ['wp-widget-'];
|
|
110
|
+
|
|
111
|
+
/** Widget type prefixes that belong to Elementor Pro. */
|
|
112
|
+
export const PRO_WIDGET_PREFIXES = ['woocommerce-', 'wc-'];
|
|
113
|
+
|
|
114
|
+
/** Widgets that may contain child elements (nested elements). */
|
|
115
|
+
export const NESTED_WIDGETS = new Set(['mega-menu', 'off-canvas']);
|
|
116
|
+
export const NESTED_WIDGET_PREFIXES = ['nested-'];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Common addon plugins, matched by widget type prefix. Used only to make
|
|
120
|
+
* messages more helpful; any widget not in the lists above is reported as
|
|
121
|
+
* an addon widget whether or not its prefix is listed here.
|
|
122
|
+
*/
|
|
123
|
+
export const ADDON_PREFIXES = [
|
|
124
|
+
['eael-', 'Essential Addons for Elementor'],
|
|
125
|
+
['tp-', 'The Plus Addons for Elementor'],
|
|
126
|
+
['premium-', 'Premium Addons for Elementor'],
|
|
127
|
+
['uael-', 'Ultimate Addons for Elementor'],
|
|
128
|
+
['ha-', 'Happy Addons'],
|
|
129
|
+
['jet-', 'a JetPlugins (Crocoblock) plugin'],
|
|
130
|
+
['elementskit-', 'ElementsKit'],
|
|
131
|
+
['ekit-', 'ElementsKit'],
|
|
132
|
+
['wpr-', 'Royal Elementor Addons'],
|
|
133
|
+
['pp-', 'PowerPack for Elementor'],
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
/** Template types Elementor uses in library exports. */
|
|
137
|
+
export const TEMPLATE_TYPES = new Set([
|
|
138
|
+
'page',
|
|
139
|
+
'section',
|
|
140
|
+
'container',
|
|
141
|
+
'header',
|
|
142
|
+
'footer',
|
|
143
|
+
'single',
|
|
144
|
+
'single-post',
|
|
145
|
+
'single-page',
|
|
146
|
+
'archive',
|
|
147
|
+
'search-results',
|
|
148
|
+
'error-404',
|
|
149
|
+
'popup',
|
|
150
|
+
'loop-item',
|
|
151
|
+
'product',
|
|
152
|
+
'product-archive',
|
|
153
|
+
'widget',
|
|
154
|
+
'kit',
|
|
155
|
+
'landing-page',
|
|
156
|
+
'wp-page',
|
|
157
|
+
'wp-post',
|
|
158
|
+
]);
|
|
159
|
+
|
|
160
|
+
/** Template types that are Elementor Pro features. */
|
|
161
|
+
export const PRO_TEMPLATE_TYPES = new Set([
|
|
162
|
+
'header',
|
|
163
|
+
'footer',
|
|
164
|
+
'single',
|
|
165
|
+
'single-post',
|
|
166
|
+
'single-page',
|
|
167
|
+
'archive',
|
|
168
|
+
'search-results',
|
|
169
|
+
'error-404',
|
|
170
|
+
'popup',
|
|
171
|
+
'loop-item',
|
|
172
|
+
'product',
|
|
173
|
+
'product-archive',
|
|
174
|
+
'widget',
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Minimum Elementor version needed by some features. Kept deliberately
|
|
179
|
+
* short: only features whose first version is well established.
|
|
180
|
+
*/
|
|
181
|
+
export const MIN_VERSIONS = {
|
|
182
|
+
container: '3.6.0',
|
|
183
|
+
atomic: '4.0.0',
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export function isAtomicType(type) {
|
|
187
|
+
return typeof type === 'string' && type.startsWith('e-');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function classifyWidget(widgetType) {
|
|
191
|
+
if (isAtomicType(widgetType)) return 'core';
|
|
192
|
+
if (FREE_WIDGETS.has(widgetType)) return 'core';
|
|
193
|
+
if (FREE_WIDGET_PREFIXES.some((p) => widgetType.startsWith(p))) return 'core';
|
|
194
|
+
if (PRO_WIDGETS.has(widgetType)) return 'pro';
|
|
195
|
+
if (PRO_WIDGET_PREFIXES.some((p) => widgetType.startsWith(p))) return 'pro';
|
|
196
|
+
return 'addon';
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function guessAddon(widgetType) {
|
|
200
|
+
const hit = ADDON_PREFIXES.find(([prefix]) => widgetType.startsWith(prefix));
|
|
201
|
+
return hit ? hit[1] : null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function canHaveChildren(widgetType) {
|
|
205
|
+
if (isAtomicType(widgetType)) return false;
|
|
206
|
+
if (NESTED_WIDGETS.has(widgetType)) return true;
|
|
207
|
+
return NESTED_WIDGET_PREFIXES.some((p) => widgetType.startsWith(p));
|
|
208
|
+
}
|
package/src/document.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Parses a JSON file and works out what kind of Elementor file it is.
|
|
2
|
+
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
classifyWidget,
|
|
6
|
+
guessAddon,
|
|
7
|
+
isAtomicType,
|
|
8
|
+
PRO_TEMPLATE_TYPES,
|
|
9
|
+
} from './data/elementor.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Formats kitcheck understands:
|
|
13
|
+
* - template: a template exported from Elementor's library ({ content: [...] })
|
|
14
|
+
* - clipboard: elements copied with Elementor's copy/paste ({ type: "elementor", elements: [...] })
|
|
15
|
+
* - element-data: the raw _elementor_data post meta (an array of elements)
|
|
16
|
+
* - kit-manifest: manifest.json from a template kit
|
|
17
|
+
* - other: any other JSON file (site settings, package.json and so on)
|
|
18
|
+
*/
|
|
19
|
+
export function detectFormat(data, rel) {
|
|
20
|
+
if (isObject(data) && Array.isArray(data.content)) return 'template';
|
|
21
|
+
if (isObject(data) && data.type === 'elementor' && Array.isArray(data.elements)) return 'clipboard';
|
|
22
|
+
if (Array.isArray(data) && data.length > 0 && data.every((item) => isObject(item) && 'elType' in item)) {
|
|
23
|
+
return 'element-data';
|
|
24
|
+
}
|
|
25
|
+
if (isObject(data) && path.posix.basename(rel).toLowerCase() === 'manifest.json') return 'kit-manifest';
|
|
26
|
+
return 'other';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function rootsOf(doc) {
|
|
30
|
+
switch (doc.format) {
|
|
31
|
+
case 'template':
|
|
32
|
+
return { elements: doc.data.content, pointer: '/content' };
|
|
33
|
+
case 'clipboard':
|
|
34
|
+
return { elements: doc.data.elements, pointer: '/elements' };
|
|
35
|
+
case 'element-data':
|
|
36
|
+
return { elements: doc.data, pointer: '' };
|
|
37
|
+
default:
|
|
38
|
+
return { elements: [], pointer: '' };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Visit every element depth first. Malformed values are passed to the
|
|
44
|
+
* visitor too, so rules can report them.
|
|
45
|
+
*/
|
|
46
|
+
export function walk(doc, visit) {
|
|
47
|
+
const { elements, pointer } = rootsOf(doc);
|
|
48
|
+
const visitList = (list, base, parent, depth) => {
|
|
49
|
+
list.forEach((el, index) => {
|
|
50
|
+
const ptr = `${base}/${index}`;
|
|
51
|
+
visit(el, { pointer: ptr, parent, depth, index });
|
|
52
|
+
if (isObject(el) && Array.isArray(el.elements)) {
|
|
53
|
+
visitList(el.elements, `${ptr}/elements`, el, depth + 1);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
visitList(elements, pointer, null, 0);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Collect what a document depends on. Used by the dependency, version and
|
|
62
|
+
* kit rules, and included in JSON output.
|
|
63
|
+
*/
|
|
64
|
+
export function analyze(doc) {
|
|
65
|
+
const usage = {
|
|
66
|
+
elements: 0,
|
|
67
|
+
widgets: new Map(), // widgetType -> count
|
|
68
|
+
pro: [], // { pointer, reason }
|
|
69
|
+
addons: new Map(), // widgetType -> { count, plugin, pointer }
|
|
70
|
+
firstContainer: null,
|
|
71
|
+
firstAtomic: null,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
if (doc.format === 'template' && PRO_TEMPLATE_TYPES.has(doc.data.type)) {
|
|
75
|
+
usage.pro.push({ pointer: '/type', reason: `the "${doc.data.type}" template type (Theme Builder)` });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
walk(doc, (el, info) => {
|
|
79
|
+
if (!isObject(el)) return;
|
|
80
|
+
usage.elements++;
|
|
81
|
+
if (el.elType === 'container' && !usage.firstContainer) usage.firstContainer = info.pointer;
|
|
82
|
+
if ((isAtomicType(el.elType) || isAtomicType(el.widgetType)) && !usage.firstAtomic) {
|
|
83
|
+
usage.firstAtomic = info.pointer;
|
|
84
|
+
}
|
|
85
|
+
if (el.elType === 'widget' && typeof el.widgetType === 'string') {
|
|
86
|
+
const type = el.widgetType;
|
|
87
|
+
usage.widgets.set(type, (usage.widgets.get(type) ?? 0) + 1);
|
|
88
|
+
const kind = classifyWidget(type);
|
|
89
|
+
if (kind === 'pro') usage.pro.push({ pointer: info.pointer, reason: `the "${type}" widget`, element: el });
|
|
90
|
+
if (kind === 'addon') {
|
|
91
|
+
const entry = usage.addons.get(type) ?? { count: 0, plugin: guessAddon(type), pointer: info.pointer };
|
|
92
|
+
entry.count++;
|
|
93
|
+
usage.addons.set(type, entry);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (const reason of proSettings(el.settings)) {
|
|
97
|
+
usage.pro.push({ pointer: info.pointer, reason, element: el });
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return usage;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function proSettings(settings) {
|
|
104
|
+
const reasons = [];
|
|
105
|
+
if (!isObject(settings)) return reasons;
|
|
106
|
+
if (isObject(settings.__dynamic__) && Object.keys(settings.__dynamic__).length > 0) {
|
|
107
|
+
reasons.push(`dynamic tags (${Object.keys(settings.__dynamic__).join(', ')})`);
|
|
108
|
+
}
|
|
109
|
+
if (typeof settings.sticky === 'string' && settings.sticky !== '') {
|
|
110
|
+
reasons.push('sticky positioning');
|
|
111
|
+
}
|
|
112
|
+
if (settings.motion_fx_motion_fx_scrolling === 'yes' || settings.motion_fx_motion_fx_mouse === 'yes') {
|
|
113
|
+
reasons.push('motion effects');
|
|
114
|
+
}
|
|
115
|
+
return reasons;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function isObject(value) {
|
|
119
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Short label for an element, such as "heading abc1234". */
|
|
123
|
+
export function labelOf(el) {
|
|
124
|
+
if (!isObject(el)) return '';
|
|
125
|
+
const type = el.elType === 'widget' ? el.widgetType : el.elType;
|
|
126
|
+
return [type, el.id].filter((v) => typeof v === 'string' && v !== '').join(' ');
|
|
127
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export type Severity = 'off' | 'info' | 'warning' | 'error';
|
|
2
|
+
|
|
3
|
+
export interface Options {
|
|
4
|
+
/** The site has Elementor Pro. Turns off the deps/pro rule. */
|
|
5
|
+
pro?: boolean;
|
|
6
|
+
/** Oldest Elementor version the files must work with, such as "3.21". */
|
|
7
|
+
target?: string | null;
|
|
8
|
+
/** Hosts media may load from without a warning. Subdomains are included. */
|
|
9
|
+
allowHosts?: string[];
|
|
10
|
+
/** Widget types you know are installed, so deps/addon skips them. */
|
|
11
|
+
knownWidgets?: string[];
|
|
12
|
+
/** Widget type prefixes you know are installed, such as "acme-". */
|
|
13
|
+
knownWidgetPrefixes?: string[];
|
|
14
|
+
/** Change rule severities, for example { "assets/insecure": "off" }. */
|
|
15
|
+
rules?: Record<string, Severity | 'warn'>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface LintOptions extends Options {
|
|
19
|
+
/** Folder that relative paths are resolved from. Defaults to process.cwd(). */
|
|
20
|
+
cwd?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LintTextOptions extends Options {
|
|
24
|
+
/** Name to show in reports. Defaults to "input.json". */
|
|
25
|
+
filename?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Finding {
|
|
29
|
+
ruleId: string;
|
|
30
|
+
severity: Exclude<Severity, 'off'>;
|
|
31
|
+
message: string;
|
|
32
|
+
/** JSON pointer to the problem, such as "/content/0/elements/1". */
|
|
33
|
+
pointer: string;
|
|
34
|
+
/** Short label for the element, such as "heading 2b8d0f4". */
|
|
35
|
+
element?: string;
|
|
36
|
+
line?: number;
|
|
37
|
+
column?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface FileResult {
|
|
41
|
+
/** File name as shown in reports. Files inside a ZIP look like "kit.zip:templates/home.json". */
|
|
42
|
+
file: string;
|
|
43
|
+
format: 'template' | 'clipboard' | 'element-data' | 'kit-manifest' | 'other' | null;
|
|
44
|
+
findings: Finding[];
|
|
45
|
+
stats: {
|
|
46
|
+
elements: number;
|
|
47
|
+
widgets: Record<string, number>;
|
|
48
|
+
needsPro: boolean;
|
|
49
|
+
addonWidgets: string[];
|
|
50
|
+
} | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface LintResult {
|
|
54
|
+
files: FileResult[];
|
|
55
|
+
errorCount: number;
|
|
56
|
+
warningCount: number;
|
|
57
|
+
infoCount: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface RuleInfo {
|
|
61
|
+
id: string;
|
|
62
|
+
severity: Exclude<Severity, 'off'>;
|
|
63
|
+
description: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Check .json files, folders and .zip archives. Use "-" to read standard input. */
|
|
67
|
+
export function lint(inputs: string[], options?: LintOptions): Promise<LintResult>;
|
|
68
|
+
|
|
69
|
+
/** Check a single JSON string. */
|
|
70
|
+
export function lintText(text: string, options?: LintTextOptions): LintResult;
|
|
71
|
+
|
|
72
|
+
/** Every rule kitcheck knows, with its default severity. */
|
|
73
|
+
export const rules: readonly RuleInfo[];
|
|
74
|
+
|
|
75
|
+
/** Find .kitcheckrc.json, .kitcheckrc, kitcheck.config.json or a "kitcheck" key in package.json. */
|
|
76
|
+
export function findConfig(cwd: string): Promise<{ path: string; config: Options } | null>;
|
|
77
|
+
|
|
78
|
+
export class UsageError extends Error {}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// kitcheck: checks Elementor templates, copied elements and template kits
|
|
2
|
+
// for problems before they are imported.
|
|
3
|
+
|
|
4
|
+
import { analyze, detectFormat, labelOf, walk } from './document.js';
|
|
5
|
+
import { loadInputs } from './load.js';
|
|
6
|
+
import { buildLocator, offsetToPosition } from './locate.js';
|
|
7
|
+
import { allRules, rules } from './rules.js';
|
|
8
|
+
import { resolveOptions } from './config.js';
|
|
9
|
+
|
|
10
|
+
export { allRules as rules } from './rules.js';
|
|
11
|
+
export { findConfig, resolveOptions, UsageError } from './config.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check files, folders and ZIP archives.
|
|
15
|
+
* @param {string[]} inputs Paths to .json files, folders or .zip files ("-" reads stdin).
|
|
16
|
+
* @param {object} [options] See README: pro, target, allowHosts, knownWidgets, knownWidgetPrefixes, rules, cwd.
|
|
17
|
+
*/
|
|
18
|
+
export async function lint(inputs, options = {}) {
|
|
19
|
+
const resolved = options.severities ? options : resolveOptions(options);
|
|
20
|
+
const projects = await loadInputs(inputs, { cwd: options.cwd, stdin: options.stdin });
|
|
21
|
+
return lintProjects(projects, resolved);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check one JSON string. Handy for editors, build scripts and tests.
|
|
26
|
+
* @param {string} text
|
|
27
|
+
* @param {object} [options] Same as lint(), plus `filename`.
|
|
28
|
+
*/
|
|
29
|
+
export function lintText(text, options = {}) {
|
|
30
|
+
const resolved = options.severities ? options : resolveOptions(options);
|
|
31
|
+
const display = options.filename ?? 'input.json';
|
|
32
|
+
const project = {
|
|
33
|
+
display,
|
|
34
|
+
kind: 'file',
|
|
35
|
+
files: [{ display, rel: display, text, explicit: true }],
|
|
36
|
+
paths: new Set([display]),
|
|
37
|
+
errors: [],
|
|
38
|
+
};
|
|
39
|
+
return lintProjects([project], resolved);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function lintProjects(projects, options) {
|
|
43
|
+
const files = [];
|
|
44
|
+
const queue = [...projects];
|
|
45
|
+
while (queue.length > 0) {
|
|
46
|
+
const project = queue.shift();
|
|
47
|
+
files.push(...lintProject(project, options));
|
|
48
|
+
if (project.children) queue.push(...project.children);
|
|
49
|
+
}
|
|
50
|
+
const count = (sev) => files.reduce((n, f) => n + f.findings.filter((x) => x.severity === sev).length, 0);
|
|
51
|
+
return {
|
|
52
|
+
files,
|
|
53
|
+
errorCount: count('error'),
|
|
54
|
+
warningCount: count('warning'),
|
|
55
|
+
infoCount: count('info'),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function lintProject(project, options) {
|
|
60
|
+
const results = new Map();
|
|
61
|
+
const entryFor = (display, format = null) => {
|
|
62
|
+
if (!results.has(display)) results.set(display, { file: display, format, findings: [], stats: null });
|
|
63
|
+
return results.get(display);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const severityOf = (rule) => options.severities[rule.id] ?? rule.severity;
|
|
67
|
+
const engineRule = (id) => allRules.find((r) => r.id === id);
|
|
68
|
+
const push = (entry, rule, finding, locate) => {
|
|
69
|
+
const severity = severityOf(rule);
|
|
70
|
+
if (severity === 'off') return;
|
|
71
|
+
const pos = finding.line ? { line: finding.line, column: finding.column } : locate?.(finding.pointer) ?? null;
|
|
72
|
+
entry.findings.push({
|
|
73
|
+
ruleId: rule.id,
|
|
74
|
+
severity,
|
|
75
|
+
message: finding.message,
|
|
76
|
+
pointer: finding.pointer ?? '',
|
|
77
|
+
element: finding.element ? labelOf(finding.element) : undefined,
|
|
78
|
+
line: pos?.line,
|
|
79
|
+
column: pos?.column,
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
for (const message of project.errors) {
|
|
84
|
+
push(entryFor(project.display), engineRule('input/unreadable'), { message });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const docs = [];
|
|
88
|
+
for (const file of project.files) {
|
|
89
|
+
let data;
|
|
90
|
+
try {
|
|
91
|
+
data = JSON.parse(file.text);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
const where = parseErrorPosition(err, file.text);
|
|
94
|
+
push(entryFor(file.display), engineRule('json/invalid'), {
|
|
95
|
+
message: `Not valid JSON: ${cleanJsonError(err.message)}`,
|
|
96
|
+
...where,
|
|
97
|
+
});
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const format = detectFormat(data, file.rel);
|
|
101
|
+
if (format === 'other') {
|
|
102
|
+
if (file.explicit) {
|
|
103
|
+
push(entryFor(file.display, format), engineRule('format/unknown'), {
|
|
104
|
+
message:
|
|
105
|
+
'This is not an Elementor template, copied elements, element data or a kit manifest. Expected a "content" array, a "type": "elementor" clipboard object, or an array of elements.',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const doc = { file, data, format, state: {}, locate: null };
|
|
111
|
+
doc.usage = analyze(doc);
|
|
112
|
+
docs.push(doc);
|
|
113
|
+
entryFor(file.display, format).stats = statsOf(doc);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const doc of docs) {
|
|
117
|
+
const entry = entryFor(doc.file.display, doc.format);
|
|
118
|
+
const locate = (pointer) => (doc.locate ??= buildLocator(doc.file.text))(pointer);
|
|
119
|
+
for (const rule of rules) {
|
|
120
|
+
if (severityOf(rule) === 'off') continue;
|
|
121
|
+
const ctx = { options, locate, report: (f) => push(entry, rule, f, locate) };
|
|
122
|
+
if (rule.document) rule.document(doc, ctx);
|
|
123
|
+
if (rule.element) walk(doc, (el, info) => rule.element(el, info, doc, ctx));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const rule of rules) {
|
|
128
|
+
if (!rule.project || severityOf(rule) === 'off') continue;
|
|
129
|
+
const ctx = {
|
|
130
|
+
options,
|
|
131
|
+
report: (f) => {
|
|
132
|
+
const doc = docs.find((d) => d.file.display === f.file);
|
|
133
|
+
const locate = doc ? (p) => (doc.locate ??= buildLocator(doc.file.text))(p) : null;
|
|
134
|
+
push(entryFor(f.file ?? project.display), rule, f, locate);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
rule.project(project, docs, ctx);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const order = new Map(allRules.map((r, i) => [r.id, i]));
|
|
141
|
+
for (const entry of results.values()) {
|
|
142
|
+
entry.findings.sort(
|
|
143
|
+
(a, b) => (a.line ?? 0) - (b.line ?? 0) || (a.column ?? 0) - (b.column ?? 0) || order.get(a.ruleId) - order.get(b.ruleId),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
return [...results.values()];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function statsOf(doc) {
|
|
150
|
+
return {
|
|
151
|
+
elements: doc.usage.elements,
|
|
152
|
+
widgets: Object.fromEntries(doc.usage.widgets),
|
|
153
|
+
needsPro: doc.usage.pro.length > 0,
|
|
154
|
+
addonWidgets: [...doc.usage.addons.keys()],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function parseErrorPosition(err, text) {
|
|
159
|
+
const match = /position (\d+)/.exec(err.message);
|
|
160
|
+
if (match) return offsetToPosition(text, Number(match[1]));
|
|
161
|
+
if (text.trim() === '') return { line: 1, column: 1 };
|
|
162
|
+
return {};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function cleanJsonError(message) {
|
|
166
|
+
return message.replace(/\s*\(line \d+ column \d+\)/, '').replace(/ in JSON at position \d+/, '');
|
|
167
|
+
}
|