{"action":"created","issue":{"url":"https://api.github.com/repos/webpack/webpack/issues/7636","repository_url":"https://api.github.com/repos/webpack/webpack","labels_url":"https://api.github.com/repos/webpack/webpack/issues/7636/labels{/name}","comments_url":"https://api.github.com/repos/webpack/webpack/issues/7636/comments","events_url":"https://api.github.com/repos/webpack/webpack/issues/7636/events","html_url":"https://github.com/webpack/webpack/issues/7636","id":337381677,"node_id":"MDU6SXNzdWUzMzczODE2Nzc=","number":7636,"title":"Parser varDeclaration hook could provide the declarator init node as well","user":{"login":"id0Sch","id":5864772,"node_id":"MDQ6VXNlcjU4NjQ3NzI=","avatar_url":"https://avatars3.githubusercontent.com/u/5864772?v=4","gravatar_id":"","url":"https://api.github.com/users/id0Sch","html_url":"https://github.com/id0Sch","followers_url":"https://api.github.com/users/id0Sch/followers","following_url":"https://api.github.com/users/id0Sch/following{/other_user}","gists_url":"https://api.github.com/users/id0Sch/gists{/gist_id}","starred_url":"https://api.github.com/users/id0Sch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/id0Sch/subscriptions","organizations_url":"https://api.github.com/users/id0Sch/orgs","repos_url":"https://api.github.com/users/id0Sch/repos","events_url":"https://api.github.com/users/id0Sch/events{/privacy}","received_events_url":"https://api.github.com/users/id0Sch/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-02T06:10:46Z","updated_at":"2018-07-02T07:02:02Z","closed_at":null,"author_association":"NONE","body":"<!-- Please don't delete this template or we'll close your issue -->\r\n\r\n## Feature request\r\n\r\n<!-- Issues which contain questions or support requests will be closed. -->\r\n<!-- Before creating an issue please make sure you are using the latest version of webpack. -->\r\n<!-- Check if this feature need to be implemented in a plugin or loader instead -->\r\n<!-- If yes: file the issue on the plugin/loader repo -->\r\n<!-- Features related to the development server should be filed on this repo instead -->\r\n\r\n**What is the expected behavior?**\r\nwhen tapping into a var declaration, it would be very convenient to also provide the init declarator node which is otherwise really hard to get(?)\r\n\r\n**What is motivation or use case for adding/changing the behavior?**\r\nI was writing a plugin that extracts feature flags from the code by looking up the declaration of a unique variable(`DEFAULT_FEATURE_FLAGS`) and then sends it to the back-office to be used for configuring A/B tests.\r\n- at the moment this is an internal plugin but I am considering releasing it publicly as it could provide value for other teams as well.\r\n\r\nwithout the support in the `varDeclaration` hook, I had to use the `program` hook and filter until I found the relevant statment like so\r\n\r\n``` javascript\r\nparser.hooks.program.tap(pluginName, (ast) => {\r\n            const module = parser.state.module\r\n            if (module.resource && module.resource.endsWith(this.options.flagsFileName)) {\r\n              const flagsDecleration = ast.body.filter(a => a.type === 'VariableDeclaration' && a.declarations[0].id.name === this.options.flagsVariable)\r\n              if (flagsDecleration) {\r\n                const initValue = flagsDecleration[0].declarations[0].init                \r\n              }\r\n            }\r\n          })\r\n```\r\n\r\nThis was extremely hard since I had 0 experience with webpack and the documentation is not really accurate, I had to read most of the Parser.js and a lot of the plugins' code (which is great, I learned a lot, but was really time-consuming)\r\n\r\n**How should this be implemented in your opinion?**\r\nIf provided by the varDeclaration hook, this could be much easier, \r\n``` javascript\r\nparser.hooks.varDeclaration.for(this.options.flagsVariable)\r\n            .tap(pluginName, (expr, initValue) => {\r\n              console.log(expr.name, initValue)\r\n            })\r\n```\r\n\r\nThe change I am suggesting is the Parser.js in `prewalkVariableDeclaration` function.\r\nall that is needed is when calling the hook (which is either `varDeclarationConst` / `varDeclarationLet` / `varDeclarationVar`) is to call it with `declarator.init` as a second argument.\r\n\r\n**Are you willing to work on this yourself?**\r\nI already patched the code locally to make sure this works and nothing else breaks and it did.\r\nI am willing to submit a pull request if you are interested in this feature\r\n\r\n\r\n\r\non a side note: \r\nThanks for maintaining webpack and providing the community with such an amazing tool ! "},"comment":{"url":"https://api.github.com/repos/webpack/webpack/issues/comments/401690758","html_url":"https://github.com/webpack/webpack/issues/7636#issuecomment-401690758","issue_url":"https://api.github.com/repos/webpack/webpack/issues/7636","id":401690758,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMTY5MDc1OA==","user":{"login":"sokra","id":1365881,"node_id":"MDQ6VXNlcjEzNjU4ODE=","avatar_url":"https://avatars0.githubusercontent.com/u/1365881?v=4","gravatar_id":"","url":"https://api.github.com/users/sokra","html_url":"https://github.com/sokra","followers_url":"https://api.github.com/users/sokra/followers","following_url":"https://api.github.com/users/sokra/following{/other_user}","gists_url":"https://api.github.com/users/sokra/gists{/gist_id}","starred_url":"https://api.github.com/users/sokra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sokra/subscriptions","organizations_url":"https://api.github.com/users/sokra/orgs","repos_url":"https://api.github.com/users/sokra/repos","events_url":"https://api.github.com/users/sokra/events{/privacy}","received_events_url":"https://api.github.com/users/sokra/received_events","type":"User","site_admin":false},"created_at":"2018-07-02T07:02:02Z","updated_at":"2018-07-02T07:02:02Z","author_association":"MEMBER","body":"Send a PR.\r\n\r\nBe aware of complexer scenarios like:\r\n\r\n``` js\r\nvar [ { x } ] = [ { x : 1 }, { y : 2 } ];\r\n```"}}
{"actor":{"display_login":"sokra"}}