@siemens/element-ng 51.5.0 → 51.7.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/README.md +3 -4
- package/element-ng.scss +3 -1
- package/fesm2022/siemens-element-ng-accordion.mjs +2 -2
- package/fesm2022/siemens-element-ng-accordion.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-ag-grid.mjs +9 -9
- package/fesm2022/siemens-element-ng-ag-grid.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-application-header.mjs +47 -14
- package/fesm2022/siemens-element-ng-application-header.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-avatar.mjs +3 -3
- package/fesm2022/siemens-element-ng-avatar.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-card.mjs +6 -5
- package/fesm2022/siemens-element-ng-card.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-carousel.mjs +6 -4
- package/fesm2022/siemens-element-ng-carousel.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-chat-messages.mjs +605 -32
- package/fesm2022/siemens-element-ng-chat-messages.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-color-picker.mjs +55 -39
- package/fesm2022/siemens-element-ng-color-picker.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-content-action-bar.mjs +2 -2
- package/fesm2022/siemens-element-ng-content-action-bar.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-dashboard.mjs +18 -17
- package/fesm2022/siemens-element-ng-dashboard.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-date-range-filter.mjs +3 -3
- package/fesm2022/siemens-element-ng-date-range-filter.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-datepicker.mjs +22 -9
- package/fesm2022/siemens-element-ng-datepicker.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-empty-state.mjs +13 -6
- package/fesm2022/siemens-element-ng-empty-state.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-file-uploader.mjs +2 -2
- package/fesm2022/siemens-element-ng-file-uploader.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-filter-bar.mjs +4 -4
- package/fesm2022/siemens-element-ng-filter-bar.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-filtered-search.mjs +19 -5
- package/fesm2022/siemens-element-ng-filtered-search.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-footer.mjs +2 -2
- package/fesm2022/siemens-element-ng-form.mjs +4 -4
- package/fesm2022/siemens-element-ng-formly.mjs +1 -1
- package/fesm2022/siemens-element-ng-formly.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-icon.mjs +2 -1
- package/fesm2022/siemens-element-ng-icon.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-info-page.mjs +20 -7
- package/fesm2022/siemens-element-ng-info-page.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-inline-notification.mjs +2 -2
- package/fesm2022/siemens-element-ng-inline-notification.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-landing-page.mjs +2 -2
- package/fesm2022/siemens-element-ng-landing-page.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-list-details.mjs +2 -2
- package/fesm2022/siemens-element-ng-list-details.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-loading-spinner.mjs +2 -2
- package/fesm2022/siemens-element-ng-loading-spinner.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-maplibre.mjs +133 -10
- package/fesm2022/siemens-element-ng-maplibre.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-menu.mjs +6 -6
- package/fesm2022/siemens-element-ng-menu.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-modal.mjs +71 -84
- package/fesm2022/siemens-element-ng-modal.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-navbar-vertical-next.mjs +64 -14
- package/fesm2022/siemens-element-ng-navbar-vertical-next.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-navbar-vertical.mjs +4 -4
- package/fesm2022/siemens-element-ng-navbar-vertical.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-navbar.mjs +8 -3
- package/fesm2022/siemens-element-ng-navbar.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-notification-item.mjs +2 -2
- package/fesm2022/siemens-element-ng-notification-item.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-number-input.mjs +2 -2
- package/fesm2022/siemens-element-ng-number-input.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-password-strength.mjs +2 -2
- package/fesm2022/siemens-element-ng-password-strength.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-phone-number.mjs +32 -23
- package/fesm2022/siemens-element-ng-phone-number.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-photo-upload.mjs +7 -2
- package/fesm2022/siemens-element-ng-photo-upload.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-pills-input.mjs +2 -2
- package/fesm2022/siemens-element-ng-pills-input.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-result-details-list.mjs +11 -6
- package/fesm2022/siemens-element-ng-result-details-list.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-search-bar.mjs +188 -15
- package/fesm2022/siemens-element-ng-search-bar.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-select.mjs +631 -764
- package/fesm2022/siemens-element-ng-select.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-side-panel.mjs +19 -10
- package/fesm2022/siemens-element-ng-side-panel.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-slider.mjs +2 -2
- package/fesm2022/siemens-element-ng-slider.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-source-chip.mjs +2 -2
- package/fesm2022/siemens-element-ng-source-chip.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-split.mjs +2 -2
- package/fesm2022/siemens-element-ng-split.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-status-bar.mjs +12 -12
- package/fesm2022/siemens-element-ng-status-bar.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-status-counter.mjs +2 -2
- package/fesm2022/siemens-element-ng-status-counter.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-status-toggle.mjs +2 -2
- package/fesm2022/siemens-element-ng-status-toggle.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-summary-chip.mjs +2 -2
- package/fesm2022/siemens-element-ng-summary-chip.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-summary-widget.mjs +2 -2
- package/fesm2022/siemens-element-ng-summary-widget.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-tabs-legacy.mjs +2 -1
- package/fesm2022/siemens-element-ng-tabs-legacy.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-tabs.mjs +3 -6
- package/fesm2022/siemens-element-ng-tabs.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-theme.mjs +9 -5
- package/fesm2022/siemens-element-ng-theme.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-threshold.mjs +2 -2
- package/fesm2022/siemens-element-ng-threshold.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-toast-notification.mjs +2 -2
- package/fesm2022/siemens-element-ng-toast-notification.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-tour.mjs +2 -2
- package/fesm2022/siemens-element-ng-tour.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-translate.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-tree-view.mjs +8 -25
- package/fesm2022/siemens-element-ng-tree-view.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-typeahead.mjs +42 -30
- package/fesm2022/siemens-element-ng-typeahead.mjs.map +1 -1
- package/fesm2022/siemens-element-ng-wizard.mjs +2 -2
- package/fesm2022/siemens-element-ng-wizard.mjs.map +1 -1
- package/maplibre/styles/maplibre-gl.scss +2 -2
- package/package.json +8 -8
- package/schematics/collection.json +5 -0
- package/schematics/migrate-search-bar/index.js +5 -0
- package/schematics/migrate-search-bar/migrate-search-bar-rule.js +733 -0
- package/schematics/migrate-search-bar/schema.json +14 -0
- package/schematics/migrate-search-bar/search-bar-template.js +742 -0
- package/schematics/migrate-search-bar/text-edits.js +23 -0
- package/schematics/migrations/data/css-custom-properties.js +11 -11
- package/schematics/utils/project-utils.js +2 -1
- package/template-i18n.json +9 -0
- package/types/siemens-element-ng-application-header.d.ts +12 -2
- package/types/siemens-element-ng-avatar.d.ts +1 -1
- package/types/siemens-element-ng-card.d.ts +2 -2
- package/types/siemens-element-ng-chat-messages.d.ts +517 -124
- package/types/siemens-element-ng-color-picker.d.ts +18 -9
- package/types/siemens-element-ng-dashboard.d.ts +6 -2
- package/types/siemens-element-ng-empty-state.d.ts +9 -2
- package/types/siemens-element-ng-info-page.d.ts +17 -5
- package/types/siemens-element-ng-maplibre.d.ts +39 -2
- package/types/siemens-element-ng-modal.d.ts +6 -4
- package/types/siemens-element-ng-navbar-vertical-next.d.ts +31 -2
- package/types/siemens-element-ng-navbar.d.ts +1 -0
- package/types/siemens-element-ng-phone-number.d.ts +5 -6
- package/types/siemens-element-ng-photo-upload.d.ts +1 -0
- package/types/siemens-element-ng-result-details-list.d.ts +7 -2
- package/types/siemens-element-ng-search-bar.d.ts +110 -5
- package/types/siemens-element-ng-select.d.ts +257 -286
- package/types/siemens-element-ng-side-panel.d.ts +5 -1
- package/types/siemens-element-ng-status-bar.d.ts +2 -2
- package/types/siemens-element-ng-tabs-legacy.d.ts +2 -1
- package/types/siemens-element-ng-theme.d.ts +1 -0
- package/types/siemens-element-ng-tour.d.ts +11 -0
- package/types/siemens-element-ng-translate.d.ts +9 -0
- package/types/siemens-element-ng-tree-view.d.ts +0 -8
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Siemens 2016 - 2026
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
import { ArrowFunction, AST, ASTWithSource, BindingPipe, Block, Call, Chain, Element, EmptyExpr, HtmlParser, ImplicitReceiver, Lexer, LiteralMap, LiteralPrimitive, ParseLocation, Parser, ParseSourceFile, ParseSourceSpan, PrefixNot, PropertyRead, RecursiveVisitor, SafeCall, SafePropertyRead, ThisReceiver, visitAll } from '@angular/compiler';
|
|
6
|
+
import { applyTextEdits } from './text-edits.js';
|
|
7
|
+
const SEARCH_BAR = 'si-search-bar';
|
|
8
|
+
const INPUT_DIRECTIVE_ATTRIBUTE = 'siSearchBarInput';
|
|
9
|
+
const DEFAULT_DEBOUNCE_TIME = 400;
|
|
10
|
+
const MAX_LINE_LENGTH = 100;
|
|
11
|
+
const DEPRECATED_INPUTS = ['placeholder', 'maxlength', 'readonly', 'disabled', 'value'];
|
|
12
|
+
const BEHAVIOR_INPUTS = ['debounceTime', 'prohibitedCharacters'];
|
|
13
|
+
const CLASSIC_FORM_INPUTS = [
|
|
14
|
+
'ngModel',
|
|
15
|
+
'formControl',
|
|
16
|
+
'formControlName',
|
|
17
|
+
'ngModelOptions',
|
|
18
|
+
'name',
|
|
19
|
+
'required',
|
|
20
|
+
'minlength',
|
|
21
|
+
'pattern',
|
|
22
|
+
'email'
|
|
23
|
+
];
|
|
24
|
+
const MANUAL_MIGRATION_HINT = 'Migrate it manually by projecting an `<input siSearchBarInput>` and binding a form field to it.';
|
|
25
|
+
class SearchBarCollector extends RecursiveVisitor {
|
|
26
|
+
searchBars = [];
|
|
27
|
+
localNames = new Set();
|
|
28
|
+
ancestors = [];
|
|
29
|
+
visitElement(element, context) {
|
|
30
|
+
element.attrs.forEach(attribute => this.collectAttributeNames(attribute));
|
|
31
|
+
if (element.name === SEARCH_BAR) {
|
|
32
|
+
this.searchBars.push({ element, ancestors: [...this.ancestors] });
|
|
33
|
+
}
|
|
34
|
+
this.ancestors.push(element);
|
|
35
|
+
const result = super.visitElement(element, context);
|
|
36
|
+
this.ancestors.pop();
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
visitBlock(block, context) {
|
|
40
|
+
block.parameters.forEach((parameter, index) => {
|
|
41
|
+
const expression = parameter.expression;
|
|
42
|
+
const forItem = /^\s*([A-Za-z_$][\w$]*)\s+of\b/.exec(expression);
|
|
43
|
+
if (block.name === 'for' && index === 0 && forItem) {
|
|
44
|
+
this.localNames.add(forItem[1]);
|
|
45
|
+
}
|
|
46
|
+
const alias = /^\s*as\s+([A-Za-z_$][\w$]*)/.exec(expression);
|
|
47
|
+
if (alias) {
|
|
48
|
+
this.localNames.add(alias[1]);
|
|
49
|
+
}
|
|
50
|
+
const letDeclarations = /^\s*let\s+([\s\S]*)$/.exec(expression);
|
|
51
|
+
for (const declaration of letDeclarations?.[1].split(',') ?? []) {
|
|
52
|
+
const name = /^\s*([A-Za-z_$][\w$]*)/.exec(declaration);
|
|
53
|
+
if (name) {
|
|
54
|
+
this.localNames.add(name[1]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
this.ancestors.push(block);
|
|
59
|
+
const result = super.visitBlock(block, context);
|
|
60
|
+
this.ancestors.pop();
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
visitLetDeclaration(declaration, context) {
|
|
64
|
+
this.localNames.add(declaration.name);
|
|
65
|
+
super.visitLetDeclaration(declaration, context);
|
|
66
|
+
}
|
|
67
|
+
collectAttributeNames(attribute) {
|
|
68
|
+
const { kind, key } = parseAttributeName(attribute.name);
|
|
69
|
+
if (kind === 'reference') {
|
|
70
|
+
this.localNames.add(key);
|
|
71
|
+
}
|
|
72
|
+
else if (attribute.name.startsWith('let-')) {
|
|
73
|
+
this.localNames.add(attribute.name.substring(4));
|
|
74
|
+
}
|
|
75
|
+
else if (kind === 'structural') {
|
|
76
|
+
for (const match of attribute.value.matchAll(/\b(?:let|as)\s+([A-Za-z_$][\w$]*)/g)) {
|
|
77
|
+
this.localNames.add(match[1]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Migrates all `si-search-bar` elements of a template from the deprecated inputs and outputs
|
|
84
|
+
* to a content-projected `<input siSearchBarInput>`.
|
|
85
|
+
*/
|
|
86
|
+
export const migrateSearchBarTemplate = (options) => {
|
|
87
|
+
const result = {
|
|
88
|
+
content: options.template,
|
|
89
|
+
changed: false,
|
|
90
|
+
migrated: 0,
|
|
91
|
+
skipped: 0,
|
|
92
|
+
fields: []
|
|
93
|
+
};
|
|
94
|
+
if (!options.template.includes(SEARCH_BAR)) {
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
const parsed = new HtmlParser().parse(options.template, 'template.html', {
|
|
98
|
+
tokenizeExpansionForms: true,
|
|
99
|
+
tokenizeBlocks: true,
|
|
100
|
+
preserveLineEndings: true
|
|
101
|
+
});
|
|
102
|
+
if (parsed.errors.length) {
|
|
103
|
+
options.warn(`${options.location(0)}: The template could not be parsed, so its search bars were not migrated.`);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
const collector = new SearchBarCollector();
|
|
107
|
+
visitAll(collector, parsed.rootNodes);
|
|
108
|
+
const context = {
|
|
109
|
+
...options,
|
|
110
|
+
localNames: collector.localNames,
|
|
111
|
+
reservedNames: new Set(),
|
|
112
|
+
eol: options.template.includes('\r\n') ? '\r\n' : '\n'
|
|
113
|
+
};
|
|
114
|
+
const edits = [];
|
|
115
|
+
for (const usage of collector.searchBars) {
|
|
116
|
+
const location = options.location(usage.element.startSourceSpan.start.offset);
|
|
117
|
+
const outcome = migrateSearchBar(usage, context);
|
|
118
|
+
if (outcome.kind === 'skip') {
|
|
119
|
+
result.skipped++;
|
|
120
|
+
options.warn(`${location}: Skipped si-search-bar. ${outcome.reason}`);
|
|
121
|
+
}
|
|
122
|
+
else if (outcome.kind === 'migrate') {
|
|
123
|
+
result.migrated++;
|
|
124
|
+
edits.push(...outcome.edits);
|
|
125
|
+
outcome.warnings.forEach(warning => options.warn(`${location}: ${warning}`));
|
|
126
|
+
if (outcome.field) {
|
|
127
|
+
result.fields.push(outcome.field);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (edits.length) {
|
|
132
|
+
result.content = applyTextEdits(options.template, edits);
|
|
133
|
+
result.changed = result.content !== options.template;
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
};
|
|
137
|
+
const migrateSearchBar = (usage, context) => {
|
|
138
|
+
const { element } = usage;
|
|
139
|
+
const attributes = element.attrs.map((attribute, order) => ({
|
|
140
|
+
attribute,
|
|
141
|
+
...parseAttributeName(attribute.name),
|
|
142
|
+
text: getSourceText(context.template, attribute),
|
|
143
|
+
order
|
|
144
|
+
}));
|
|
145
|
+
const searchChange = attributes.find(attribute => attribute.kind === 'event' && attribute.key === 'searchChange');
|
|
146
|
+
const deprecatedInputs = attributes.filter(attribute => isInput(attribute, ...DEPRECATED_INPUTS));
|
|
147
|
+
const prohibitedCharacters = findInput(attributes, 'prohibitedCharacters');
|
|
148
|
+
const formKind = attributes.some(attribute => isInput(attribute, 'formField'))
|
|
149
|
+
? 'signal'
|
|
150
|
+
: attributes.some(attribute => isInput(attribute, 'ngModel', 'formControlName', 'formControl') ||
|
|
151
|
+
(attribute.kind === 'twoWay' && attribute.key === 'ngModel'))
|
|
152
|
+
? 'classic'
|
|
153
|
+
: 'none';
|
|
154
|
+
const hasTypeahead = attributes.some(attribute => isInput(attribute, 'siTypeahead'));
|
|
155
|
+
const movable = attributes.filter(attribute => isMovableAttribute(attribute, formKind, hasTypeahead));
|
|
156
|
+
const hasDeprecatedUsage = !!searchChange ||
|
|
157
|
+
deprecatedInputs.length > 0 ||
|
|
158
|
+
attributes.some(attribute => isInput(attribute, ...BEHAVIOR_INPUTS));
|
|
159
|
+
const projectsInput = element.children.some(child => child instanceof Element &&
|
|
160
|
+
child.name === 'input' &&
|
|
161
|
+
child.attrs.some(attribute => attribute.name === INPUT_DIRECTIVE_ATTRIBUTE));
|
|
162
|
+
if (projectsInput) {
|
|
163
|
+
return hasDeprecatedUsage
|
|
164
|
+
? {
|
|
165
|
+
kind: 'skip',
|
|
166
|
+
reason: 'It already projects an input with `siSearchBarInput`. Move its remaining deprecated inputs and outputs to that input manually.'
|
|
167
|
+
}
|
|
168
|
+
: { kind: 'none' };
|
|
169
|
+
}
|
|
170
|
+
if (!hasDeprecatedUsage && !movable.length) {
|
|
171
|
+
return { kind: 'none' };
|
|
172
|
+
}
|
|
173
|
+
if (context.component?.directiveImportBlocker) {
|
|
174
|
+
return {
|
|
175
|
+
kind: 'skip',
|
|
176
|
+
reason: `\`SiSearchBarInputDirective\` cannot be added to the component imports because ${context.component.directiveImportBlocker}. ${MANUAL_MIGRATION_HINT}`
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (element.comments?.length || element.directives?.length) {
|
|
180
|
+
return {
|
|
181
|
+
kind: 'skip',
|
|
182
|
+
reason: `Its start tag contains comments or selectorless directives. ${MANUAL_MIGRATION_HINT}`
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (searchChange) {
|
|
186
|
+
if (formKind !== 'none') {
|
|
187
|
+
return {
|
|
188
|
+
kind: 'skip',
|
|
189
|
+
reason: `It uses \`(searchChange)\` together with a form binding. ${MANUAL_MIGRATION_HINT}`
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return migrateToSignalForm(usage, attributes, movable, searchChange, context);
|
|
193
|
+
}
|
|
194
|
+
if (formKind !== 'none' &&
|
|
195
|
+
prohibitedCharacters &&
|
|
196
|
+
getLiteral(prohibitedCharacters)?.value !== '') {
|
|
197
|
+
return {
|
|
198
|
+
kind: 'skip',
|
|
199
|
+
reason: `It uses \`prohibitedCharacters\` together with a form binding. Add a pattern validator to the form control and ignore invalid values instead. ${MANUAL_MIGRATION_HINT}`
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (formKind === 'signal') {
|
|
203
|
+
// Signal Forms manage these properties of a form field input.
|
|
204
|
+
const restricted = deprecatedInputs.filter(attribute => attribute.key !== 'placeholder');
|
|
205
|
+
if (restricted.length) {
|
|
206
|
+
const names = restricted.map(attribute => `\`${attribute.key}\``).join(', ');
|
|
207
|
+
return {
|
|
208
|
+
kind: 'skip',
|
|
209
|
+
reason: `Signal Forms do not allow ${names} on an input with \`[formField]\`. Configure them in the form schema instead. ${MANUAL_MIGRATION_HINT}`
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const inputAttributes = [
|
|
214
|
+
...movable.map(toInputAttribute),
|
|
215
|
+
...deprecatedInputs.map(attribute => ({
|
|
216
|
+
...toInputAttribute(attribute),
|
|
217
|
+
text: migrateDeprecatedInput(attribute, formKind, context)
|
|
218
|
+
}))
|
|
219
|
+
];
|
|
220
|
+
const warnings = [];
|
|
221
|
+
if (formKind !== 'none') {
|
|
222
|
+
const debounceTimeAttribute = findInput(attributes, 'debounceTime');
|
|
223
|
+
const debounceTime = getDebounceTime(debounceTimeAttribute);
|
|
224
|
+
if (debounceTime !== 0) {
|
|
225
|
+
const duration = debounceTime ?? `\`${debounceTimeAttribute.attribute.value.trim()}\``;
|
|
226
|
+
warnings.push(`The search bar debounced form value changes by ${duration} ms. The projected input updates the form value immediately. Debounce the value changes in your code if needed.`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const consumed = attributes.filter(attribute => isInput(attribute, ...BEHAVIOR_INPUTS));
|
|
230
|
+
return {
|
|
231
|
+
kind: 'migrate',
|
|
232
|
+
edits: renderMigration(element, attributes, inputAttributes, consumed, context),
|
|
233
|
+
warnings
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
const migrateToSignalForm = (usage, attributes, movable, searchChange, context) => {
|
|
237
|
+
const skip = (reason) => ({
|
|
238
|
+
kind: 'skip',
|
|
239
|
+
reason: `${reason} ${MANUAL_MIGRATION_HINT}`
|
|
240
|
+
});
|
|
241
|
+
const component = context.component;
|
|
242
|
+
if (!component || component.generationBlocker) {
|
|
243
|
+
return skip(`Its \`(searchChange)\` output is replaced with a Signal Forms field in the component class, but ${component?.generationBlocker ?? 'the component class could not be determined'}.`);
|
|
244
|
+
}
|
|
245
|
+
const repeatedContext = getRepeatedContext(usage);
|
|
246
|
+
if (repeatedContext) {
|
|
247
|
+
return skip(`It is inside ${repeatedContext}, so one Signal Forms field cannot replace its \`(searchChange)\` output.`);
|
|
248
|
+
}
|
|
249
|
+
const conditionalContext = getConditionalContext(usage);
|
|
250
|
+
if (conditionalContext) {
|
|
251
|
+
return skip(`It is inside ${conditionalContext}, where its \`(searchChange)\` handler may depend on template type narrowing.`);
|
|
252
|
+
}
|
|
253
|
+
const rules = [];
|
|
254
|
+
const debounceTime = getDebounceTime(findInput(attributes, 'debounceTime'));
|
|
255
|
+
if (debounceTime === undefined) {
|
|
256
|
+
return skip('Its `debounceTime` is not a constant number.');
|
|
257
|
+
}
|
|
258
|
+
if (debounceTime > 0) {
|
|
259
|
+
rules.push({ fn: 'debounce', args: [`${debounceTime}`] });
|
|
260
|
+
}
|
|
261
|
+
for (const name of ['disabled', 'readonly']) {
|
|
262
|
+
const attribute = findInput(attributes, name);
|
|
263
|
+
const literal = attribute && getLiteral(attribute);
|
|
264
|
+
if (!attribute || (literal && !booleanAttribute(literal.value))) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (literal) {
|
|
268
|
+
rules.push({ fn: name, args: [] });
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const signalRead = getSignalRead(attribute, context);
|
|
272
|
+
if (!signalRead || (!signalRead.negated && signalRead.valueKind !== 'boolean')) {
|
|
273
|
+
return skip(`Its \`${name}\` input is neither a constant nor a boolean signal.`);
|
|
274
|
+
}
|
|
275
|
+
rules.push({ fn: name, args: [`{ when: () => ${signalRead.expression} }`] });
|
|
276
|
+
}
|
|
277
|
+
const maxlength = findInput(attributes, 'maxlength');
|
|
278
|
+
if (maxlength) {
|
|
279
|
+
const literal = getLiteral(maxlength);
|
|
280
|
+
const signalRead = literal ? undefined : getSignalRead(maxlength, context);
|
|
281
|
+
if (literal) {
|
|
282
|
+
const value = numberAttribute(literal.value);
|
|
283
|
+
if (!Number.isNaN(value)) {
|
|
284
|
+
rules.push({ fn: 'maxLength', args: [`${value}`] });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else if (signalRead && !signalRead.negated && signalRead.valueKind === 'number') {
|
|
288
|
+
rules.push({ fn: 'maxLength', args: [`() => ${signalRead.expression}`] });
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
return skip('Its `maxlength` input is neither a constant nor a number signal.');
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
let validate = false;
|
|
295
|
+
const prohibitedCharacters = findInput(attributes, 'prohibitedCharacters');
|
|
296
|
+
if (prohibitedCharacters) {
|
|
297
|
+
const literal = getLiteral(prohibitedCharacters);
|
|
298
|
+
if (!literal || (literal.value != null && typeof literal.value !== 'string')) {
|
|
299
|
+
return skip('Its `prohibitedCharacters` input is not a constant string.');
|
|
300
|
+
}
|
|
301
|
+
if (literal.value) {
|
|
302
|
+
rules.push({
|
|
303
|
+
fn: 'pattern',
|
|
304
|
+
args: [createProhibitedCharactersPattern(literal.value)]
|
|
305
|
+
});
|
|
306
|
+
validate = true;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
let initialValue = `''`;
|
|
310
|
+
const value = findInput(attributes, 'value');
|
|
311
|
+
if (value) {
|
|
312
|
+
const literal = getLiteral(value);
|
|
313
|
+
if (!literal || (literal.value != null && typeof literal.value !== 'string')) {
|
|
314
|
+
return skip('Its `value` input is not a constant string.');
|
|
315
|
+
}
|
|
316
|
+
initialValue = toStringLiteral(literal.value ?? '');
|
|
317
|
+
}
|
|
318
|
+
const handler = convertAction(searchChange.attribute.value, context.localNames);
|
|
319
|
+
if (typeof handler === 'string') {
|
|
320
|
+
return skip(`Its \`(searchChange)\` handler ${handler}.`);
|
|
321
|
+
}
|
|
322
|
+
const { termName, fieldName } = reserveFieldNames(component, context);
|
|
323
|
+
const field = {
|
|
324
|
+
termName,
|
|
325
|
+
fieldName,
|
|
326
|
+
initialValue,
|
|
327
|
+
rules,
|
|
328
|
+
statements: handler.map(statement => statement.text.replaceAll(EVENT_PLACEHOLDER, termName)),
|
|
329
|
+
expressionBody: handler.length === 1 && handler[0].isCall,
|
|
330
|
+
validate
|
|
331
|
+
};
|
|
332
|
+
const placeholder = findInput(attributes, 'placeholder');
|
|
333
|
+
const formField = `[formField]="${fieldName}"`;
|
|
334
|
+
const inputAttributes = [
|
|
335
|
+
...movable.map(toInputAttribute),
|
|
336
|
+
...(placeholder ? [toInputAttribute(placeholder)] : []),
|
|
337
|
+
{
|
|
338
|
+
kind: 'property',
|
|
339
|
+
text: context.quote ? escapeQuote(formField, context.quote) : formField,
|
|
340
|
+
order: Number.MAX_SAFE_INTEGER
|
|
341
|
+
}
|
|
342
|
+
];
|
|
343
|
+
const consumed = attributes.filter(attribute => attribute === searchChange ||
|
|
344
|
+
(attribute !== placeholder && isInput(attribute, ...BEHAVIOR_INPUTS, ...DEPRECATED_INPUTS)));
|
|
345
|
+
return {
|
|
346
|
+
kind: 'migrate',
|
|
347
|
+
edits: renderMigration(usage.element, attributes, inputAttributes, consumed, context),
|
|
348
|
+
warnings: [],
|
|
349
|
+
field
|
|
350
|
+
};
|
|
351
|
+
};
|
|
352
|
+
const reserveFieldNames = (component, context) => {
|
|
353
|
+
const isTaken = (name) => component.hasMember(name) || context.localNames.has(name) || context.reservedNames.has(name);
|
|
354
|
+
for (let index = 1;; index++) {
|
|
355
|
+
const suffix = index === 1 ? '' : `${index}`;
|
|
356
|
+
const termName = `searchTerm${suffix}`;
|
|
357
|
+
const fieldName = `searchField${suffix}`;
|
|
358
|
+
if (!isTaken(termName) && !isTaken(fieldName)) {
|
|
359
|
+
context.reservedNames.add(termName);
|
|
360
|
+
context.reservedNames.add(fieldName);
|
|
361
|
+
return { termName, fieldName };
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
const isInput = (attribute, ...keys) => (attribute.kind === 'static' || attribute.kind === 'property') && keys.includes(attribute.key);
|
|
366
|
+
const findInput = (attributes, key) => attributes.find(attribute => isInput(attribute, key));
|
|
367
|
+
const toInputAttribute = (attribute) => ({
|
|
368
|
+
text: attribute.text,
|
|
369
|
+
kind: attribute.kind,
|
|
370
|
+
order: attribute.order,
|
|
371
|
+
source: attribute
|
|
372
|
+
});
|
|
373
|
+
const isMovableAttribute = (attribute, formKind, hasTypeahead) => {
|
|
374
|
+
const { kind, key } = attribute;
|
|
375
|
+
if (kind === 'reference') {
|
|
376
|
+
const exportName = attribute.attribute.value;
|
|
377
|
+
return ((formKind === 'classic' && exportName === 'ngModel') ||
|
|
378
|
+
(hasTypeahead && exportName === 'si-typeahead'));
|
|
379
|
+
}
|
|
380
|
+
if ((kind === 'static' || kind === 'attribute') && key.startsWith('aria-')) {
|
|
381
|
+
return key !== 'aria-hidden';
|
|
382
|
+
}
|
|
383
|
+
if ((kind === 'static' || kind === 'property' || kind === 'event') &&
|
|
384
|
+
(key === 'siTypeahead' || key.startsWith('typeahead'))) {
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
if (formKind === 'signal') {
|
|
388
|
+
return kind === 'property' && key === 'formField';
|
|
389
|
+
}
|
|
390
|
+
if (formKind === 'classic') {
|
|
391
|
+
return (((kind === 'static' || kind === 'property' || kind === 'twoWay') &&
|
|
392
|
+
CLASSIC_FORM_INPUTS.includes(key)) ||
|
|
393
|
+
(kind === 'event' && key === 'ngModelChange'));
|
|
394
|
+
}
|
|
395
|
+
return false;
|
|
396
|
+
};
|
|
397
|
+
const migrateDeprecatedInput = (attribute, formKind, context) => {
|
|
398
|
+
const { key, kind, text } = attribute;
|
|
399
|
+
const { keySpan, valueSpan, sourceSpan } = attribute.attribute;
|
|
400
|
+
if (kind !== 'property' || !keySpan || !valueSpan) {
|
|
401
|
+
return text;
|
|
402
|
+
}
|
|
403
|
+
if (key === 'maxlength' && formKind !== 'classic') {
|
|
404
|
+
// `maxlength` is no DOM property, so only an attribute binding compiles on a native input.
|
|
405
|
+
return `[attr.maxlength]${context.template.substring(keySpan.end.offset, sourceSpan.end.offset)}`;
|
|
406
|
+
}
|
|
407
|
+
if (key === 'value' && needsEmptyStringFallback(attribute, context)) {
|
|
408
|
+
// The search bar displayed `null` and `undefined` as an empty input.
|
|
409
|
+
const name = context.template.substring(sourceSpan.start.offset, keySpan.end.offset);
|
|
410
|
+
const closingQuote = context.template[valueSpan.end.offset];
|
|
411
|
+
const quote = closingQuote === "'" || closingQuote === '"' ? closingQuote : '"';
|
|
412
|
+
const emptyString = quote === '"' ? "''" : '""';
|
|
413
|
+
const expression = context.template.substring(valueSpan.start.offset, valueSpan.end.offset);
|
|
414
|
+
const parsed = parseBinding(attribute.attribute.value);
|
|
415
|
+
const isSimple = parsed instanceof PropertyRead ||
|
|
416
|
+
parsed instanceof SafePropertyRead ||
|
|
417
|
+
parsed instanceof Call;
|
|
418
|
+
const fallback = ` ?? ${context.quote ? escapeQuote(emptyString, context.quote) : emptyString}`;
|
|
419
|
+
return `${name}=${quote}${isSimple ? expression : `(${expression})`}${fallback}${quote}`;
|
|
420
|
+
}
|
|
421
|
+
return text;
|
|
422
|
+
};
|
|
423
|
+
const needsEmptyStringFallback = (attribute, context) => {
|
|
424
|
+
const parsed = parseBinding(attribute.attribute.value);
|
|
425
|
+
if (parsed instanceof LiteralPrimitive) {
|
|
426
|
+
return parsed.value == null;
|
|
427
|
+
}
|
|
428
|
+
const memberAccess = getMemberAccess(parsed, context);
|
|
429
|
+
return !(memberAccess && context.component?.isNonNullableString(memberAccess.name, memberAccess.call));
|
|
430
|
+
};
|
|
431
|
+
const getMemberAccess = (ast, context) => {
|
|
432
|
+
const call = ast instanceof Call && ast.args.length === 0;
|
|
433
|
+
const read = ast instanceof Call ? (call ? ast.receiver : undefined) : ast;
|
|
434
|
+
if (read instanceof PropertyRead &&
|
|
435
|
+
!read.name.startsWith('$') &&
|
|
436
|
+
(read.receiver instanceof ThisReceiver ||
|
|
437
|
+
(read.receiver instanceof ImplicitReceiver && !context.localNames.has(read.name)))) {
|
|
438
|
+
return { name: read.name, call };
|
|
439
|
+
}
|
|
440
|
+
return undefined;
|
|
441
|
+
};
|
|
442
|
+
const getSignalRead = (attribute, context) => {
|
|
443
|
+
if (attribute.kind !== 'property') {
|
|
444
|
+
return undefined;
|
|
445
|
+
}
|
|
446
|
+
let ast = parseBinding(attribute.attribute.value);
|
|
447
|
+
const negated = ast instanceof PrefixNot;
|
|
448
|
+
if (ast instanceof PrefixNot) {
|
|
449
|
+
ast = ast.expression;
|
|
450
|
+
}
|
|
451
|
+
const memberAccess = getMemberAccess(ast, context);
|
|
452
|
+
const valueKind = memberAccess?.call
|
|
453
|
+
? context.component?.signalValueKind(memberAccess.name)
|
|
454
|
+
: undefined;
|
|
455
|
+
return memberAccess && valueKind
|
|
456
|
+
? { expression: `${negated ? '!' : ''}this.${memberAccess.name}()`, negated, valueKind }
|
|
457
|
+
: undefined;
|
|
458
|
+
};
|
|
459
|
+
const getRepeatedContext = (usage) => {
|
|
460
|
+
for (const node of [...usage.ancestors, usage.element]) {
|
|
461
|
+
if (node instanceof Block) {
|
|
462
|
+
if (node.name === 'for') {
|
|
463
|
+
return 'a `@for` block';
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
else if (node.name === 'ng-template') {
|
|
467
|
+
return 'an `<ng-template>`';
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
const structural = node.attrs.find(attribute => attribute.name.startsWith('*') && attribute.name !== '*ngIf');
|
|
471
|
+
if (structural) {
|
|
472
|
+
return `an element with \`${structural.name}\``;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return undefined;
|
|
477
|
+
};
|
|
478
|
+
const getConditionalContext = (usage) => {
|
|
479
|
+
for (const node of [...usage.ancestors, usage.element]) {
|
|
480
|
+
if (node instanceof Block) {
|
|
481
|
+
if (['if', 'else if', 'else', 'switch', 'case', 'default'].includes(node.name)) {
|
|
482
|
+
return `a \`@${node.name}\` block`;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
else if (node.attrs.some(attribute => attribute.name === '*ngIf')) {
|
|
486
|
+
return 'an element with `*ngIf`';
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return undefined;
|
|
490
|
+
};
|
|
491
|
+
const renderMigration = (element, attributes, inputAttributes, consumed, context) => {
|
|
492
|
+
// i18n metadata moves together with the static attribute that it translates.
|
|
493
|
+
const movedStaticAttributes = new Set(inputAttributes
|
|
494
|
+
.filter(attribute => attribute.source?.kind === 'static')
|
|
495
|
+
.map(attribute => attribute.source.key));
|
|
496
|
+
const allInputAttributes = [
|
|
497
|
+
...inputAttributes,
|
|
498
|
+
...attributes
|
|
499
|
+
.filter(attribute => attribute.kind === 'i18n' && movedStaticAttributes.has(attribute.key))
|
|
500
|
+
.map(toInputAttribute)
|
|
501
|
+
];
|
|
502
|
+
const moved = new Set(allInputAttributes.map(attribute => attribute.source));
|
|
503
|
+
const hostAttributes = attributes.filter(attribute => !moved.has(attribute) && !consumed.includes(attribute));
|
|
504
|
+
const { template, eol } = context;
|
|
505
|
+
const multiline = !context.quote;
|
|
506
|
+
const start = element.startSourceSpan.start.offset;
|
|
507
|
+
const indent = getIndentation(template, start);
|
|
508
|
+
const inputIndent = `${indent} `;
|
|
509
|
+
const hostTag = renderStartTag(SEARCH_BAR, hostAttributes.map(attribute => attribute.text), indent, '>', multiline, eol);
|
|
510
|
+
const inputTag = renderStartTag('input', sortInputAttributes([
|
|
511
|
+
{ kind: 'static', text: INPUT_DIRECTIVE_ATTRIBUTE, order: -1 },
|
|
512
|
+
...allInputAttributes
|
|
513
|
+
]), inputIndent, '/>', multiline, eol);
|
|
514
|
+
const separator = multiline ? `${eol}${inputIndent}` : '';
|
|
515
|
+
const closingTag = multiline ? `${eol}${indent}</${SEARCH_BAR}>` : `</${SEARCH_BAR}>`;
|
|
516
|
+
const endTag = element.isSelfClosing ? null : element.endSourceSpan;
|
|
517
|
+
const content = endTag
|
|
518
|
+
? template.substring(element.startSourceSpan.end.offset, endTag.start.offset)
|
|
519
|
+
: '';
|
|
520
|
+
if (endTag && content.trim()) {
|
|
521
|
+
return [
|
|
522
|
+
{ start, end: element.startSourceSpan.end.offset, text: `${hostTag}${separator}${inputTag}` }
|
|
523
|
+
];
|
|
524
|
+
}
|
|
525
|
+
return [
|
|
526
|
+
{
|
|
527
|
+
start,
|
|
528
|
+
end: endTag?.end.offset ?? element.startSourceSpan.end.offset,
|
|
529
|
+
text: `${hostTag}${separator}${inputTag}${closingTag}`
|
|
530
|
+
}
|
|
531
|
+
];
|
|
532
|
+
};
|
|
533
|
+
/** Orders attributes like the `@angular-eslint/template/attributes-order` rule does. */
|
|
534
|
+
const sortInputAttributes = (attributes) => {
|
|
535
|
+
const rank = {
|
|
536
|
+
structural: 0,
|
|
537
|
+
reference: 1,
|
|
538
|
+
static: 2,
|
|
539
|
+
i18n: 2,
|
|
540
|
+
other: 2,
|
|
541
|
+
attribute: 3,
|
|
542
|
+
property: 3,
|
|
543
|
+
twoWay: 4,
|
|
544
|
+
event: 5
|
|
545
|
+
};
|
|
546
|
+
return [...attributes]
|
|
547
|
+
.sort((left, right) => rank[left.kind] - rank[right.kind] || left.order - right.order)
|
|
548
|
+
.map(attribute => attribute.text);
|
|
549
|
+
};
|
|
550
|
+
const renderStartTag = (name, attributes, indent, closing, multiline, eol) => {
|
|
551
|
+
const inline = `<${name}${attributes.map(attribute => ` ${attribute}`).join('')}${closing === '/>' ? ' />' : '>'}`;
|
|
552
|
+
if (!multiline ||
|
|
553
|
+
(indent.length + inline.length <= MAX_LINE_LENGTH &&
|
|
554
|
+
!attributes.some(attribute => attribute.includes('\n')))) {
|
|
555
|
+
return inline;
|
|
556
|
+
}
|
|
557
|
+
return `<${name}${attributes
|
|
558
|
+
.map(attribute => `${eol}${indent} ${attribute}`)
|
|
559
|
+
.join('')}${eol}${indent}${closing}`;
|
|
560
|
+
};
|
|
561
|
+
const getIndentation = (template, offset) => {
|
|
562
|
+
const lineStart = template.lastIndexOf('\n', offset - 1) + 1;
|
|
563
|
+
return /^[ \t]*/.exec(template.substring(lineStart, offset))[0];
|
|
564
|
+
};
|
|
565
|
+
const getSourceText = (template, attribute) => template.substring(attribute.sourceSpan.start.offset, attribute.sourceSpan.end.offset);
|
|
566
|
+
const parseAttributeName = (name) => {
|
|
567
|
+
if (name.startsWith('[(') && name.endsWith(')]')) {
|
|
568
|
+
return { kind: 'twoWay', key: name.slice(2, -2) };
|
|
569
|
+
}
|
|
570
|
+
if (name.startsWith('bindon-')) {
|
|
571
|
+
return { kind: 'twoWay', key: name.substring(7) };
|
|
572
|
+
}
|
|
573
|
+
const property = name.startsWith('[') && name.endsWith(']')
|
|
574
|
+
? name.slice(1, -1)
|
|
575
|
+
: name.startsWith('bind-')
|
|
576
|
+
? name.substring(5)
|
|
577
|
+
: undefined;
|
|
578
|
+
if (property !== undefined) {
|
|
579
|
+
if (property.startsWith('attr.')) {
|
|
580
|
+
return { kind: 'attribute', key: property.substring(5) };
|
|
581
|
+
}
|
|
582
|
+
return /^(class|style)\.|^@/.test(property)
|
|
583
|
+
? { kind: 'other', key: property }
|
|
584
|
+
: { kind: 'property', key: property };
|
|
585
|
+
}
|
|
586
|
+
if (name.startsWith('(') && name.endsWith(')')) {
|
|
587
|
+
return { kind: 'event', key: name.slice(1, -1) };
|
|
588
|
+
}
|
|
589
|
+
if (name.startsWith('on-')) {
|
|
590
|
+
return { kind: 'event', key: name.substring(3) };
|
|
591
|
+
}
|
|
592
|
+
if (name.startsWith('#')) {
|
|
593
|
+
return { kind: 'reference', key: name.substring(1) };
|
|
594
|
+
}
|
|
595
|
+
if (name.startsWith('ref-')) {
|
|
596
|
+
return { kind: 'reference', key: name.substring(4) };
|
|
597
|
+
}
|
|
598
|
+
if (name.startsWith('*')) {
|
|
599
|
+
return { kind: 'structural', key: name.substring(1) };
|
|
600
|
+
}
|
|
601
|
+
if (name.startsWith('i18n-')) {
|
|
602
|
+
return { kind: 'i18n', key: name.substring(5) };
|
|
603
|
+
}
|
|
604
|
+
if (name === 'i18n' || name.startsWith('let-') || name.startsWith('@')) {
|
|
605
|
+
return { kind: 'other', key: name };
|
|
606
|
+
}
|
|
607
|
+
return { kind: 'static', key: name };
|
|
608
|
+
};
|
|
609
|
+
/** Returns the effective debounce time, or `undefined` if it is not constant. */
|
|
610
|
+
const getDebounceTime = (attribute) => {
|
|
611
|
+
if (!attribute) {
|
|
612
|
+
return DEFAULT_DEBOUNCE_TIME;
|
|
613
|
+
}
|
|
614
|
+
const literal = getLiteral(attribute);
|
|
615
|
+
if (!literal) {
|
|
616
|
+
return undefined;
|
|
617
|
+
}
|
|
618
|
+
// The search bar only debounced positive times.
|
|
619
|
+
const debounceTime = numberAttribute(literal.value);
|
|
620
|
+
return debounceTime > 0 ? debounceTime : 0;
|
|
621
|
+
};
|
|
622
|
+
/** Returns the constant value of a static attribute or of a binding to a literal. */
|
|
623
|
+
const getLiteral = (attribute) => {
|
|
624
|
+
if (attribute.kind === 'static') {
|
|
625
|
+
return /\{\{[\s\S]*\}\}/.test(attribute.attribute.value)
|
|
626
|
+
? undefined
|
|
627
|
+
: { value: attribute.attribute.value };
|
|
628
|
+
}
|
|
629
|
+
const parsed = parseBinding(attribute.attribute.value);
|
|
630
|
+
return parsed instanceof LiteralPrimitive ? { value: parsed.value } : undefined;
|
|
631
|
+
};
|
|
632
|
+
/** Same as `booleanAttribute` of `@angular/core`. */
|
|
633
|
+
const booleanAttribute = (value) => typeof value === 'boolean' ? value : value != null && value !== 'false';
|
|
634
|
+
/** Same as `numberAttribute` of `@angular/core`. */
|
|
635
|
+
const numberAttribute = (value) => !Number.isNaN(Number.parseFloat(value)) && !Number.isNaN(Number(value))
|
|
636
|
+
? Number(value)
|
|
637
|
+
: Number.NaN;
|
|
638
|
+
const createProhibitedCharactersPattern = (characters) => {
|
|
639
|
+
const unique = [...new Set(characters)];
|
|
640
|
+
const unicode = unique.some(character => character.codePointAt(0) > 0xffff);
|
|
641
|
+
const escaped = unique
|
|
642
|
+
.map(character => {
|
|
643
|
+
if ('\\]^-[/'.includes(character)) {
|
|
644
|
+
return `\\${character}`;
|
|
645
|
+
}
|
|
646
|
+
const code = character.codePointAt(0);
|
|
647
|
+
return code < 0x20 || code === 0x7f || code === 0x2028 || code === 0x2029
|
|
648
|
+
? `\\u${code.toString(16).padStart(4, '0')}`
|
|
649
|
+
: character;
|
|
650
|
+
})
|
|
651
|
+
.join('');
|
|
652
|
+
return `/^[^${escaped}]*$/${unicode ? 'u' : ''}`;
|
|
653
|
+
};
|
|
654
|
+
const toStringLiteral = (value) => `'${value
|
|
655
|
+
.replaceAll('\\', '\\\\')
|
|
656
|
+
.replaceAll("'", "\\'")
|
|
657
|
+
.replaceAll('\n', '\\n')
|
|
658
|
+
.replaceAll('\r', '\\r')
|
|
659
|
+
.replaceAll('\u2028', '\\u2028')
|
|
660
|
+
.replaceAll('\u2029', '\\u2029')}'`;
|
|
661
|
+
const escapeQuote = (text, quote) => text.replaceAll(quote, `\\${quote}`);
|
|
662
|
+
const expressionParser = new Parser(new Lexer());
|
|
663
|
+
const createSourceSpan = (source) => {
|
|
664
|
+
const file = new ParseSourceFile(source, 'expression');
|
|
665
|
+
return new ParseSourceSpan(new ParseLocation(file, 0, 0, 0), new ParseLocation(file, source.length, 0, source.length));
|
|
666
|
+
};
|
|
667
|
+
const unwrap = (ast) => (ast instanceof ASTWithSource ? unwrap(ast.ast) : ast);
|
|
668
|
+
const parseBinding = (source) => {
|
|
669
|
+
const result = expressionParser.parseBinding(source, createSourceSpan(source), 0);
|
|
670
|
+
return result.errors.length ? undefined : unwrap(result.ast);
|
|
671
|
+
};
|
|
672
|
+
const EVENT_PLACEHOLDER = '\u0000event\u0000';
|
|
673
|
+
/**
|
|
674
|
+
* Converts an event binding into statements for the component class by prefixing component
|
|
675
|
+
* members with `this.`. `$event` is replaced with a placeholder.
|
|
676
|
+
* Returns a description of the problem if the conversion is not possible.
|
|
677
|
+
*/
|
|
678
|
+
const convertAction = (source, localNames) => {
|
|
679
|
+
const result = expressionParser.parseAction(source, createSourceSpan(source), 0);
|
|
680
|
+
if (result.errors.length) {
|
|
681
|
+
return 'could not be parsed';
|
|
682
|
+
}
|
|
683
|
+
const edits = [];
|
|
684
|
+
let problem;
|
|
685
|
+
const visit = (node) => {
|
|
686
|
+
if (problem || !(node instanceof AST)) {
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
if (node instanceof ArrowFunction) {
|
|
690
|
+
problem = 'uses an arrow function';
|
|
691
|
+
}
|
|
692
|
+
else if (node instanceof BindingPipe) {
|
|
693
|
+
problem = 'uses a pipe';
|
|
694
|
+
}
|
|
695
|
+
else if (node instanceof LiteralMap &&
|
|
696
|
+
node.keys.some(key => key.kind === 'property' && key.isShorthandInitialized)) {
|
|
697
|
+
problem = 'uses a shorthand object property';
|
|
698
|
+
}
|
|
699
|
+
else if ((node instanceof PropertyRead || node instanceof SafePropertyRead) &&
|
|
700
|
+
node.receiver instanceof ImplicitReceiver &&
|
|
701
|
+
!(node.receiver instanceof ThisReceiver)) {
|
|
702
|
+
if (node.name === '$event') {
|
|
703
|
+
edits.push({ start: node.nameSpan.start, end: node.nameSpan.end, text: EVENT_PLACEHOLDER });
|
|
704
|
+
}
|
|
705
|
+
else if (node.name.startsWith('$')) {
|
|
706
|
+
problem = `uses \`${node.name}\``;
|
|
707
|
+
}
|
|
708
|
+
else if (localNames.has(node.name)) {
|
|
709
|
+
problem = `uses the template variable \`${node.name}\``;
|
|
710
|
+
}
|
|
711
|
+
else {
|
|
712
|
+
edits.push({ start: node.nameSpan.start, end: node.nameSpan.start, text: 'this.' });
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
for (const value of Object.values(node)) {
|
|
716
|
+
if (Array.isArray(value)) {
|
|
717
|
+
value.forEach(visit);
|
|
718
|
+
}
|
|
719
|
+
else {
|
|
720
|
+
visit(value);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
const ast = unwrap(result.ast);
|
|
725
|
+
visit(ast);
|
|
726
|
+
if (problem) {
|
|
727
|
+
return problem;
|
|
728
|
+
}
|
|
729
|
+
const statements = (ast instanceof Chain ? ast.expressions : [ast])
|
|
730
|
+
.filter(statement => !(statement instanceof EmptyExpr))
|
|
731
|
+
.map(statement => {
|
|
732
|
+
const { start, end } = statement.sourceSpan;
|
|
733
|
+
const statementEdits = edits
|
|
734
|
+
.filter(edit => edit.start >= start && edit.end <= end)
|
|
735
|
+
.map(edit => ({ ...edit, start: edit.start - start, end: edit.end - start }));
|
|
736
|
+
return {
|
|
737
|
+
text: applyTextEdits(source.substring(start, end), statementEdits).trim(),
|
|
738
|
+
isCall: statement instanceof Call || statement instanceof SafeCall
|
|
739
|
+
};
|
|
740
|
+
});
|
|
741
|
+
return statements.length ? statements : 'is empty';
|
|
742
|
+
};
|