notationUndeclaredNames function
The names document (a page or a component as expanded) reads, sets or
binds in its expressions that known does not hold and the document
does not declare with a line of its own (text is its notation: a name
with a state line there is declared): what the app's state file still
has to declare for the page to show it. In the order first read.
It reads the expressions as text, so it is a list to go by, not a check: a loop's item, a formula's own parameter, a field after a dot, a function and the runtime's names are left out.
With others (the texts of the files the page may call: its components,
the site's definitions), only a name the app's own writing has is listed:
one text or one of them mentions. A name the expansion made itself (the
pane a phone shows of a list beside its detail, a component's own state
kept for each call, what a row passes to its menu) is in no file, and
nothing the writer can declare. A name a state line of text gives is
declared whatever the document made of it (a worked-out value is written
out where it is read, and is no variable of the document).
drawing, when given, takes the names read only inside a drawing (a
customPaint's lines) in place of the list answered: what is read there
by a wrong name is most often a repeat's index, which is index.
Implementation
List<String> notationUndeclaredNames(
Json document, {
required String text,
Set<String> known = const {},
Iterable<String>? others,
List<String>? drawing,
}) {
Set<String> statedIn(String text) => {
for (final m in RegExp(r'^[ \t]*(?:state|let|derived|const|var|project)\s+([A-Za-z_][A-Za-z0-9_]*)', multiLine: true).allMatches(text)) m[1]!,
for (final m in RegExp(r'^([a-z_][A-Za-z0-9_]*)\s*=(?!=)', multiLine: true).allMatches(text)) m[1]!,
for (final m in RegExp(r'^[ \t]*param\s+([A-Za-z_][A-Za-z0-9_]*)', multiLine: true).allMatches(text)) m[1]!,
};
final stated = statedIn(text);
// What a component states is its own, kept by the expansion.
final statedElsewhere = {for (final o in others ?? const <String>[]) ...statedIn(o)};
// The names each and its index give the lines under them.
final loops = <String>{
for (final m in RegExp(r'^[ \t]*each\s+([A-Za-z_][A-Za-z0-9_]*)(?:\s*,\s*([A-Za-z_][A-Za-z0-9_]*))?\s+in\b', multiLine: true).allMatches(text)) ...[m[1]!, ?m[2]],
};
final declared = <String>{};
final made = <String>{};
for (final scope in (readMap(document['variables']) ?? const {}).values) {
for (final v in readList(scope) ?? const []) {
final name = readString(readMap(v)?['name']);
if (name == null) continue;
// One the document declares that no line states was made for a name
// the text sets or binds: it is still to be declared.
(stated.contains(name) ? declared : made).add(name);
}
}
final mentioned = <String, bool>{};
// (With no [others] given, what the page's own text does not mention may
// be a component's, and is listed as before.)
bool written(String name) =>
mentioned[name] ??= _mentions(text, name) || (others == null ? !made.contains(name) : others.any((o) => _mentions(o, name)));
final out = <String>{};
// The pressed row of a list with a menu (`menu=rowMenu` under `each m in
// …`): the reader keeps it in a variable of the each's name, set to the
// row's item before the menu opens, so the menu's lines read `m`. That
// variable is the reader's own: the text declared the name with its each.
final eaches = <String>{
for (final m in RegExp(r'^[ \t]*each\s+([A-Za-z_][A-Za-z0-9_]*)(?:\s*,\s*([A-Za-z_][A-Za-z0-9_]*))?\s+in\s', multiLine: true).allMatches(text)) ...[m[1]!, ?m[2]],
};
final rows = <String>{};
void pressed(Object? json) {
if (json is List) {
json.forEach(pressed);
} else if (json is Map) {
if (json['type'] == 'setVariable' && eaches.contains(json['variable']) && RegExp(r'^\$\{= (parent\.)*item\}$').hasMatch('${json['value']}')) {
rows.add(json['variable'] as String);
}
// The names a drawing's repeat gives its item and its position.
if (json['op'] == 'repeat') {
rows.addAll([for (final k in const ['as', 'indexAs']) if (json[k] case final String name) name]);
}
json.values.forEach(pressed);
}
}
pressed(document['nodes']);
final drawn = <String>{};
var inDrawing = false;
bool skip(String name) =>
known.contains(name) ||
declared.contains(name) ||
rows.contains(name) ||
stated.contains(name) ||
_notNames.contains(name) ||
!RegExp(r'^[a-z_]').hasMatch(name) ||
// In no file of the app: the expansion's own.
!written(name) ||
// Made by the expansion under the name of a loop's item (what a row
// hands the menu it opens): the loop declares it.
(made.contains(name) && loops.contains(name)) ||
(made.contains(name) && statedElsewhere.contains(name) && !_mentions(text, name));
void add(String name) => (inDrawing ? drawn : out).add(name);
void read(String expression) {
final code = expression.replaceAll(RegExp('"(?:[^"\\\\]|\\\\.)*"|\'(?:[^\'\\\\]|\\\\.)*\''), '""');
final own = <String>{
for (final m in RegExp(r'([A-Za-z_][A-Za-z0-9_]*)\s*=>').allMatches(code)) m[1]!,
for (final m in RegExp(r'\(([A-Za-z_][A-Za-z0-9_,\s]*)\)\s*=>').allMatches(code)) ...m[1]!.split(',').map((p) => p.trim()),
};
for (final m in RegExp(r'(?<![A-Za-z0-9_.$#"])([A-Za-z_][A-Za-z0-9_]*)(?![A-Za-z0-9_])').allMatches(code)) {
final name = m[1]!;
final after = code.substring(m.end).trimLeft();
if (after.startsWith('(') || own.contains(name) || skip(name)) continue;
// A record's key (`{merchant: draft}`) is a name, not a value.
final before = code.substring(0, m.start).trimRight();
if ((before.endsWith('{') || before.endsWith(',')) && RegExp(r'^:(?!:)').hasMatch(after) && !before.endsWith('?')) continue;
add(name);
}
}
void walk(Object? json) {
if (json is String) {
var at = 0;
while (true) {
final start = json.indexOf(r'${', at);
if (start < 0) break;
var depth = 0, end = -1;
for (var i = start + 1; i < json.length; i++) {
if (json[i] == '{') depth++;
if (json[i] == '}' && --depth == 0) {
end = i;
break;
}
}
if (end < 0) break;
final inner = json.substring(start + 2, end).trim();
read(inner.startsWith('=') ? inner.substring(1) : inner);
at = end + 1;
}
} else if (json is List) {
json.forEach(walk);
} else if (json is Map) {
if (json['type'] == 'setVariable' && json['variable'] is String) {
final name = (json['variable'] as String).split('.').first.split('[').first;
if (!skip(name)) add(name);
}
for (final e in json.entries) {
// A drawing's lines (a customPaint's painter and foregroundPainter).
final paint = !inDrawing && (e.key == 'painter' || e.key == 'foregroundPainter') && e.value is List;
if (paint) inDrawing = true;
walk(e.value);
if (paint) inDrawing = false;
}
}
}
walk(document['nodes']);
for (final name in made) {
if (!skip(name) && _mentions(text, name)) out.add(name);
}
drawn.removeAll(out);
if (drawing != null) {
drawing.addAll(drawn);
} else {
out.addAll(drawn);
}
return out.toList();
}