mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
More tests and compiler API docs
This commit is contained in:
parent
b0df950242
commit
53700d4e94
@ -556,7 +556,7 @@ builder.selfInvokingFunction(
|
||||
Or, without params and args:
|
||||
|
||||
```javascript
|
||||
builder.selfInvokingFunction([
|
||||
builder.selfInvokingFunction(null, null, [
|
||||
builder.vars(['foo']),
|
||||
builder.assignment('foo', builder.literal('bar'))
|
||||
])
|
||||
@ -569,10 +569,57 @@ builder.selfInvokingFunction([
|
||||
}())
|
||||
```
|
||||
|
||||
### slot()
|
||||
### selfInvokingFunction(body)
|
||||
|
||||
Equivalent to `selfInvokingFunction(null, null, body)`.
|
||||
|
||||
### slot(onDone)
|
||||
|
||||
Returns a node that defers generating code until everything else is done. This can be helpful in situations where a fragment of code is not known until the rest of the code is generated.
|
||||
|
||||
As an example, the [TemplateRoot](../compiler/ast/TemplateRoot.js) node uses a slot to defer generating the static variables section of the compiled template. Not until all of the nodes have generated code is it known which static variables need to be added at the top of the compiled template.
|
||||
|
||||
```javascript
|
||||
builder.program([
|
||||
builder.slot((slot, generator) => {
|
||||
slot.setContent(generator.builder.vars(vars));
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'foo',
|
||||
init: generator.builder.literal('abc')
|
||||
});
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'bar',
|
||||
init: generator.builder.literal(123)
|
||||
});
|
||||
})
|
||||
])
|
||||
|
||||
// Output code:
|
||||
var foo = "abc",
|
||||
bar = 123;
|
||||
```
|
||||
|
||||
### strictEquality(left, right)
|
||||
|
||||
Returns a node that generates the following code:
|
||||
|
||||
```javascript
|
||||
<left> === <right>
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```javascript
|
||||
builder.strictEquality('a', 'b')
|
||||
|
||||
// Output code:
|
||||
a === b
|
||||
```
|
||||
|
||||
### templateRoot(body)
|
||||
|
||||
### text(argument, escape)
|
||||
|
||||
5
test/fixtures/codegen/autotest/selfInvokingFunction-null-args/expected.js
vendored
Normal file
5
test/fixtures/codegen/autotest/selfInvokingFunction-null-args/expected.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
(function() {
|
||||
var foo;
|
||||
|
||||
foo = "bar";
|
||||
}())
|
||||
11
test/fixtures/codegen/autotest/selfInvokingFunction-null-args/index.js
vendored
Normal file
11
test/fixtures/codegen/autotest/selfInvokingFunction-null-args/index.js
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(builder) {
|
||||
return builder.selfInvokingFunction(
|
||||
null,
|
||||
null,
|
||||
[
|
||||
builder.vars(['foo']),
|
||||
builder.assignment('foo', builder.literal('bar'))
|
||||
]);
|
||||
};
|
||||
6
test/fixtures/codegen/autotest/slot-with-statements/expected.js
vendored
Normal file
6
test/fixtures/codegen/autotest/slot-with-statements/expected.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
a = "abc";
|
||||
|
||||
var foo = "abc",
|
||||
bar = 123;
|
||||
|
||||
b = "def";
|
||||
25
test/fixtures/codegen/autotest/slot-with-statements/index.js
vendored
Normal file
25
test/fixtures/codegen/autotest/slot-with-statements/index.js
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(builder) {
|
||||
var vars = [];
|
||||
|
||||
return builder.program([
|
||||
builder.assignment('a', builder.literal('abc')),
|
||||
builder.slot((slot, generator) => {
|
||||
slot.setContent(generator.builder.vars(vars));
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'foo',
|
||||
init: generator.builder.literal('abc')
|
||||
});
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'bar',
|
||||
init: generator.builder.literal(123)
|
||||
});
|
||||
}),
|
||||
builder.assignment('b', builder.literal('def'))
|
||||
]);
|
||||
};
|
||||
8
test/fixtures/codegen/autotest/slot-with-writes-nested/expected.js
vendored
Normal file
8
test/fixtures/codegen/autotest/slot-with-writes-nested/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
if (true) {
|
||||
out.w("BEFORE - Hello World");
|
||||
|
||||
var foo = "abc",
|
||||
bar = 123;
|
||||
|
||||
out.w("AFTER - Hello World");
|
||||
}
|
||||
29
test/fixtures/codegen/autotest/slot-with-writes-nested/index.js
vendored
Normal file
29
test/fixtures/codegen/autotest/slot-with-writes-nested/index.js
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(builder) {
|
||||
var vars = [];
|
||||
|
||||
var varsSlot = builder.slot((slot, generator) => {
|
||||
slot.setContent(generator.builder.vars(vars));
|
||||
});
|
||||
|
||||
return builder.program([
|
||||
builder.ifStatement('true', [
|
||||
builder.text(builder.literal('BEFORE - Hello World')),
|
||||
varsSlot,
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'foo',
|
||||
init: builder.literal('abc')
|
||||
});
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'bar',
|
||||
init: builder.literal(123)
|
||||
});
|
||||
}),
|
||||
builder.text(builder.literal('AFTER - Hello World'))
|
||||
])
|
||||
]);
|
||||
};
|
||||
6
test/fixtures/codegen/autotest/slot-with-writes/expected.js
vendored
Normal file
6
test/fixtures/codegen/autotest/slot-with-writes/expected.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
out.w("BEFORE - Hello World");
|
||||
|
||||
var foo = "abc",
|
||||
bar = 123;
|
||||
|
||||
out.w("AFTER - Hello World");
|
||||
27
test/fixtures/codegen/autotest/slot-with-writes/index.js
vendored
Normal file
27
test/fixtures/codegen/autotest/slot-with-writes/index.js
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(builder) {
|
||||
var vars = [];
|
||||
|
||||
var varsSlot = builder.slot((slot, generator) => {
|
||||
slot.setContent(generator.builder.vars(vars));
|
||||
});
|
||||
|
||||
return builder.program([
|
||||
builder.text(builder.literal('BEFORE - Hello World')),
|
||||
varsSlot,
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'foo',
|
||||
init: builder.literal('abc')
|
||||
});
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'bar',
|
||||
init: builder.literal(123)
|
||||
});
|
||||
}),
|
||||
builder.text(builder.literal('AFTER - Hello World'))
|
||||
]);
|
||||
};
|
||||
2
test/fixtures/codegen/autotest/slot/expected.js
vendored
Normal file
2
test/fixtures/codegen/autotest/slot/expected.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var foo = "abc",
|
||||
bar = 123;
|
||||
23
test/fixtures/codegen/autotest/slot/index.js
vendored
Normal file
23
test/fixtures/codegen/autotest/slot/index.js
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(builder) {
|
||||
var vars = [];
|
||||
|
||||
return builder.program([
|
||||
builder.slot((slot, generator) => {
|
||||
slot.setContent(generator.builder.vars(vars));
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'foo',
|
||||
init: generator.builder.literal('abc')
|
||||
});
|
||||
}),
|
||||
builder.node(function(node, generator) {
|
||||
vars.push({
|
||||
id: 'bar',
|
||||
init: generator.builder.literal(123)
|
||||
});
|
||||
})
|
||||
]);
|
||||
};
|
||||
1
test/fixtures/codegen/autotest/strictEquality/expected.js
vendored
Normal file
1
test/fixtures/codegen/autotest/strictEquality/expected.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
a === b
|
||||
5
test/fixtures/codegen/autotest/strictEquality/index.js
vendored
Normal file
5
test/fixtures/codegen/autotest/strictEquality/index.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(builder) {
|
||||
return builder.strictEquality('a', 'b');
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user