You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.6 KiB
93 lines
2.6 KiB
"""Rewrite soma/attrs imports.
|
|
|
|
For each soma file (excluding soma/attrs itself):
|
|
- find each `import { ... } from '<rel>/attrs'` or `'$soma/attrs'`
|
|
- split: morfo symbols → '$uix/morfo', dom symbols → '$libs/dom'
|
|
- emit one or two replacement imports
|
|
"""
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
MORFO_SYMBOLS = {
|
|
'createAttrs', 'AttrsOf', 'AttrsReturn',
|
|
'registerContract', 'getContract', 'assertContract',
|
|
}
|
|
DOM_SYMBOLS = {
|
|
'boolToStr', 'boolToEmptyStrOrUndef', 'boolToTrueOrUndef',
|
|
'getDataOpenClosed', 'getDataChecked', 'getAriaChecked',
|
|
}
|
|
|
|
IMPORT_RE = re.compile(
|
|
r"import\s+(type\s+)?\{([^}]+)\}\s+from\s+['\"](?:(?:\.\./)+attrs|\$soma/attrs)['\"];?",
|
|
re.MULTILINE,
|
|
)
|
|
|
|
|
|
def parse_names(block):
|
|
out = []
|
|
for raw in block.split(','):
|
|
item = raw.strip()
|
|
if not item:
|
|
continue
|
|
is_type = False
|
|
if item.startswith('type '):
|
|
is_type = True
|
|
item = item[5:].strip()
|
|
out.append((item, is_type))
|
|
return out
|
|
|
|
|
|
def render_import(items, source, has_type_import):
|
|
if not items:
|
|
return ''
|
|
type_marker = 'type ' if has_type_import else ''
|
|
parts = []
|
|
for name, is_type in items:
|
|
if has_type_import:
|
|
parts.append(name)
|
|
else:
|
|
parts.append(f'type {name}' if is_type else name)
|
|
body = ',\n\t'.join(parts)
|
|
return f"import {type_marker}{{\n\t{body}\n}} from '{source}';"
|
|
|
|
|
|
def process_match(m):
|
|
has_type_import = bool(m.group(1))
|
|
names = parse_names(m.group(2))
|
|
|
|
morfo_items = [(n, t) for (n, t) in names if n in MORFO_SYMBOLS]
|
|
dom_items = [(n, t) for (n, t) in names if n in DOM_SYMBOLS]
|
|
other_items = [(n, t) for (n, t) in names if n not in MORFO_SYMBOLS and n not in DOM_SYMBOLS]
|
|
if other_items:
|
|
print('UNKNOWN names:', other_items, file=sys.stderr)
|
|
return m.group(0)
|
|
|
|
out = []
|
|
if morfo_items:
|
|
out.append(render_import(morfo_items, '$uix/morfo', has_type_import))
|
|
if dom_items:
|
|
out.append(render_import(dom_items, '$libs/dom', has_type_import))
|
|
return '\n'.join(out)
|
|
|
|
|
|
SKIP_DIR = os.path.normpath('src/uix/soma/attrs')
|
|
|
|
changed = []
|
|
for root, dirs, files in os.walk('src/uix/soma'):
|
|
if os.path.normpath(root) == SKIP_DIR:
|
|
continue
|
|
for f in files:
|
|
if not f.endswith(('.ts', '.svelte', '.svelte.ts')):
|
|
continue
|
|
p = os.path.join(root, f)
|
|
s = open(p, 'r', encoding='utf-8').read()
|
|
new = IMPORT_RE.sub(process_match, s)
|
|
if new != s:
|
|
open(p, 'w', encoding='utf-8').write(new)
|
|
changed.append(p)
|
|
|
|
for f in changed:
|
|
print(f)
|
|
print(f'--- {len(changed)} files updated ---')
|