Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Feb 18, 2017
1 parent a4fb22b commit 9f93d83
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
19 changes: 15 additions & 4 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var SPEC_TYPES = /^"(?:number|date(?:time)?|time|month|email|color)\b/i
* Matches the 'import' statement
* @const {RegExp}
*/
var IMPORT_STATEMENT = /^\s*import(?:(?:\s|[^\s'"])*)['|"].*\n?/gm
var IMPORT_STATEMENT = /^\s*import(?!\w)(?:(?:\s|[^\s'"])*)['|"].*\n?/gm

/**
* Matches trailing spaces and tabs by line.
Expand Down Expand Up @@ -337,7 +337,7 @@ function compileHTML (html, opts, pcex) {
* 2016-01-18: rewritten to capture only the method name (performant)
* @const {RegExp}
*/
var JS_ES6SIGN = /^[ \t]*([$_A-Za-z][$\w]*)\s*\([^()]*\)\s*{/m
var JS_ES6SIGN = /^[ \t]*(((?:async)\s*)?([$_A-Za-z][$\w]*))\s*\([^()]*\)\s*{/m

/**
* Regex for remotion of multiline and single-line JavaScript comments, merged with
Expand Down Expand Up @@ -368,6 +368,8 @@ function riotjs (js) {
match,
toes5,
pos,
method,
prefix,
name,
RE = RegExp

Expand All @@ -379,9 +381,18 @@ function riotjs (js) {
js = RE.rightContext
pos = skipBody(js, JS_ES6END)

name = match[1]
method = match[1]
prefix = match[2] || ''
name = match[3]

toes5 = !/^(?:if|while|for|switch|catch|function)$/.test(name)
name = toes5 ? match[0].replace(name, 'this.' + name + ' = function') : match[0]

if (toes5) {
name = match[0].replace(method, 'this.' + name + ' =' + prefix + ' function')
} else {
name = match[0]
}

parts.push(name, js.slice(0, pos))
js = js.slice(pos)

Expand Down
17 changes: 14 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ function compileHTML (html, opts, pcex) {
* 2016-01-18: rewritten to capture only the method name (performant)
* @const {RegExp}
*/
var JS_ES6SIGN = /^[ \t]*([$_A-Za-z][$\w]*)\s*\([^()]*\)\s*{/m
var JS_ES6SIGN = /^[ \t]*(((?:async)\s*)?([$_A-Za-z][$\w]*))\s*\([^()]*\)\s*{/m

/**
* Regex for remotion of multiline and single-line JavaScript comments, merged with
Expand Down Expand Up @@ -399,6 +399,8 @@ function riotjs (js) {
match,
toes5,
pos,
method,
prefix,
name,
RE = RegExp

Expand All @@ -412,9 +414,18 @@ function riotjs (js) {
pos = skipBody(js, JS_ES6END)

// convert ES6 method signature to ES5 function, exclude JS keywords
name = match[1]
method = match[1] // the full match
prefix = match[2] || '' // async or ''
name = match[3] // the method name

toes5 = !/^(?:if|while|for|switch|catch|function)$/.test(name)
name = toes5 ? match[0].replace(name, 'this.' + name + ' = function') : match[0]

if (toes5) {
name = match[0].replace(method, 'this.' + name + ' =' + prefix + ' function')
} else {
name = match[0]
}

parts.push(name, js.slice(0, pos))
js = js.slice(pos)

Expand Down
9 changes: 9 additions & 0 deletions test/specs/expect/es6-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
riot.tag2('async', '<h1>async test</h1>', '', '', function(opts) {
this.foo = function(){
return 'foo'
}.bind(this)
this.doAsyncThing =async function(){
await new Promise((resolve) =>{setTimeout(resolve,2000)});
console.log("done");
}.bind(this)
});
14 changes: 14 additions & 0 deletions test/specs/fixtures/es6-async.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<async>
<h1>async test</h1>

<script>
foo() {
return 'foo'
}

async doAsyncThing() {
await new Promise((resolve) => {setTimeout(resolve,2000)});
console.log("done");
}
</script>
</async>
4 changes: 4 additions & 0 deletions test/specs/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ describe('Compile tags', function () {
testFile('es6-import')
})

it('Support short cut async functions', function () {
testFile('es6-async')
})

it('Detect es6 import in the untagged JS block', function () {
testFile('es6-import-untagged')
})
Expand Down

0 comments on commit 9f93d83

Please sign in to comment.