@sixbell-telco/eslint-plugin-sdk 0.0.0 → 0.1.1
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 +29 -0
- package/README.md +249 -2
- package/package.json +26 -4
- package/src/index.mjs +149 -0
- package/src/manifest.mjs +216 -0
- package/src/rules/app-rules.mjs +353 -0
- package/src/rules/app-support.mjs +131 -0
- package/src/rules/composition-rules.mjs +373 -0
- package/src/rules/import-rules.mjs +42 -0
- package/src/rules/style-rules.mjs +294 -0
- package/src/rules/template-rules.mjs +461 -0
- package/src/types.d.ts +122 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared parts of the app-convention rules ported from the MFE validators: each finding keeps the
|
|
3
|
+
* validator's rule id as its `messageId` and as the `[ID]` prefix of its message, and the rules
|
|
4
|
+
* that read app files (translation.json, themes.json, federation.config.js) resolve them from the
|
|
5
|
+
* app root (`settings.sixbellSdk.appRoot`, else ESLint's cwd).
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
8
|
+
import { dirname, join, relative, sep } from 'node:path';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {import('eslint').Rule.RuleContext} RuleContext
|
|
12
|
+
* @typedef {import('eslint').Rule.RuleModule} RuleModule
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** @param {string} name */
|
|
16
|
+
const docs = (name) => `https://github.com/EnghouseGlobal/nw-sx-web-libs-ui-kit/blob/develop/projects/eslint-plugin-sdk/README.md#${name}`;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A rule whose messages are keyed by the original rule id; each message starts with `[ID]`.
|
|
20
|
+
* @param {{ name: string, type?: 'problem' | 'suggestion', description: string, messages: Record<string, string>, create: (context: RuleContext) => import('eslint').Rule.RuleListener }} definition
|
|
21
|
+
* @returns {RuleModule}
|
|
22
|
+
*/
|
|
23
|
+
export function idRule({ name, type = 'problem', description, messages, create }) {
|
|
24
|
+
return {
|
|
25
|
+
meta: {
|
|
26
|
+
type,
|
|
27
|
+
docs: { description, url: docs(name) },
|
|
28
|
+
schema: [],
|
|
29
|
+
messages: Object.fromEntries(Object.entries(messages).map(([id, text]) => [id, `[${id}] ${text}`])),
|
|
30
|
+
},
|
|
31
|
+
create,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The app root: `settings.sixbellSdk.appRoot`, else ESLint's cwd.
|
|
37
|
+
* @param {RuleContext} context
|
|
38
|
+
*/
|
|
39
|
+
export function appRoot(context) {
|
|
40
|
+
const settings = /** @type {{ sixbellSdk?: { appRoot?: string } }} */ (context.settings);
|
|
41
|
+
return settings.sixbellSdk?.appRoot ?? context.cwd;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The physical file's path relative to the app root, with `/` separators (an inline template
|
|
46
|
+
* resolves to its component file).
|
|
47
|
+
* @param {RuleContext} context
|
|
48
|
+
*/
|
|
49
|
+
export function appPath(context) {
|
|
50
|
+
return relative(appRoot(context), context.physicalFilename).split(sep).join('/');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A file's text, or null when it does not exist (read on every call: an editor keeps the linter alive).
|
|
55
|
+
* @param {string} file
|
|
56
|
+
*/
|
|
57
|
+
export function readText(file) {
|
|
58
|
+
return existsSync(file) ? readFileSync(file, 'utf8') : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A JSON file under the app root, or null when missing or invalid.
|
|
63
|
+
* @param {RuleContext} context
|
|
64
|
+
* @param {string} file
|
|
65
|
+
* @returns {any}
|
|
66
|
+
*/
|
|
67
|
+
function readAppJson(context, file) {
|
|
68
|
+
const text = readText(join(appRoot(context), file));
|
|
69
|
+
try {
|
|
70
|
+
return text === null ? null : JSON.parse(text);
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* `child` when federation.config.js declares exposes, `host` when it exists without them, else null.
|
|
78
|
+
* `settings.sixbellSdk.appKind` overrides the detection.
|
|
79
|
+
* @param {RuleContext} context
|
|
80
|
+
* @returns {'host' | 'child' | null}
|
|
81
|
+
*/
|
|
82
|
+
export function appKind(context) {
|
|
83
|
+
const settings = /** @type {{ sixbellSdk?: { appKind?: 'host' | 'child' } }} */ (context.settings);
|
|
84
|
+
if (settings.sixbellSdk?.appKind) return settings.sixbellSdk.appKind;
|
|
85
|
+
const federation = readText(join(appRoot(context), 'federation.config.js'));
|
|
86
|
+
if (federation === null) return null;
|
|
87
|
+
return /\bexposes\s*:\s*\{\s*['"]/.test(federation) ? 'child' : 'host';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Languages configured in src/assets/translation/translation.json, or null without the file.
|
|
92
|
+
* @param {RuleContext} context
|
|
93
|
+
*/
|
|
94
|
+
export function languageCount(context) {
|
|
95
|
+
const translation = readAppJson(context, 'src/assets/translation/translation.json');
|
|
96
|
+
if (!translation) return null;
|
|
97
|
+
const locales = Object.keys(translation.locale?.languageToLocale ?? {});
|
|
98
|
+
if (locales.length) return locales.length;
|
|
99
|
+
return 1 + (translation.additionalLanguages?.length ?? 0) - (translation.excludeLanguages?.length ?? 0);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Enabled themes in src/assets/themes/themes.json, or null without the file.
|
|
104
|
+
* @param {RuleContext} context
|
|
105
|
+
*/
|
|
106
|
+
export function themeCount(context) {
|
|
107
|
+
const themes = readAppJson(context, 'src/assets/themes/themes.json');
|
|
108
|
+
if (!Array.isArray(themes?.themes)) return null;
|
|
109
|
+
const excluded = new Set(themes.excludeThemes ?? []);
|
|
110
|
+
return themes.themes.filter((/** @type {unknown} */ theme) => !excluded.has(theme)).length;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The text of the file next to this one with the other extension (`x.component.html` for
|
|
115
|
+
* `x.component.ts`), or ''.
|
|
116
|
+
* @param {RuleContext} context
|
|
117
|
+
*/
|
|
118
|
+
export function siblingText(context) {
|
|
119
|
+
const file = context.physicalFilename;
|
|
120
|
+
const other = file.endsWith('.html') ? file.replace(/\.html$/, '.ts') : file.replace(/\.ts$/, '.html');
|
|
121
|
+
return other === file ? '' : (readText(other) ?? '');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The text of another file in the same folder, or ''.
|
|
126
|
+
* @param {RuleContext} context
|
|
127
|
+
* @param {string} name
|
|
128
|
+
*/
|
|
129
|
+
export function neighbourText(context, name) {
|
|
130
|
+
return readText(join(dirname(context.physicalFilename), name)) ?? '';
|
|
131
|
+
}
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template rules for the UI composition standard (DESIGN.md "UI composition") and other app
|
|
3
|
+
* conventions, ported from the MFE validators with their original rule ids.
|
|
4
|
+
*/
|
|
5
|
+
import { getTemplateParserServices } from '@angular-eslint/utils';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { appPath, appRoot, idRule, languageCount, readText, siblingText, themeCount } from './app-support.mjs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Template AST nodes are read by shape (Element, Text, BoundText, IfBlock, SwitchBlock ...).
|
|
11
|
+
* @typedef {any} Node
|
|
12
|
+
* @typedef {import('eslint').Rule.RuleContext} RuleContext
|
|
13
|
+
* @typedef {import('../types.js').TemplateSpan} TemplateSpan
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {RuleContext} context
|
|
18
|
+
* @returns {(span: TemplateSpan) => import('eslint').AST.SourceLocation}
|
|
19
|
+
*/
|
|
20
|
+
const locOf = (context) => {
|
|
21
|
+
const services = /** @type {{ convertNodeSourceSpanToLoc(span: TemplateSpan): import('eslint').AST.SourceLocation }} */ (
|
|
22
|
+
/** @type {unknown} */ (getTemplateParserServices(/** @type {any} */ (context)))
|
|
23
|
+
);
|
|
24
|
+
return (span) => services.convertNodeSourceSpanToLoc(span);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** @param {Node} element */
|
|
28
|
+
const startSpan = (element) => element.startSourceSpan ?? element.sourceSpan;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Static attribute value, `undefined` when absent, `null` when bound (`[name]="expr"`) and so unknown here.
|
|
32
|
+
* @param {Node} element
|
|
33
|
+
* @param {string} name
|
|
34
|
+
* @returns {string | null | undefined}
|
|
35
|
+
*/
|
|
36
|
+
function staticInput(element, name) {
|
|
37
|
+
const attribute = element.attributes.find((/** @type {Node} */ candidate) => candidate.name === name);
|
|
38
|
+
if (attribute) return attribute.value;
|
|
39
|
+
const input = element.inputs.find((/** @type {Node} */ candidate) => candidate.name === name);
|
|
40
|
+
if (!input) return undefined;
|
|
41
|
+
const literal = /^\s*'([^']*)'\s*$|^\s*"([^"]*)"\s*$/.exec(input.value?.source ?? '');
|
|
42
|
+
return literal ? (literal[1] ?? literal[2]) : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A boolean flag input (`ghost`, `[ghost]="true"`, `[ghost]="expr"`) that is not literally false.
|
|
47
|
+
* @param {Node} element
|
|
48
|
+
* @param {string} name
|
|
49
|
+
*/
|
|
50
|
+
function flagSet(element, name) {
|
|
51
|
+
const attribute = element.attributes.find((/** @type {Node} */ candidate) => candidate.name === name);
|
|
52
|
+
if (attribute) return attribute.value !== 'false';
|
|
53
|
+
const input = element.inputs.find((/** @type {Node} */ candidate) => candidate.name === name);
|
|
54
|
+
return Boolean(input) && input.value?.source?.trim() !== 'false';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Lower-case words of a text: splits on non-letters and camelCase (`confirmDelete()` -> confirm, delete).
|
|
59
|
+
* @param {string} text
|
|
60
|
+
*/
|
|
61
|
+
function textWords(text) {
|
|
62
|
+
return text
|
|
63
|
+
.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
64
|
+
.toLowerCase()
|
|
65
|
+
.split(/[^a-zà-ÿ]+/)
|
|
66
|
+
.filter(Boolean);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The visible label of an element: its text and interpolations (i18n keys included).
|
|
71
|
+
* @param {Node} element
|
|
72
|
+
* @returns {string}
|
|
73
|
+
*/
|
|
74
|
+
function labelText(element) {
|
|
75
|
+
return (element.children ?? [])
|
|
76
|
+
.map((/** @type {Node} */ child) => {
|
|
77
|
+
if (typeof child.value === 'string') return child.value;
|
|
78
|
+
if (typeof child.value?.source === 'string') return child.value.source;
|
|
79
|
+
return child.children ? labelText(child) : '';
|
|
80
|
+
})
|
|
81
|
+
.join(' ');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Function names the element's event handlers call.
|
|
86
|
+
* @param {Node} element
|
|
87
|
+
* @returns {string[]}
|
|
88
|
+
*/
|
|
89
|
+
const handlerCalls = (element) =>
|
|
90
|
+
(element.outputs ?? []).flatMap((/** @type {Node} */ output) =>
|
|
91
|
+
[...(output.handler?.source ?? '').matchAll(/([A-Za-z_$][\w$]*)\s*\(/g)].map((m) => m[1]),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
/** @param {Node} element */
|
|
95
|
+
function iconWords(element) {
|
|
96
|
+
const icon = (element.inputs ?? []).find((/** @type {Node} */ input) => input.name === 'icon');
|
|
97
|
+
const attribute = (element.attributes ?? []).find((/** @type {Node} */ candidate) => candidate.name === 'icon');
|
|
98
|
+
return textWords(icon?.value?.source ?? attribute?.value ?? '');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Every rendered order of `st-button`s under `nodes`: one sequence per combination of `@if`/`@switch`
|
|
103
|
+
* branches (an `@if` without `@else`, or a `@switch` without `@default`, may also render nothing).
|
|
104
|
+
* Capped so a large template stays cheap.
|
|
105
|
+
* @param {Node[]} nodes
|
|
106
|
+
* @param {number} [cap]
|
|
107
|
+
* @returns {Node[][]}
|
|
108
|
+
*/
|
|
109
|
+
function buttonSequences(nodes, cap = 32) {
|
|
110
|
+
/** @type {Node[][]} */
|
|
111
|
+
let sequences = [[]];
|
|
112
|
+
/** @param {Node[][]} options */
|
|
113
|
+
const combine = (options) => {
|
|
114
|
+
/** @type {Node[][]} */
|
|
115
|
+
const next = [];
|
|
116
|
+
for (const sequence of sequences) for (const option of options) if (next.length < cap) next.push([...sequence, ...option]);
|
|
117
|
+
sequences = next;
|
|
118
|
+
};
|
|
119
|
+
for (const node of nodes) {
|
|
120
|
+
if (node.name === 'st-button') {
|
|
121
|
+
sequences = sequences.map((sequence) => [...sequence, node]);
|
|
122
|
+
} else if (Array.isArray(node.branches)) {
|
|
123
|
+
const options = node.branches.flatMap((/** @type {Node} */ branch) => buttonSequences(branch.children ?? [], cap));
|
|
124
|
+
if (!node.branches.some((/** @type {Node} */ branch) => branch.expression == null)) options.push([]);
|
|
125
|
+
combine(options);
|
|
126
|
+
} else if (Array.isArray(node.cases)) {
|
|
127
|
+
const options = node.cases.flatMap((/** @type {Node} */ switchCase) => buttonSequences(switchCase.children ?? [], cap));
|
|
128
|
+
if (!node.cases.some((/** @type {Node} */ switchCase) => switchCase.expression == null)) options.push([]);
|
|
129
|
+
combine(options);
|
|
130
|
+
} else if (node.children?.length) {
|
|
131
|
+
combine(buttonSequences(node.children, cap));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return sequences;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** BUTTON_VARIANT_001: every st-button names its variant. */
|
|
138
|
+
export const buttonVariant = idRule({
|
|
139
|
+
name: 'button-variant',
|
|
140
|
+
type: 'suggestion',
|
|
141
|
+
description: 'Give every <st-button> an explicit variant',
|
|
142
|
+
messages: {
|
|
143
|
+
BUTTON_VARIANT_001:
|
|
144
|
+
'<st-button> without variant: name the action role (primary, secondary, tertiary outline, error ...). SDK 3.1 and earlier defaulted to primary.',
|
|
145
|
+
},
|
|
146
|
+
create(context) {
|
|
147
|
+
const loc = locOf(context);
|
|
148
|
+
return {
|
|
149
|
+
/** @param {Node} element */
|
|
150
|
+
Element(element) {
|
|
151
|
+
if (element.name !== 'st-button' || staticInput(element, 'variant') !== undefined) return;
|
|
152
|
+
context.report({ loc: loc(startSpan(element)), messageId: 'BUTTON_VARIANT_001' });
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
/** UI_BUTTON_BASE_001: variant="base" is only for ghost or link utilities. */
|
|
159
|
+
export const buttonRole = idRule({
|
|
160
|
+
name: 'button-role',
|
|
161
|
+
type: 'suggestion',
|
|
162
|
+
description: 'Use variant="base" only on ghost or link buttons',
|
|
163
|
+
messages: {
|
|
164
|
+
UI_BUTTON_BASE_001:
|
|
165
|
+
'<st-button variant="base"> without [ghost] or [link]: base is only for ghost/link utilities. Give the action its role variant (primary, secondary, tertiary outline, error).',
|
|
166
|
+
},
|
|
167
|
+
create(context) {
|
|
168
|
+
const loc = locOf(context);
|
|
169
|
+
return {
|
|
170
|
+
/** @param {Node} element */
|
|
171
|
+
Element(element) {
|
|
172
|
+
if (element.name !== 'st-button' || staticInput(element, 'variant') !== 'base') return;
|
|
173
|
+
if (flagSet(element, 'ghost') || flagSet(element, 'link')) return;
|
|
174
|
+
context.report({ loc: loc(startSpan(element)), messageId: 'UI_BUTTON_BASE_001' });
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const CONFIRM_VARIANTS = new Set(['primary', 'error']);
|
|
181
|
+
/** Whole words of a visible dismiss label (es/en/pt), matched against the button's label words only. */
|
|
182
|
+
const DISMISS_WORDS = new Set(['cancel', 'cancelar', 'close', 'cerrar', 'fechar', 'dismiss', 'back', 'volver', 'voltar']);
|
|
183
|
+
/** Word stems of a destructive action, matched against the label, the called handler names and the icon. */
|
|
184
|
+
const DELETE_STEMS = ['delet', 'elimin', 'exclu', 'borrar', 'destroy', 'trash'];
|
|
185
|
+
/** Remove-from-a-list stems: only the visible label counts (`removeOpen.set(false)` is not a delete). */
|
|
186
|
+
const REMOVE_STEMS = ['remov', 'quitar'];
|
|
187
|
+
/** A secondary or tertiary button in dialog actions is the dismiss, whatever its label. */
|
|
188
|
+
const NON_CONFIRM_VARIANTS = new Set(['secondary', 'tertiary']);
|
|
189
|
+
|
|
190
|
+
/** UI_DIALOG_CONFIRM_001 and UI_DIALOG_DELETE_001: dialog actions are dismiss then confirm, and a delete confirm is error. */
|
|
191
|
+
export const dialogActions = idRule({
|
|
192
|
+
name: 'dialog-actions',
|
|
193
|
+
type: 'suggestion',
|
|
194
|
+
description: 'Put the dialog confirm last and make a delete confirm variant="error"',
|
|
195
|
+
messages: {
|
|
196
|
+
UI_DIALOG_CONFIRM_001:
|
|
197
|
+
'Dialog confirm (variant="{{variant}}") is not the last button: dialogs are right-aligned dismiss then confirm, with the confirm rightmost.',
|
|
198
|
+
UI_DIALOG_DELETE_001:
|
|
199
|
+
'Delete confirm in st-dialog-actions is variant="{{variant}}": a destructive confirm is variant="error", after a secondary Cancel.',
|
|
200
|
+
},
|
|
201
|
+
create(context) {
|
|
202
|
+
const loc = locOf(context);
|
|
203
|
+
/** @param {Node} button */
|
|
204
|
+
const isConfirm = (button) => CONFIRM_VARIANTS.has(staticInput(button, 'variant') ?? '') && !flagSet(button, 'ghost') && !flagSet(button, 'link');
|
|
205
|
+
return {
|
|
206
|
+
/** @param {Node} actions */
|
|
207
|
+
Element(actions) {
|
|
208
|
+
if (actions.name !== 'st-dialog-actions') return;
|
|
209
|
+
const sequences = buttonSequences(actions.children);
|
|
210
|
+
const misordered = sequences.find((sequence) => {
|
|
211
|
+
if (sequence.length < 2 || sequence.some((button) => staticInput(button, 'variant') === null)) return false;
|
|
212
|
+
return sequence.some(isConfirm) && !isConfirm(sequence[sequence.length - 1]);
|
|
213
|
+
});
|
|
214
|
+
if (misordered) {
|
|
215
|
+
const confirm = misordered.find(isConfirm);
|
|
216
|
+
context.report({
|
|
217
|
+
loc: loc(startSpan(confirm)),
|
|
218
|
+
messageId: 'UI_DIALOG_CONFIRM_001',
|
|
219
|
+
data: { variant: staticInput(confirm, 'variant') ?? '' },
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
for (const button of new Set(sequences.flat())) {
|
|
223
|
+
const variant = staticInput(button, 'variant');
|
|
224
|
+
if (variant === null || variant === 'error' || NON_CONFIRM_VARIANTS.has(variant ?? '')) continue;
|
|
225
|
+
const label = textWords(labelText(button));
|
|
226
|
+
const calls = handlerCalls(button);
|
|
227
|
+
const isDelete =
|
|
228
|
+
[...label, ...calls.flatMap(textWords), ...iconWords(button)].some((word) => DELETE_STEMS.some((stem) => word.startsWith(stem))) ||
|
|
229
|
+
label.some((word) => REMOVE_STEMS.some((stem) => word.startsWith(stem)));
|
|
230
|
+
const isDismiss = label.some((word) => DISMISS_WORDS.has(word)) || calls.some((call) => DISMISS_WORDS.has(textWords(call)[0] ?? ''));
|
|
231
|
+
if (!isDelete || isDismiss) continue;
|
|
232
|
+
context.report({ loc: loc(startSpan(button)), messageId: 'UI_DIALOG_DELETE_001', data: { variant: variant ?? '(none)' } });
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
/** SDK_UI_WARN_002: an st-card inside another st-card. */
|
|
240
|
+
export const noNestedCard = idRule({
|
|
241
|
+
name: 'no-nested-card',
|
|
242
|
+
type: 'suggestion',
|
|
243
|
+
description: 'Do not nest <st-card> inside another <st-card>',
|
|
244
|
+
messages: { SDK_UI_WARN_002: '<st-card> nested inside another <st-card>: use one card per surface, and sections or st-divider inside it.' },
|
|
245
|
+
create(context) {
|
|
246
|
+
const loc = locOf(context);
|
|
247
|
+
return {
|
|
248
|
+
/** @param {Node} element */
|
|
249
|
+
Element(element) {
|
|
250
|
+
if (element.name !== 'st-card') return;
|
|
251
|
+
for (let parent = element.parent; parent; parent = parent.parent) {
|
|
252
|
+
if (parent.name === 'st-card') {
|
|
253
|
+
context.report({ loc: loc(startSpan(element)), messageId: 'SDK_UI_WARN_002' });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
/** SDK_UI_005 and SDK_UI_006: st-button has no appearance or color input. */
|
|
263
|
+
export const buttonLegacyInput = idRule({
|
|
264
|
+
name: 'button-legacy-input',
|
|
265
|
+
description: 'Disallow the removed `appearance` and `color` inputs on <st-button>',
|
|
266
|
+
messages: {
|
|
267
|
+
SDK_UI_005: '<st-button> has no `appearance` input: use variant, size, ghost, circle or icon.',
|
|
268
|
+
SDK_UI_006: '<st-button> has no `color` input: use variant.',
|
|
269
|
+
},
|
|
270
|
+
create(context) {
|
|
271
|
+
const loc = locOf(context);
|
|
272
|
+
return {
|
|
273
|
+
/** @param {Node} element */
|
|
274
|
+
Element(element) {
|
|
275
|
+
if (element.name !== 'st-button') return;
|
|
276
|
+
// Bound [appearance]/[color] are left to the type checker (NG8002), so nothing is reported twice.
|
|
277
|
+
for (const attribute of element.attributes) {
|
|
278
|
+
const messageId = { appearance: 'SDK_UI_005', color: 'SDK_UI_006' }[/** @type {string} */ (attribute.name)];
|
|
279
|
+
if (messageId) context.report({ loc: loc(attribute.keySpan ?? attribute.sourceSpan), messageId });
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
/** SDK_UI_001: the removed tyVariant API on a non-kit element (kit elements get unknown-input). */
|
|
287
|
+
export const noTyVariant = idRule({
|
|
288
|
+
name: 'no-ty-variant',
|
|
289
|
+
description: 'Disallow the removed tyVariant typography API',
|
|
290
|
+
messages: { SDK_UI_001: 'tyVariant was removed: use <st-text> (preset, as) or the stText directive.' },
|
|
291
|
+
create(context) {
|
|
292
|
+
const loc = locOf(context);
|
|
293
|
+
return {
|
|
294
|
+
/** @param {Node} element */
|
|
295
|
+
Element(element) {
|
|
296
|
+
if (/^st-/.test(element.name)) return;
|
|
297
|
+
for (const node of [...element.attributes, ...element.inputs]) {
|
|
298
|
+
if (node.name === 'tyVariant') context.report({ loc: loc(node.keySpan ?? node.sourceSpan), messageId: 'SDK_UI_001' });
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* MFE_PREF_001, MFE_PREF_002, MFE_PREF_003: a language, theme or layout selector is shown although
|
|
307
|
+
* the app configures only one option.
|
|
308
|
+
*/
|
|
309
|
+
export const preferenceSelectorTemplate = idRule({
|
|
310
|
+
name: 'preference-selectors',
|
|
311
|
+
description: 'Hide the language, theme or layout selector when only one option is configured',
|
|
312
|
+
messages: {
|
|
313
|
+
MFE_PREF_001: 'Only one language is configured (translation.json): hide the language selector (showLanguageSelector).',
|
|
314
|
+
MFE_PREF_002: 'Only one theme is configured (themes.json): hide the theme selector (showThemeSelector).',
|
|
315
|
+
MFE_PREF_003: 'Only one layout is enabled (layout.service.ts LAYOUT_OPTIONS): hide the layout selector (showLayoutSelector).',
|
|
316
|
+
},
|
|
317
|
+
create(context) {
|
|
318
|
+
const source = context.sourceCode.getText();
|
|
319
|
+
const nearby = `${source}\n${siblingText(context)}`;
|
|
320
|
+
/**
|
|
321
|
+
* @param {RegExp} use
|
|
322
|
+
* @param {string} messageId
|
|
323
|
+
*/
|
|
324
|
+
const report = (use, messageId) => {
|
|
325
|
+
const match = use.exec(source);
|
|
326
|
+
if (!match) return;
|
|
327
|
+
const position = context.sourceCode.getLocFromIndex(match.index);
|
|
328
|
+
context.report({ loc: { start: position, end: context.sourceCode.getLocFromIndex(match.index + match[0].length) }, messageId });
|
|
329
|
+
};
|
|
330
|
+
return {
|
|
331
|
+
Program() {
|
|
332
|
+
if (
|
|
333
|
+
/getAvailableLanguagesConfig/.test(source) &&
|
|
334
|
+
!/showLanguageSelector|languages\(?\)?\.length\s*>\s*1/.test(nearby) &&
|
|
335
|
+
languageCount(context) === 1
|
|
336
|
+
) {
|
|
337
|
+
report(/getAvailableLanguagesConfig/, 'MFE_PREF_001');
|
|
338
|
+
}
|
|
339
|
+
if (/getAvailableThemes\(\)/.test(source) && !/showThemeSelector/.test(nearby) && themeCount(context) === 1) {
|
|
340
|
+
report(/getAvailableThemes\(\)/, 'MFE_PREF_002');
|
|
341
|
+
}
|
|
342
|
+
if (/openLayoutSwitcher|handleOpenLayoutSwitcher|layout-switcher/.test(source) && !/showLayoutSelector/.test(nearby)) {
|
|
343
|
+
const layout = readText(join(appRoot(context), 'src/app/core/services/layout/layout.service.ts'));
|
|
344
|
+
if (layout === null) return;
|
|
345
|
+
const options = /LAYOUT_OPTIONS[\s\S]*?=\s*\[([\s\S]*?)\];/.exec(layout)?.[1] ?? layout;
|
|
346
|
+
if ((options.match(/value:\s*['"](classic|modern)['"]/g) ?? []).length === 1) {
|
|
347
|
+
report(/openLayoutSwitcher|handleOpenLayoutSwitcher|layout-switcher/, 'MFE_PREF_003');
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
const PLACEHOLDER_MARKERS = ['circle cx="120" cy="120" r="88"', 'path d="M78 150h84"'];
|
|
356
|
+
|
|
357
|
+
/** MFE_STATUS_ILLUSTRATION_001: a status illustration still holds the starter placeholder SVG. */
|
|
358
|
+
export const statusIllustration = idRule({
|
|
359
|
+
name: 'status-illustration',
|
|
360
|
+
description: 'Replace the starter placeholder SVG in status page illustrations',
|
|
361
|
+
messages: {
|
|
362
|
+
MFE_STATUS_ILLUSTRATION_001: 'Status illustration is the starter placeholder SVG: use the product illustrations (copy them from IRP or Tracer).',
|
|
363
|
+
},
|
|
364
|
+
create(context) {
|
|
365
|
+
return {
|
|
366
|
+
Program(node) {
|
|
367
|
+
if (!/-illustration\.component\.(html|ts)$/.test(appPath(context))) return;
|
|
368
|
+
const source = context.sourceCode.getText();
|
|
369
|
+
if (PLACEHOLDER_MARKERS.every((marker) => source.includes(marker))) context.report({ node, messageId: 'MFE_STATUS_ILLUSTRATION_001' });
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
},
|
|
373
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** TypeScript rules: imports of @sixbell-telco/sdk checked against the manifest's entry points. */
|
|
2
|
+
import { closest, manifestFor } from '../manifest.mjs';
|
|
3
|
+
|
|
4
|
+
const PACKAGE = '@sixbell-telco/sdk';
|
|
5
|
+
|
|
6
|
+
export const entryPointImport = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: /** @type {const} */ ('problem'),
|
|
9
|
+
docs: {
|
|
10
|
+
description: 'Import the SDK from an entry point that exists, never from the package root',
|
|
11
|
+
url: 'https://github.com/EnghouseGlobal/nw-sx-web-libs-ui-kit/blob/develop/projects/eslint-plugin-sdk/README.md#entry-point-import',
|
|
12
|
+
},
|
|
13
|
+
schema: [],
|
|
14
|
+
messages: {
|
|
15
|
+
barrel: 'Import from an entry point such as "@sixbell-telco/sdk/components/button", never from "@sixbell-telco/sdk".',
|
|
16
|
+
unknown: '"{{source}}" is not an entry point of @sixbell-telco/sdk {{version}}.{{suggestion}}',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
|
20
|
+
create(context) {
|
|
21
|
+
const index = manifestFor(context);
|
|
22
|
+
/** @param {{ source?: { type: string, value?: unknown } | null }} node */
|
|
23
|
+
const check = (node) => {
|
|
24
|
+
if (!node.source || node.source.type !== 'Literal') return;
|
|
25
|
+
const source = node.source.value;
|
|
26
|
+
if (typeof source !== 'string' || (source !== PACKAGE && !source.startsWith(`${PACKAGE}/`))) return;
|
|
27
|
+
if (source === PACKAGE) {
|
|
28
|
+
context.report({ node: node.source, messageId: 'barrel' });
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Non-module files the package exports on purpose (stylesheet, manifest, i18n bundles).
|
|
32
|
+
if (/\.(css|json)$/.test(source) || !index || index.entryPoints.has(source)) return;
|
|
33
|
+
const match = closest(source, [...index.entryPoints], 8);
|
|
34
|
+
context.report({
|
|
35
|
+
node: node.source,
|
|
36
|
+
messageId: 'unknown',
|
|
37
|
+
data: { source, version: index.manifest.version, suggestion: match ? ` Did you mean "${match}"?` : '' },
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
return { ImportDeclaration: check, ExportNamedDeclaration: check, ExportAllDeclaration: check, ImportExpression: check };
|
|
41
|
+
},
|
|
42
|
+
};
|