{"action":"created","issue":{"url":"https://api.github.com/repos/rails/sprockets/issues/186","labels_url":"https://api.github.com/repos/rails/sprockets/issues/186/labels{/name}","comments_url":"https://api.github.com/repos/rails/sprockets/issues/186/comments","events_url":"https://api.github.com/repos/rails/sprockets/issues/186/events","html_url":"https://github.com/rails/sprockets/issues/186","id":119194592,"number":186,"title":"directive parser erroneously adds extra newline","user":{"login":"vaneyckt","id":767465,"avatar_url":"https://avatars.githubusercontent.com/u/767465?v=3","gravatar_id":"","url":"https://api.github.com/users/vaneyckt","html_url":"https://github.com/vaneyckt","followers_url":"https://api.github.com/users/vaneyckt/followers","following_url":"https://api.github.com/users/vaneyckt/following{/other_user}","gists_url":"https://api.github.com/users/vaneyckt/gists{/gist_id}","starred_url":"https://api.github.com/users/vaneyckt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vaneyckt/subscriptions","organizations_url":"https://api.github.com/users/vaneyckt/orgs","repos_url":"https://api.github.com/users/vaneyckt/repos","events_url":"https://api.github.com/users/vaneyckt/events{/privacy}","received_events_url":"https://api.github.com/users/vaneyckt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-27T13:16:01Z","updated_at":"2015-11-27T13:16:17Z","closed_at":null,"body":"`HEADER_PATTERN` is a regex that Sprockets uses to find the header section of a js file in preparation for scanning it for directives. `$'` is ruby shorthand for the global postmatch regex variable.\r\n\r\nUnfortunately, the `HEADER_PATTERN` regex matches until right before the final `\\n` of the js header. So this `\\n` then gets added to the `$'` var, in turn causing the `@body` var to start with a `\\n` char.\r\n\r\nThis causes js code like this\r\n\r\n```\r\n/* global Ember */\r\n/* global SwrveEmber */\r\n\r\n(function () {\r\n  \"use strict\";\r\n  ...\r\n}\r\n```\r\nto get loaded into the rails app with an extra newline, like so\r\n\r\n```\r\n/* global Ember */\r\n/* global SwrveEmber */\r\n\r\n\r\n(function () {\r\n  \"use strict\";\r\n  ...\r\n}\r\n```\r\n\r\nthis in turn doesn't play nice with code coverage tools, as the line numbers generated by the coverage tools are off by one with the line numbers of the source files."},"comment":{"url":"https://api.github.com/repos/rails/sprockets/issues/comments/160137418","html_url":"https://github.com/rails/sprockets/issues/186#issuecomment-160137418","issue_url":"https://api.github.com/repos/rails/sprockets/issues/186","id":160137418,"user":{"login":"vaneyckt","id":767465,"avatar_url":"https://avatars.githubusercontent.com/u/767465?v=3","gravatar_id":"","url":"https://api.github.com/users/vaneyckt","html_url":"https://github.com/vaneyckt","followers_url":"https://api.github.com/users/vaneyckt/followers","following_url":"https://api.github.com/users/vaneyckt/following{/other_user}","gists_url":"https://api.github.com/users/vaneyckt/gists{/gist_id}","starred_url":"https://api.github.com/users/vaneyckt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vaneyckt/subscriptions","organizations_url":"https://api.github.com/users/vaneyckt/orgs","repos_url":"https://api.github.com/users/vaneyckt/repos","events_url":"https://api.github.com/users/vaneyckt/events{/privacy}","received_events_url":"https://api.github.com/users/vaneyckt/received_events","type":"User","site_admin":false},"created_at":"2015-11-27T13:16:17Z","updated_at":"2015-11-27T13:16:17Z","body":"repro:\r\n\r\n```\r\ncomments = \"\"\r\ncomments << \"/* global Ember */\\n\"\r\ncomments << \"/* global SwrveEmber*/\\n\"\r\ncomments << \"\\n\"\r\ncomments << \"\\n\"\r\ncomments << \"\\n\"\r\ncomments << \"\\n\"\r\ncomments << \"(function () {\\n\"\r\ncomments << \"\\\"use strict\\\";\\n\"\r\ncomments << \"}\\n\"\r\n\r\ndirectives = \"\"\r\ndirectives << \"//= global Ember\\n\"\r\ndirectives << \"//= global SwrveEmber\\n\"\r\ndirectives << \"\\n\"\r\ndirectives << \"\\n\"\r\ndirectives << \"\\n\"\r\ndirectives << \"\\n\"\r\ndirectives << \"(function () {\\n\"\r\ndirectives << \"\\\"use strict\\\";\\n\"\r\ndirectives << \"}\\n\"\r\n\r\nHEADER_PATTERN = /\r\n      \\A (\r\n        (?m:\\s*) (\r\n          (\\/\\* (?m:.*?) \\*\\/) |\r\n          (\\#\\#\\# (?m:.*?) \\#\\#\\#) |\r\n          (\\/\\/ .* \\n?)+ |\r\n          (\\# .* \\n?)+\r\n        )\r\n      )+\r\n    /x\r\n\r\ndef test(data)\r\n  header = data[HEADER_PATTERN, 0] || \"\"\r\n  body   = $' || data\r\n\r\n  puts \"----------\"\r\n  puts header.dump\r\n  puts body.dump\r\n  puts \"----------\"\r\n  puts \"\\n\"\r\nend\r\n\r\ntest(comments)\r\ntest(directives)\r\n```\r\n\r\nthis gives:\r\n```\r\n----------\r\n\"/* global Ember */\\n/* global SwrveEmber*/\"      # no newline at end\r\n\"\\n\\n\\n\\n\\n(function () {\\n\\\"use strict\\\";\\n}\\n\"  # 5 newlines at start\r\n----------\r\n\r\n----------\r\n\"//= global Ember\\n//= global SwrveEmber\\n\"      # newline at end\r\n\"\\n\\n\\n\\n(function () {\\n\\\"use strict\\\";\\n}\\n\"   # 4 newlines at start\r\n----------\r\n```"}}