(source, { filename, use_ts } = {})
| 122 | * @returns {{ code: string; }} |
| 123 | */ |
| 124 | export function migrate(source, { filename, use_ts } = {}) { |
| 125 | let og_source = source; |
| 126 | try { |
| 127 | has_migration_task = false; |
| 128 | // Blank CSS, could contain SCSS or similar that needs a preprocessor. |
| 129 | // Since we don't care about CSS in this migration, we'll just ignore it. |
| 130 | /** @type {Array<[number, string]>} */ |
| 131 | const style_contents = []; |
| 132 | source = source.replace(regex_style_tags, (_, start, content, end, idx) => { |
| 133 | style_contents.push([idx + start.length, content]); |
| 134 | return start + style_placeholder + end; |
| 135 | }); |
| 136 | |
| 137 | reset({ warning: () => false, filename }); |
| 138 | |
| 139 | let parsed = parse(source); |
| 140 | |
| 141 | const { customElement: customElementOptions, ...parsed_options } = parsed.options || {}; |
| 142 | |
| 143 | /** @type {ValidatedCompileOptions} */ |
| 144 | const combined_options = { |
| 145 | ...validate_component_options({}, ''), |
| 146 | ...parsed_options, |
| 147 | customElementOptions, |
| 148 | filename: filename ?? UNKNOWN_FILENAME, |
| 149 | css: 'css' in parsed_options ? () => parsed_options.css ?? 'external' : () => 'external', |
| 150 | runes: 'runes' in parsed_options ? () => parsed_options.runes : () => undefined, |
| 151 | experimental: { |
| 152 | async: true |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | const str = new MagicString(source); |
| 157 | const analysis = analyze_component(parsed, source, combined_options); |
| 158 | const indent = guess_indent(source); |
| 159 | |
| 160 | str.replaceAll(/(<svelte:options\s.*?\s?)accessors\s?/g, (_, $1) => $1); |
| 161 | |
| 162 | for (const content of style_contents) { |
| 163 | str.overwrite(content[0], content[0] + style_placeholder.length, content[1]); |
| 164 | } |
| 165 | |
| 166 | /** @type {State} */ |
| 167 | let state = { |
| 168 | scope: analysis.instance.scope, |
| 169 | analysis, |
| 170 | filename, |
| 171 | str, |
| 172 | indent, |
| 173 | props: [], |
| 174 | props_insertion_point: parsed.instance?.content.start ?? 0, |
| 175 | has_props_rune: false, |
| 176 | has_type_or_fallback: false, |
| 177 | end: source.length, |
| 178 | names: { |
| 179 | props: analysis.root.unique('props').name, |
| 180 | rest: analysis.root.unique('rest').name, |
| 181 |
no test coverage detected