Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/nuedom/src/compiler/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function parseText(text, imports) {
// foo() {} --> this.foo = function() { }
export function convertFunctions(script) {
return script.replace(
/^( *)(async\s+)?(\w+)\s*\(([^)]*)\)\s*{/gm, (_, indent, asy, name, args) => {
if (_.includes('function') || ['for', 'if'].includes(name)) return _
/^(\s*)(async\s+)?(\w+)\s*\(([^)]*)\)\s*{/gm, (_, indent, asy, name, args) => {
if (_.includes('function') || ['for', 'while', 'if', 'switch'].includes(name)) return _
return `${indent}this.${name} = ${asy ? 'async ' : ''}function(${args.trim()}) {`
}
)
Expand All @@ -108,14 +108,14 @@ export function convertGetters(script) {
return script
// Handle multiline getters first
.replace(
/^( *)get (\w+)\(\)\s*\{([\s\S]*?)\n\1\}/gm,
/^(\s*)get (\w+)\(\)\s*\{([\s\S]*?)\n\1\}/gm,
(_, indent, name, body) => {
return `${indent}Object.defineProperty(this, '${name}', { get() {${body}} })`
}
)
// Then handle single-line getters
.replace(
/^( *)get (\w+)\(\)\s*\{([^}]*)\}/gm,
/^(\s*)get (\w+)\(\)\s*\{([^}]*)\}/gm,
(_, indent, name, body) => {
return `${indent}Object.defineProperty(this, '${name}', { get() {${body}} })`
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nuedom/src/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const RE_FN = /(script|h_fn|fn):\s*(['"`])([^\2]*?)\2/g

export function compileJS(js) {
return js.replace(RE_FN, function(_, key, __, expr) {
return key == 'script' ? `${key}: function() { ${expr.trim().replaceAll('\\n', '\n')} \n\t\t}`
return key == 'script' ? `${key}: function() { ${expr.trim().replaceAll('\\n', '\n').replaceAll('\\r', '\r').replaceAll('\\t', '\t')} \n\t\t}`
: `${key}: ${compileFn(expr, key[0] == 'h')}`
})
}
Expand Down
Loading