{"action":"created","issue":{"url":"https://api.github.com/repos/mozilla/nunjucks/issues/791","repository_url":"https://api.github.com/repos/mozilla/nunjucks","labels_url":"https://api.github.com/repos/mozilla/nunjucks/issues/791/labels{/name}","comments_url":"https://api.github.com/repos/mozilla/nunjucks/issues/791/comments","events_url":"https://api.github.com/repos/mozilla/nunjucks/issues/791/events","html_url":"https://github.com/mozilla/nunjucks/pull/791","id":165108297,"number":791,"title":"fix: macro parameter with default value overshadowed by unrelated macro","user":{"login":"shepherdwind","id":452899,"avatar_url":"https://avatars.githubusercontent.com/u/452899?v=3","gravatar_id":"","url":"https://api.github.com/users/shepherdwind","html_url":"https://github.com/shepherdwind","followers_url":"https://api.github.com/users/shepherdwind/followers","following_url":"https://api.github.com/users/shepherdwind/following{/other_user}","gists_url":"https://api.github.com/users/shepherdwind/gists{/gist_id}","starred_url":"https://api.github.com/users/shepherdwind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shepherdwind/subscriptions","organizations_url":"https://api.github.com/users/shepherdwind/orgs","repos_url":"https://api.github.com/users/shepherdwind/repos","events_url":"https://api.github.com/users/shepherdwind/events{/privacy}","received_events_url":"https://api.github.com/users/shepherdwind/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2016-07-12T15:23:37Z","updated_at":"2016-07-13T23:26:40Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/mozilla/nunjucks/pulls/791","html_url":"https://github.com/mozilla/nunjucks/pull/791","diff_url":"https://github.com/mozilla/nunjucks/pull/791.diff","patch_url":"https://github.com/mozilla/nunjucks/pull/791.patch"},"body":"close #774 \r\n\r\nThere is only a little code to fix that issue. If their is some problem, I will fix soon. Or maybe their is some more good solution, I don't sure if this realization is good or not."},"comment":{"url":"https://api.github.com/repos/mozilla/nunjucks/issues/comments/232515875","html_url":"https://github.com/mozilla/nunjucks/pull/791#issuecomment-232515875","issue_url":"https://api.github.com/repos/mozilla/nunjucks/issues/791","id":232515875,"user":{"login":"shepherdwind","id":452899,"avatar_url":"https://avatars.githubusercontent.com/u/452899?v=3","gravatar_id":"","url":"https://api.github.com/users/shepherdwind","html_url":"https://github.com/shepherdwind","followers_url":"https://api.github.com/users/shepherdwind/followers","following_url":"https://api.github.com/users/shepherdwind/following{/other_user}","gists_url":"https://api.github.com/users/shepherdwind/gists{/gist_id}","starred_url":"https://api.github.com/users/shepherdwind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shepherdwind/subscriptions","organizations_url":"https://api.github.com/users/shepherdwind/orgs","repos_url":"https://api.github.com/users/shepherdwind/repos","events_url":"https://api.github.com/users/shepherdwind/events{/privacy}","received_events_url":"https://api.github.com/users/shepherdwind/received_events","type":"User","site_admin":false},"created_at":"2016-07-13T23:26:40Z","updated_at":"2016-07-13T23:26:40Z","body":"First, let us take a look how this bug happen.\r\n\r\nWhen parse `macro1`, code like this\r\n\r\n```\r\n{% macro macro1() %}\r\n{% endmacro %}\r\n```\r\n\r\ncompiler will output code like this\r\n\r\n```js\r\n// this is macro function body\r\nvar macro_t_1 = runtime.makeMacro([], [], () => ());\r\ncontext.addExport(\"macro1\");\r\ncontext.setVariable(\"macro1\", macro_t_1);\r\n```\r\n\r\nAt the same time, compile-time frame will add varibale\r\n\r\n```js\r\n        var funcId = this._compileMacro(node, frame);\r\n\r\n        // Expose the macro to the templates\r\n        var name = node.name.value;\r\n        frame.set(name, funcId);\r\n```\r\n\r\nSo, compiler continue parse, when deal with\r\n\r\n```\r\n{% macro macro2(macro1=\"default\") %}\r\n{{macro1}}\r\n{% endmacro %}\r\n```\r\n\r\nWhen symbol `macro1` appear, frame.lookup will return functionId `macro_t_1` , so after compile this will output code\r\n\r\n```js\r\nframe.set(\"macro1\", kwargs.hasOwnProperty(\"macro1\") ? kwargs[\"macro1\"] : \"default\");\r\nvar t_4 = \"\";\r\n_4 += runtime.suppressValue(macro_t_1, env.opts.autoescape);\r\n```\r\n\r\nFirst macro1 argument will add to run-time frame, but the `{{macro1}}` has already became symbol `macro_t_1`. That why this bug happen.\r\n\r\nNow, why the second test will pass. When compileMacro compile-time frame is a new frame, so when `macro2` compile, frame.lookup('macro1') will return null, so code generate right\r\n\r\n```js\r\n var t_4 = \"\";\r\n t_4 += runtime.suppressValue((lineno = 0,\r\n   colno = 96,\r\n  runtime.callWrap(runtime.contextOrFrameLookup(context, frame, \"macro1\"), \"macro1\", context, [])), env.opts.autoescape);\r\n```\r\n\r\nIn my first test case, code generate is just the same as the next one. Only diff difference is the local argument value set\r\n\r\n```\r\n// first test case, key is macro1\r\nframe.set(\"macro1\", kwargs.hasOwnProperty(\"macro1\") ? kwargs[\"macro1\"] : \"default\");\r\n// next test case, key is text\r\nframe.set(\"text\", kwargs.hasOwnProperty(\"text\") ? kwargs[\"text\"] : \"default\");\r\n```\r\n\r\nSo, at the first test, symbol `macro1` will found as string, and the next test case, `macro1` not find the current frame, will lookup from the context(`context.setVariable(\"macro1\", macro_t_1)`), so return a function."}}
{"actor":{"display_login":"shepherdwind"}}