-
-
Notifications
You must be signed in to change notification settings - Fork 236
Show compiler errors #869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show compiler errors #869
Changes from all commits
3076b59
dd3514a
4a98852
e258c52
9135d4b
3f71897
17511a9
2df2a7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,13 +2,15 @@ import { compileAllBlocks } from '../../compiler/compile-blocks'; | |||||||||||||||||||||||
| import { createImportMap, replaceSFCImports } from '../../compiler/import-map'; | ||||||||||||||||||||||||
| import { getCompileResult } from '../../compiler/utils'; | ||||||||||||||||||||||||
| import type { CompilerFunction, Config, Language } from '../../models'; | ||||||||||||||||||||||||
| import { getErrorMessage } from '../../utils/utils'; | ||||||||||||||||||||||||
| import { getLanguageByAlias, getLanguageCustomSettings } from '../utils'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| (self as any).createSvelteCompiler = (): CompilerFunction => { | ||||||||||||||||||||||||
| const MAIN_FILE = '__LiveCodes_App__.svelte'; | ||||||||||||||||||||||||
| const SECONDARY_FILE = '__LiveCodes_Component__.svelte'; | ||||||||||||||||||||||||
| let importedContent = ''; | ||||||||||||||||||||||||
| let imports: Record<string, string> = {}; | ||||||||||||||||||||||||
| let errors: string[] = []; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const compileSvelteSFC = async ( | ||||||||||||||||||||||||
| code: string, | ||||||||||||||||||||||||
|
|
@@ -42,21 +44,32 @@ import { getLanguageByAlias, getLanguageCustomSettings } from '../utils'; | |||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| const customSettings = getLanguageCustomSettings('svelte', config); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const { js } = (window as any).svelte.compile(processedCode, { | ||||||||||||||||||||||||
| css: 'injected', | ||||||||||||||||||||||||
| filename, | ||||||||||||||||||||||||
| ...customSettings, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (filename === MAIN_FILE || filename === SECONDARY_FILE) { | ||||||||||||||||||||||||
| imports = createImportMap(importedContent, config); | ||||||||||||||||||||||||
| errors = []; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let js: { code: string }; | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| const result = (window as any).svelte.compile(processedCode, { | ||||||||||||||||||||||||
| css: 'injected', | ||||||||||||||||||||||||
| filename, | ||||||||||||||||||||||||
| ...customSettings, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Comment on lines
+54
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainStabilize component name for mount(). Pass name to svelte.compile so getMountCode’s LiveCodes_App symbol is guaranteed across Svelte versions. const result = (window as any).svelte.compile(processedCode, {
css: 'injected',
filename,
+ name: filename.replace(/\.svelte$/,'').replace(/[^A-Za-z0-9_$]/g, '_'),
...customSettings,
});🌐 Web query: 💡 Result: Yes.
Sources: Stabilize component name for mount(). Svelte supports the compile option File: src/livecodes/languages/svelte/lang-svelte-compiler.ts — Lines: 54-58 const result = (window as any).svelte.compile(processedCode, {
css: 'injected',
filename,
+ name: filename.replace(/\.svelte$/,'').replace(/[^A-Za-z0-9_$]/g, '_'),
...customSettings,
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| js = result.js; | ||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||
| errors.push(getErrorMessage(err)); | ||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| code: '', | ||||||||||||||||||||||||
| info: { errors }, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
Comment on lines
+62
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Keep info shape consistent on error path. Return importedContent and imports alongside errors. - return {
- code: '',
- info: { errors },
- };
+ return {
+ code: '',
+ info: { importedContent, imports, errors },
+ };📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const compiledCode = filename === MAIN_FILE ? getMountCode(js.code) : js.code; | ||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| code: | ||||||||||||||||||||||||
| language === 'svelte-app' ? `<script type="module">${compiledCode}</script>` : compiledCode, | ||||||||||||||||||||||||
| info: { importedContent, imports }, | ||||||||||||||||||||||||
| info: { importedContent, imports, errors }, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { compileInCompiler } from '../../compiler/compile-in-compiler'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createImportMap, replaceSFCImports } from '../../compiler/import-map'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { LanguageOrProcessor } from '../../compiler/models'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { CompilerFunction, Config } from '../../models'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getRandomString, replaceAsync } from '../../utils/utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getErrorMessage, getRandomString, replaceAsync } from '../../utils/utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getLanguageByAlias } from '../utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // based on: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -363,35 +363,48 @@ import { getLanguageByAlias } from '../utils'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return async (code, { config, language }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isMainFile = config.markup.language !== 'vue-app' || language === 'vue-app'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filename = isMainFile ? MAIN_FILE : SECONDARY_FILE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await compileVueSFC(code, { config, filename }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isMainFile = config.markup.language !== 'vue-app' || language === 'vue-app'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filename = isMainFile ? MAIN_FILE : SECONDARY_FILE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await compileVueSFC(code, { config, filename }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (result) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { css, js } = result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (result) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { css, js } = result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const injectCSS = !css.trim() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| : ` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const injectCSS = !css.trim() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| : ` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.head.insertBefore( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object.assign(document.createElement('style'), { textContent: ${JSON.stringify(css)} }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.head.getElementsByTagName('style')[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const compiledCode = js + injectCSS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const compiledCode = js + injectCSS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| language === 'vue-app' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? `<script type="module">${compiledCode}</script>` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| : compiledCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| info: { importedContent, imports: createImportMap(importedContent, config), errors }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+384
to
+391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. errors contains non-strings; violates CompileInfo contract and breaks UI rendering. errors gathers objects from Vue compiler (parse/style/template). Map to strings before returning. Apply this diff: - return {
- code:
- language === 'vue-app'
- ? `<script type="module">${compiledCode}</script>`
- : compiledCode,
- info: { importedContent, imports: createImportMap(importedContent, config), errors },
- };
+ const errorMessages = errors.map(getErrorMessage).filter(Boolean);
+ return {
+ code:
+ language === 'vue-app'
+ ? `<script type="module">${compiledCode}</script>`
+ : compiledCode,
+ info: {
+ importedContent,
+ imports: createImportMap(importedContent, config),
+ errors: errorMessages,
+ },
+ };Additionally, change the declaration to enforce string accumulation (outside this hunk): // before
let errors: any[] = [];
// after
let errors: string[] = [];And when pushing compiler errors elsewhere in this file, convert to messages: // parse errors
errors.push(...err.map(getErrorMessage));
// style errors
errors.push(...styleResult.errors.map(getErrorMessage));
// template errors
errors.push(...templateResult.errors.map(getErrorMessage));🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (errors.length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(...errors); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const empty = `export default () => {}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| language === 'vue-app' ? `<script type="module">${compiledCode}</script>` : compiledCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| info: { importedContent, imports: createImportMap(importedContent, config) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| code: language === 'vue-app' ? `<script type="module">${empty}</script>` : empty, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| info: { errors }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| code: '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| info: { errors: [...errors, getErrorMessage(err)] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+398
to
407
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Normalize errors in fallback/catch paths. Ensure strings only; include prior errors. Apply this diff: - const empty = `export default () => {}`;
- return {
- code: language === 'vue-app' ? `<script type="module">${empty}</script>` : empty,
- info: { errors },
- };
+ const empty = `export default () => {}`;
+ return {
+ code: language === 'vue-app' ? `<script type="module">${empty}</script>` : empty,
+ info: { errors: errors.map(getErrorMessage).filter(Boolean) },
+ };
} catch (err) {
- return {
- code: '',
- info: { errors: [...errors, getErrorMessage(err)] },
- };
+ return {
+ code: '',
+ info: {
+ errors: [...errors.map(getErrorMessage), getErrorMessage(err)].filter(Boolean),
+ },
+ };📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (errors.length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(...errors); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset errors at the root entry, not here (prevents losing nested SFC errors) and also reset imports for SECONDARY_FILE.
Errors collected while compiling nested SFCs via replaceSFCImports are wiped here. Move the reset to the root-file preamble (for BOTH MAIN_FILE and SECONDARY_FILE) and keep this block for import map creation only.
Apply within this hunk:
imports = createImportMap(importedContent, config); - errors = [];Additionally (outside this hunk), ensure root entry resets state and handles empty code:
🤖 Prompt for AI Agents