diff --git a/fixtures/components-named-slots/alert.edge b/fixtures/components-named-slots/alert.edge index d038c9f..2565540 100644 --- a/fixtures/components-named-slots/alert.edge +++ b/fixtures/components-named-slots/alert.edge @@ -1,2 +1,2 @@ -{{ $slots.heading }} -{{ $slots.main }} \ No newline at end of file +{{ $slots.heading() }} +{{ $slots.main() }} \ No newline at end of file diff --git a/fixtures/components-named-slots/compiled.js b/fixtures/components-named-slots/compiled.js index e226c54..f84811a 100644 --- a/fixtures/components-named-slots/compiled.js +++ b/fixtures/components-named-slots/compiled.js @@ -1,15 +1,25 @@ (function (template, ctx) { let out = '' out += template.renderWithState('components-named-slots/alert', {}, { 'heading': (function (template, ctx) { +return function (props) { let out = '' + ctx.newFrame() + ctx.setOnFrame('props', props) out += ' This is title' out += '\n' + ctx.removeFrame() return out +} })(template, ctx), 'main': (function (template, ctx) { +return function (props) { let out = '' + ctx.newFrame() + ctx.setOnFrame('props', props) out += ' This is then body' out += '\n' + ctx.removeFrame() return out +} })(template, ctx) }) return out })(template, ctx) \ No newline at end of file diff --git a/fixtures/components-props/alert.edge b/fixtures/components-props/alert.edge index 3d05ce2..862ed21 100644 --- a/fixtures/components-props/alert.edge +++ b/fixtures/components-props/alert.edge @@ -1,2 +1,2 @@ {{ title }} -{{ $slots.main }} \ No newline at end of file +{{ $slots.main() }} \ No newline at end of file diff --git a/fixtures/components-props/compiled.js b/fixtures/components-props/compiled.js index a58696f..345bb0b 100644 --- a/fixtures/components-props/compiled.js +++ b/fixtures/components-props/compiled.js @@ -1,10 +1,15 @@ (function (template, ctx) { let out = '' out += template.renderWithState('components-props/alert', { 'title': 'H1' }, { 'main': (function (template, ctx) { +return function (props) { let out = '' + ctx.newFrame() + ctx.setOnFrame('props', props) out += 'Hello world' out += '\n' + ctx.removeFrame() return out +} })(template, ctx) }) return out })(template, ctx) \ No newline at end of file diff --git a/fixtures/components-slot-props/alert.edge b/fixtures/components-slot-props/alert.edge new file mode 100644 index 0000000..c4c43f6 --- /dev/null +++ b/fixtures/components-slot-props/alert.edge @@ -0,0 +1,2 @@ +Top level username is {{ username || 'Guest' }} +{{ $slots.title({ username: 'nikk' }) }} \ No newline at end of file diff --git a/fixtures/components-slot-props/compiled.js b/fixtures/components-slot-props/compiled.js new file mode 100644 index 0000000..b0eddd0 --- /dev/null +++ b/fixtures/components-slot-props/compiled.js @@ -0,0 +1,16 @@ +(function (template, ctx) { + let out = '' + out += template.renderWithState('components-slot-props/alert', {}, { 'title': (function (template, ctx) { +return function (props) { + let out = '' + ctx.newFrame() + ctx.setOnFrame('props', props) + out += ' Hello ' + out += `${ctx.escape(ctx.resolve('props').username)}` + out += '\n' + ctx.removeFrame() + return out +} +})(template, ctx) }) + return out +})(template, ctx) \ No newline at end of file diff --git a/fixtures/components-slot-props/index.edge b/fixtures/components-slot-props/index.edge new file mode 100644 index 0000000..a570785 --- /dev/null +++ b/fixtures/components-slot-props/index.edge @@ -0,0 +1,5 @@ +@component('components-slot-props/alert') + @slot('title') + Hello {{ props.username }} + @endslot +@endcomponent \ No newline at end of file diff --git a/fixtures/components-slot-props/index.json b/fixtures/components-slot-props/index.json new file mode 100644 index 0000000..d5f5540 --- /dev/null +++ b/fixtures/components-slot-props/index.json @@ -0,0 +1,3 @@ +{ + "username": "virk" +} \ No newline at end of file diff --git a/fixtures/components-slot-props/index.txt b/fixtures/components-slot-props/index.txt new file mode 100644 index 0000000..a00f96a --- /dev/null +++ b/fixtures/components-slot-props/index.txt @@ -0,0 +1,2 @@ +Top level username is Guest + Hello nikk \ No newline at end of file diff --git a/fixtures/components-state/alert.edge b/fixtures/components-state/alert.edge index d781d68..0781835 100644 --- a/fixtures/components-state/alert.edge +++ b/fixtures/components-state/alert.edge @@ -1 +1 @@ -{{ $slots.main }} \ No newline at end of file +{{ $slots.main() }} \ No newline at end of file diff --git a/fixtures/components-state/compiled.js b/fixtures/components-state/compiled.js index d36d018..1a1c926 100644 --- a/fixtures/components-state/compiled.js +++ b/fixtures/components-state/compiled.js @@ -1,11 +1,16 @@ (function (template, ctx) { let out = '' out += template.renderWithState('components-state/alert', {}, { 'main': (function (template, ctx) { +return function (props) { let out = '' + ctx.newFrame() + ctx.setOnFrame('props', props) out += ' Hello ' out += `${ctx.escape(ctx.resolve('username'))}` out += '\n' + ctx.removeFrame() return out +} })(template, ctx) }) return out })(template, ctx) \ No newline at end of file diff --git a/fixtures/components/alert.edge b/fixtures/components/alert.edge index d781d68..0781835 100644 --- a/fixtures/components/alert.edge +++ b/fixtures/components/alert.edge @@ -1 +1 @@ -{{ $slots.main }} \ No newline at end of file +{{ $slots.main() }} \ No newline at end of file diff --git a/fixtures/components/compiled.js b/fixtures/components/compiled.js index ef04fac..f2ea3a7 100644 --- a/fixtures/components/compiled.js +++ b/fixtures/components/compiled.js @@ -1,10 +1,15 @@ (function (template, ctx) { let out = '' out += template.renderWithState('components/alert', {}, { 'main': (function (template, ctx) { +return function (props) { let out = '' + ctx.newFrame() + ctx.setOnFrame('props', props) out += ' Hello world' out += '\n' + ctx.removeFrame() return out +} })(template, ctx) }) return out })(template, ctx) \ No newline at end of file diff --git a/japaFile.js b/japaFile.js index aa43bb7..cd41bb7 100644 --- a/japaFile.js +++ b/japaFile.js @@ -10,4 +10,4 @@ Assertion.use((chai, utils) => { } }) -cli.run('test/edge.spec.ts') +cli.run('test/*.spec.ts') diff --git a/package-lock.json b/package-lock.json index 28423ee..18103c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -148,9 +148,9 @@ } }, "@types/node": { - "version": "10.5.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.1.tgz", - "integrity": "sha512-AFLl1IALIuyt6oK4AYZsgWVJ/5rnyzQWud7IebaZWWV3YmgtPZkQmYio9R5Ze/2pdd7XfqF5bP+hWS11mAKoOQ==", + "version": "10.5.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.2.tgz", + "integrity": "sha512-m9zXmifkZsMHZBOyxZWilMwmTlpC8x5Ty360JKTiXvlXZfBWYpsg9ZZvP/Ye+iZUh+Q+MxDLjItVTWIsfwz+8Q==", "dev": true }, "@types/semver": { @@ -1179,9 +1179,9 @@ } }, "edge-parser": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/edge-parser/-/edge-parser-1.0.9.tgz", - "integrity": "sha512-PjiTVHlxrMkY4VuTNNgi//XAJ8u057tZfyNmlRConGhJGCtlSgByXY8ErXEd7ow9XExGNts+V3p5FgFrGYbVsg==", + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/edge-parser/-/edge-parser-1.0.11.tgz", + "integrity": "sha512-T6Cv7Mrwu1sFgs5E2kyv655g8r20d6AuR6DXfvjtMItdwpddDptVDS9yTQ7yBQ0S8WG9yrazcIt+dJFHlg1IVg==", "requires": { "acorn": "^5.7.1", "astring": "^1.3.1", @@ -1522,12 +1522,13 @@ } }, "git-username": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/git-username/-/git-username-0.5.1.tgz", - "integrity": "sha512-xjUjrj3i4kup2A3a/ZVZB1Nt0PUX7SU7KeVqIbXPdslT7NbNfyO04JMxBv4gar77JePdS+A6f05jG1Viy6+U1w==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/git-username/-/git-username-1.0.0.tgz", + "integrity": "sha512-xm45KwBR6Eu1jO4umx/o2M84v9TC7tdOBuzLx8ayhdR9H1FBiiG9azz31uC0esDvaWVBTDINpJ5USomk+ja8OQ==", "dev": true, "requires": { - "remote-origin-url": "^0.4.0" + "parse-github-url": "^1.0.2", + "remote-origin-url": "^1.0.0" } }, "glob": { @@ -2496,63 +2497,112 @@ } }, "mrm": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/mrm/-/mrm-1.1.1.tgz", - "integrity": "sha512-M5V8JIqf5CxPqQMJtc3OYhKjVFK6cFi71ryyshOS07gv2+GtGj11nSYqu3lxrn215Rb3My8hTFnvEHiBTC0vHQ==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/mrm/-/mrm-1.2.0.tgz", + "integrity": "sha512-B9WqklT0YkaBzhwsm9o6rCwng2c9IHMbD8yK1ORIvsOpVNFSBjaUjUJu9oscDqLvujGoBkrjCjiK3JfdDrAaYg==", "dev": true, "requires": { - "chalk": "^2.1.0", - "git-username": "^0.5.0", + "chalk": "^2.4.1", + "git-username": "^1.0.0", "glob": "^7.1.2", "is-directory": "^0.3.1", "listify": "^1.0.0", - "lodash": "^4.17.4", + "lodash": "^4.17.10", "longest": "^2.0.1", "middleearth-names": "^1.1.0", "minimist": "^1.2.0", - "mrm-core": "^2.2.2", - "mrm-preset-default": "^1.0.1", - "requireg": "^0.1.7", - "semver-utils": "^1.1.1", - "update-notifier": "^2.2.0", + "mrm-core": "^3.1.2", + "mrm-preset-default": "^1.8.0", + "requireg": "^0.1.8", + "semver-utils": "^1.1.2", + "update-notifier": "^2.5.0", "user-home": "^2.0.0", "user-meta": "^1.0.0" }, "dependencies": { - "fs-extra": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz", - "integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==", + "find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", "dev": true, "requires": { - "graceful-fs": "^4.1.2", - "jsonfile": "^4.0.0", - "universalify": "^0.1.0" + "locate-path": "^3.0.0" + } + }, + "locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "dev": true, + "requires": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" } }, "mrm-core": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/mrm-core/-/mrm-core-2.6.0.tgz", - "integrity": "sha512-NRhdbwvEuAk2btF7se3SFFtDYw/RkWrEWf0z3dld1cDZzVEgfXn2iXG08nlhwgES8c0hxRsaOsgpuhp6z+Yq0Q==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/mrm-core/-/mrm-core-3.1.2.tgz", + "integrity": "sha512-2pJpnWMrt3ic/8DnmGNgCpAGZwuJbY9TE/c8tDyf9wchl9qSaHSvFP+tQs4Fbt5vcapuCMgQh1cpP+a1Mztxlw==", "dev": true, "requires": { "babel-code-frame": "^6.26.0", - "chalk": "^2.3.0", + "chalk": "^2.4.1", "comment-json": "^1.1.3", "detect-indent": "^5.0.0", "editorconfig": "^0.15.0", - "find-up": "^2.1.0", - "fs-extra": "^4.0.2", - "js-yaml": "^3.10.0", + "find-up": "^3.0.0", + "fs-extra": "^6.0.1", + "js-yaml": "^3.12.0", "listify": "^1.0.0", - "lodash": "^4.17.4", + "lodash": "^4.17.10", + "minimist": "^1.2.0", "prop-ini": "^0.0.2", - "readme-badger": "^0.2.0", + "readme-badger": "^0.3.0", + "semver": "^5.5.0", "smpltmpl": "^1.0.2", - "split-lines": "^1.1.0", + "split-lines": "^2.0.0", "strip-bom": "^3.0.0", - "webpack-merge": "^4.1.1" + "webpack-merge": "^4.1.3" } + }, + "p-limit": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.0.0.tgz", + "integrity": "sha512-fl5s52lI5ahKCernzzIyAP0QAZbGIovtVHGwpcu1Jr/EpzLVDI2myISHwGqK7m8uQFugVWSrbxH7XnhGtvEc+A==", + "dev": true, + "requires": { + "p-try": "^2.0.0" + } + }, + "p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "dev": true, + "requires": { + "p-limit": "^2.0.0" + } + }, + "p-try": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.0.0.tgz", + "integrity": "sha512-hMp0onDKIajHfIkdRk3P4CdCmErkYAxxDtP3Wx/4nZ3aGlau2VKh3mZpcuFkH27WQkL/3WBCPOktzA9ZOAnMQQ==", + "dev": true + }, + "readme-badger": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/readme-badger/-/readme-badger-0.3.0.tgz", + "integrity": "sha512-+sMOLSs1imZUISZ2Rhz7qqVd77QtpcAPbGeIraFdgJmijb04YtdlPjGNBvDChTNtLbeQ6JNGQy3pOgslWfaP3g==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0" + } + }, + "split-lines": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/split-lines/-/split-lines-2.0.0.tgz", + "integrity": "sha512-gaIdhbqxkB5/VflPXsJwZvEzh/kdwiRPF9iqpkxX4us+lzB8INedFwjCyo6vwuz5x2Ddlnav2zh270CEjCG8mA==", + "dev": true } } }, @@ -2628,6 +2678,35 @@ "requires": { "git-username": "^0.5.0", "mrm-core": "^3.1.0" + }, + "dependencies": { + "git-username": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/git-username/-/git-username-0.5.1.tgz", + "integrity": "sha512-xjUjrj3i4kup2A3a/ZVZB1Nt0PUX7SU7KeVqIbXPdslT7NbNfyO04JMxBv4gar77JePdS+A6f05jG1Viy6+U1w==", + "dev": true, + "requires": { + "remote-origin-url": "^0.4.0" + } + }, + "parse-git-config": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/parse-git-config/-/parse-git-config-0.2.0.tgz", + "integrity": "sha1-Jygz/dFf6hRvt10zbSNrljtv9wY=", + "dev": true, + "requires": { + "ini": "^1.3.3" + } + }, + "remote-origin-url": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-0.4.0.tgz", + "integrity": "sha1-TT4pAvNOLTfRwmPYdxC3frQIajA=", + "dev": true, + "requires": { + "parse-git-config": "^0.2.0" + } + } } }, "mrm-task-contributing": { @@ -2638,6 +2717,35 @@ "requires": { "git-username": "^0.5.0", "mrm-core": "^3.1.0" + }, + "dependencies": { + "git-username": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/git-username/-/git-username-0.5.1.tgz", + "integrity": "sha512-xjUjrj3i4kup2A3a/ZVZB1Nt0PUX7SU7KeVqIbXPdslT7NbNfyO04JMxBv4gar77JePdS+A6f05jG1Viy6+U1w==", + "dev": true, + "requires": { + "remote-origin-url": "^0.4.0" + } + }, + "parse-git-config": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/parse-git-config/-/parse-git-config-0.2.0.tgz", + "integrity": "sha1-Jygz/dFf6hRvt10zbSNrljtv9wY=", + "dev": true, + "requires": { + "ini": "^1.3.3" + } + }, + "remote-origin-url": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-0.4.0.tgz", + "integrity": "sha1-TT4pAvNOLTfRwmPYdxC3frQIajA=", + "dev": true, + "requires": { + "parse-git-config": "^0.2.0" + } + } } }, "mrm-task-editorconfig": { @@ -2706,6 +2814,35 @@ "git-username": "^0.5.0", "mrm-core": "^3.1.0", "user-meta": "^1.0.0" + }, + "dependencies": { + "git-username": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/git-username/-/git-username-0.5.1.tgz", + "integrity": "sha512-xjUjrj3i4kup2A3a/ZVZB1Nt0PUX7SU7KeVqIbXPdslT7NbNfyO04JMxBv4gar77JePdS+A6f05jG1Viy6+U1w==", + "dev": true, + "requires": { + "remote-origin-url": "^0.4.0" + } + }, + "parse-git-config": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/parse-git-config/-/parse-git-config-0.2.0.tgz", + "integrity": "sha1-Jygz/dFf6hRvt10zbSNrljtv9wY=", + "dev": true, + "requires": { + "ini": "^1.3.3" + } + }, + "remote-origin-url": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-0.4.0.tgz", + "integrity": "sha1-TT4pAvNOLTfRwmPYdxC3frQIajA=", + "dev": true, + "requires": { + "parse-git-config": "^0.2.0" + } + } } }, "mrm-task-prettier": { @@ -2727,6 +2864,35 @@ "git-username": "^0.5.0", "mrm-core": "^3.1.0", "user-meta": "^1.0.0" + }, + "dependencies": { + "git-username": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/git-username/-/git-username-0.5.1.tgz", + "integrity": "sha512-xjUjrj3i4kup2A3a/ZVZB1Nt0PUX7SU7KeVqIbXPdslT7NbNfyO04JMxBv4gar77JePdS+A6f05jG1Viy6+U1w==", + "dev": true, + "requires": { + "remote-origin-url": "^0.4.0" + } + }, + "parse-git-config": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/parse-git-config/-/parse-git-config-0.2.0.tgz", + "integrity": "sha1-Jygz/dFf6hRvt10zbSNrljtv9wY=", + "dev": true, + "requires": { + "ini": "^1.3.3" + } + }, + "remote-origin-url": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-0.4.0.tgz", + "integrity": "sha1-TT4pAvNOLTfRwmPYdxC3frQIajA=", + "dev": true, + "requires": { + "parse-git-config": "^0.2.0" + } + } } }, "mrm-task-semantic-release": { @@ -2766,6 +2932,35 @@ "lodash": "^4.17.4", "mrm-core": "^3.1.0", "semver-utils": "^1.1.1" + }, + "dependencies": { + "git-username": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/git-username/-/git-username-0.5.1.tgz", + "integrity": "sha512-xjUjrj3i4kup2A3a/ZVZB1Nt0PUX7SU7KeVqIbXPdslT7NbNfyO04JMxBv4gar77JePdS+A6f05jG1Viy6+U1w==", + "dev": true, + "requires": { + "remote-origin-url": "^0.4.0" + } + }, + "parse-git-config": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/parse-git-config/-/parse-git-config-0.2.0.tgz", + "integrity": "sha1-Jygz/dFf6hRvt10zbSNrljtv9wY=", + "dev": true, + "requires": { + "ini": "^1.3.3" + } + }, + "remote-origin-url": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-0.4.0.tgz", + "integrity": "sha1-TT4pAvNOLTfRwmPYdxC3frQIajA=", + "dev": true, + "requires": { + "parse-git-config": "^0.2.0" + } + } } }, "mrm-task-typescript": { @@ -5760,23 +5955,12 @@ } }, "remote-origin-url": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-0.4.0.tgz", - "integrity": "sha1-TT4pAvNOLTfRwmPYdxC3frQIajA=", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/remote-origin-url/-/remote-origin-url-1.0.0.tgz", + "integrity": "sha512-xHDM6IBqivpiQ1e4WOuFpM/T6rbzA/WBsu+3WLtgPOhHyjA0nYlijV3NprlTb4FcXlQ5+Q+z174sQ1NnUF5FwA==", "dev": true, "requires": { - "parse-git-config": "^0.2.0" - }, - "dependencies": { - "parse-git-config": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/parse-git-config/-/parse-git-config-0.2.0.tgz", - "integrity": "sha1-Jygz/dFf6hRvt10zbSNrljtv9wY=", - "dev": true, - "requires": { - "ini": "^1.3.3" - } - } + "parse-git-config": "^1.1.1" } }, "remove-trailing-separator": { diff --git a/package.json b/package.json index 5f13f9b..5bfd9ac 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "homepage": "https://github.com/poppinss/edge#readme", "devDependencies": { "@adonisjs/mrm-preset": "^1.0.7", - "@types/node": "^10.5.1", + "@types/node": "^10.5.2", "commitizen": "^2.10.1", "coveralls": "^3.0.2", "cz-conventional-changelog": "^2.1.0", @@ -39,7 +39,7 @@ "fs-extra": "^6.0.1", "japa": "^1.0.6", "japa-cli": "^1.0.1", - "mrm": "^1.1.1", + "mrm": "^1.2.0", "nyc": "^12.0.2", "pkg-ok": "^2.2.0", "ts-node": "^7.0.0", @@ -63,7 +63,7 @@ }, "dependencies": { "deep-extend": "^0.6.0", - "edge-parser": "^1.0.9", + "edge-parser": "^1.0.11", "he": "^1.1.1", "macroable": "^1.0.0", "node-exceptions": "^3.0.0", diff --git a/src/Context/index.ts b/src/Context/index.ts index 9950b5e..06a1fd3 100644 --- a/src/Context/index.ts +++ b/src/Context/index.ts @@ -136,12 +136,19 @@ export class Context extends Macroable { * created on the frame and not the presenter state */ public set (key: string, value: any): void { - if (this.frames.length) { - this.setOnFrame(key, value) + /** + * If value already exists on the presenter + * state, then mutate it first + */ + if (this.presenter.state[key] !== undefined || !this.frames.length) { + set(this.presenter.state, key, value) return } - set(this.presenter.state, key, value) + /** + * If frames exists, then set it on frame + */ + this.setOnFrame(key, value) } /** diff --git a/src/Edge/index.ts b/src/Edge/index.ts index ebaae35..f507793 100644 --- a/src/Edge/index.ts +++ b/src/Edge/index.ts @@ -56,6 +56,7 @@ export class Edge { * Mount a disk to the loader */ public static mount (diskName: string, dirPath?: string): void { + /* istanbul ignore else */ if (!this.compiler) { this.configure({}) } diff --git a/src/Tags/Component.ts b/src/Tags/Component.ts index ed059a7..8f238c6 100644 --- a/src/Tags/Component.ts +++ b/src/Tags/Component.ts @@ -33,13 +33,20 @@ export class ComponentTag { name = (child as IBlockNode).properties.jsArg } - slots[name] = slots[name] || new EdgeBuffer() + if (!slots[name]) { + slots[name] = new EdgeBuffer() + slots[name].writeStatement('ctx.newFrame()') + slots[name].writeStatement(`ctx.setOnFrame('props', props)`) + } + parser.processToken(child, slots[name]) }) const obj = new ObjectifyString() Object.keys(slots).forEach((slot) => { - obj.add(slot, slots[slot].flush()) + slots[slot].writeStatement('ctx.removeFrame()') + slots[slot].wrap('return function (props) {', '}') + obj.add(slot, slots[slot].flush(true)) }) buffer.writeLine(`template.renderWithState(${name}, ${props}, ${obj.flush()})`) diff --git a/test/context.spec.ts b/test/context.spec.ts index cea20d6..7d6d544 100644 --- a/test/context.spec.ts +++ b/test/context.spec.ts @@ -240,6 +240,25 @@ test.group('Context', (group) => { assert.equal(context.resolve('getUsername')(), 'virk') }) + test('frame functions should retain access to context', (assert) => { + const sharedState = {} + const data = { + username: 'virk', + } + + class MyPresenter extends Presenter { + } + + const presenter = new MyPresenter(data) + const context = new Context(presenter, sharedState) + context.newFrame() + context.setOnFrame('getUsername', function () { + return this.resolve('username') + }) + assert.equal(context.resolve('getUsername')(), 'virk') + context.removeFrame() + }) + test('mutate values inside presenter state', (assert) => { const sharedState = { } @@ -318,4 +337,42 @@ test.group('Context', (group) => { assert.isUndefined(context.resolve('username')) }) + + test('mutate value inside to the presenter state when value already exists there', (assert) => { + const sharedState = { + } + + const data = { + username: 'virk', + } + + class MyPresenter extends Presenter { + } + + const presenter = new MyPresenter(data) + const context = new Context(presenter, sharedState) + + context.newFrame() + context.set('username', 'nikk') + assert.deepEqual(context['frames'][0], {}) + assert.equal(context['presenter'].state.username, 'nikk') + }) + + test('raise error when trying to call setOnFrame without calling newFrame', (assert) => { + const sharedState = { + } + + const data = { + username: 'virk', + } + + class MyPresenter extends Presenter { + } + + const presenter = new MyPresenter(data) + const context = new Context(presenter, sharedState) + + const fn = () => context.setOnFrame('username', 'nikk') + assert.throw(fn, 'Make sure to call {newFrame} before calling {setOnFrame}') + }) })