{"action":"created","comment":{"url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/comments/48455492","id":48455492,"diff_hunk":"@@ -62,20 +64,31 @@ class UnusedImportRule extends AbstractRule {\n     private findReference(SourceCode sourceCode, String alias, String className = null) {\r\n         def aliasSameAsNonQualifiedClassName = className && className.endsWith(alias)\r\n         sourceCode.lines.find { line ->\r\n-            if (!isImportStatementForAlias(line, alias)) {\r\n-                def aliasCount = countUsage(line, alias)\r\n+            String aliasPattern = Pattern.quote(alias)\r\n+            if (!isImportStatementForAlias(line, aliasPattern)) {\r\n+                def aliasCount = countUsage(line, aliasPattern)\r\n                 return aliasSameAsNonQualifiedClassName ?\r\n-                    aliasCount && aliasCount > countUsage(line, className) : aliasCount\r\n+                        aliasCount && aliasCount > countUsage(line, Pattern.quote(className)) : aliasCount\r\n             }\r\n         }\r\n     }\r\n \r\n-    private isImportStatementForAlias(String line, String alias) {\r\n-        final IMPORT_PATTERN = /import\\s+.*/ + alias\r\n+    private isImportStatementForAlias(String line, String pattern) {\r\n+        final IMPORT_PATTERN = /import\\s+.*/ + pattern\r\n         line =~ IMPORT_PATTERN\r\n     }\r\n \r\n     private countUsage(String line, String pattern) {\r\n-        (line =~ /\\b${pattern}\\b/).count\r\n+        // Valid class name contains the following chars a-zA-Z0-9_$\r\n+        // Define boundary as\r\n+        //      right char is valid for class name\r\n+        //      left char is not valid for class name\r\n+        // or vice versa.\r\n+        // Use boundary before and after class name.\r\n+        // Yep, \\b is much shorter :)\r\n+        def regexp = /(?:(?=[a-zA-Z0-9_\\$])(?<![a-zA-Z0-9_\\$])|(?<=[a-zA-Z0-9_\\$])(?![a-zA-Z0-9_\\$]))/ +\r\n+                /${pattern}/ +\r\n+                /(?:(?=[a-zA-Z0-9_\\$])(?<![a-zA-Z0-9_\\$])|(?<=[a-zA-Z0-9_\\$])(?![a-zA-Z0-9_\\$]))/\r\n+        (line =~ regexp).count\r","path":"src/main/groovy/org/codenarc/rule/imports/UnusedImportRule.groovy","position":50,"original_position":50,"commit_id":"cb6a9bcd2d6e81191b48fca825b9d2656307e90a","original_commit_id":"cb6a9bcd2d6e81191b48fca825b9d2656307e90a","user":{"login":"chrismair","id":2054847,"avatar_url":"https://avatars.githubusercontent.com/u/2054847?v=3","gravatar_id":"","url":"https://api.github.com/users/chrismair","html_url":"https://github.com/chrismair","followers_url":"https://api.github.com/users/chrismair/followers","following_url":"https://api.github.com/users/chrismair/following{/other_user}","gists_url":"https://api.github.com/users/chrismair/gists{/gist_id}","starred_url":"https://api.github.com/users/chrismair/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrismair/subscriptions","organizations_url":"https://api.github.com/users/chrismair/orgs","repos_url":"https://api.github.com/users/chrismair/repos","events_url":"https://api.github.com/users/chrismair/events{/privacy}","received_events_url":"https://api.github.com/users/chrismair/received_events","type":"User","site_admin":false},"body":"That regular expression is pretty brutal.\r\n\r\nI was able to achieve similar results with this:\r\n        final INVALID = '[^a-zA-Z0-9_\\\\$]'\r\n        def regexp = /($INVALID|^)${pattern}($INVALID|$)/\r\n","created_at":"2015-12-27T15:13:37Z","updated_at":"2015-12-27T15:13:37Z","html_url":"https://github.com/CodeNarc/CodeNarc/pull/131#discussion_r48455492","pull_request_url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131","_links":{"self":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/comments/48455492"},"html":{"href":"https://github.com/CodeNarc/CodeNarc/pull/131#discussion_r48455492"},"pull_request":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131"}}},"pull_request":{"url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131","id":54568745,"html_url":"https://github.com/CodeNarc/CodeNarc/pull/131","diff_url":"https://github.com/CodeNarc/CodeNarc/pull/131.diff","patch_url":"https://github.com/CodeNarc/CodeNarc/pull/131.patch","issue_url":"https://api.github.com/repos/CodeNarc/CodeNarc/issues/131","number":131,"state":"open","locked":false,"title":"Fixed issue #115 UnusedImport does not handle Scala imports","user":{"login":"ychulovskyy","id":350366,"avatar_url":"https://avatars.githubusercontent.com/u/350366?v=3","gravatar_id":"","url":"https://api.github.com/users/ychulovskyy","html_url":"https://github.com/ychulovskyy","followers_url":"https://api.github.com/users/ychulovskyy/followers","following_url":"https://api.github.com/users/ychulovskyy/following{/other_user}","gists_url":"https://api.github.com/users/ychulovskyy/gists{/gist_id}","starred_url":"https://api.github.com/users/ychulovskyy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ychulovskyy/subscriptions","organizations_url":"https://api.github.com/users/ychulovskyy/orgs","repos_url":"https://api.github.com/users/ychulovskyy/repos","events_url":"https://api.github.com/users/ychulovskyy/events{/privacy}","received_events_url":"https://api.github.com/users/ychulovskyy/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-24T23:57:24Z","updated_at":"2015-12-27T15:13:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a5d2bf37f27ea55903e5792bc1f140cb2277eebc","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131/commits","review_comments_url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131/comments","review_comment_url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CodeNarc/CodeNarc/issues/131/comments","statuses_url":"https://api.github.com/repos/CodeNarc/CodeNarc/statuses/cb6a9bcd2d6e81191b48fca825b9d2656307e90a","head":{"label":"ychulovskyy:115-UnusedImport-does-not-handle-Scala-imports","ref":"115-UnusedImport-does-not-handle-Scala-imports","sha":"cb6a9bcd2d6e81191b48fca825b9d2656307e90a","user":{"login":"ychulovskyy","id":350366,"avatar_url":"https://avatars.githubusercontent.com/u/350366?v=3","gravatar_id":"","url":"https://api.github.com/users/ychulovskyy","html_url":"https://github.com/ychulovskyy","followers_url":"https://api.github.com/users/ychulovskyy/followers","following_url":"https://api.github.com/users/ychulovskyy/following{/other_user}","gists_url":"https://api.github.com/users/ychulovskyy/gists{/gist_id}","starred_url":"https://api.github.com/users/ychulovskyy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ychulovskyy/subscriptions","organizations_url":"https://api.github.com/users/ychulovskyy/orgs","repos_url":"https://api.github.com/users/ychulovskyy/repos","events_url":"https://api.github.com/users/ychulovskyy/events{/privacy}","received_events_url":"https://api.github.com/users/ychulovskyy/received_events","type":"User","site_admin":false},"repo":{"id":48453923,"name":"CodeNarc","full_name":"ychulovskyy/CodeNarc","owner":{"login":"ychulovskyy","id":350366,"avatar_url":"https://avatars.githubusercontent.com/u/350366?v=3","gravatar_id":"","url":"https://api.github.com/users/ychulovskyy","html_url":"https://github.com/ychulovskyy","followers_url":"https://api.github.com/users/ychulovskyy/followers","following_url":"https://api.github.com/users/ychulovskyy/following{/other_user}","gists_url":"https://api.github.com/users/ychulovskyy/gists{/gist_id}","starred_url":"https://api.github.com/users/ychulovskyy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ychulovskyy/subscriptions","organizations_url":"https://api.github.com/users/ychulovskyy/orgs","repos_url":"https://api.github.com/users/ychulovskyy/repos","events_url":"https://api.github.com/users/ychulovskyy/events{/privacy}","received_events_url":"https://api.github.com/users/ychulovskyy/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ychulovskyy/CodeNarc","description":"CodeNarc source","fork":true,"url":"https://api.github.com/repos/ychulovskyy/CodeNarc","forks_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/forks","keys_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/teams","hooks_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/hooks","issue_events_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/issues/events{/number}","events_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/events","assignees_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/assignees{/user}","branches_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/branches{/branch}","tags_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/tags","blobs_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/git/refs{/sha}","trees_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/statuses/{sha}","languages_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/languages","stargazers_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/stargazers","contributors_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/contributors","subscribers_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/subscribers","subscription_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/subscription","commits_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/commits{/sha}","git_commits_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/git/commits{/sha}","comments_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/comments{/number}","issue_comment_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/issues/comments{/number}","contents_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/contents/{+path}","compare_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/merges","archive_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/downloads","issues_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/issues{/number}","pulls_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/pulls{/number}","milestones_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/milestones{/number}","notifications_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/labels{/name}","releases_url":"https://api.github.com/repos/ychulovskyy/CodeNarc/releases{/id}","created_at":"2015-12-22T21:05:45Z","updated_at":"2015-12-22T21:05:49Z","pushed_at":"2015-12-27T13:09:07Z","git_url":"git://github.com/ychulovskyy/CodeNarc.git","ssh_url":"git@github.com:ychulovskyy/CodeNarc.git","clone_url":"https://github.com/ychulovskyy/CodeNarc.git","svn_url":"https://github.com/ychulovskyy/CodeNarc","homepage":null,"size":10071,"stargazers_count":0,"watchers_count":0,"language":"Groovy","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"CodeNarc:master","ref":"master","sha":"43906175a7dc5007a521c964d58e537cd8d67f4a","user":{"login":"CodeNarc","id":2061054,"avatar_url":"https://avatars.githubusercontent.com/u/2061054?v=3","gravatar_id":"","url":"https://api.github.com/users/CodeNarc","html_url":"https://github.com/CodeNarc","followers_url":"https://api.github.com/users/CodeNarc/followers","following_url":"https://api.github.com/users/CodeNarc/following{/other_user}","gists_url":"https://api.github.com/users/CodeNarc/gists{/gist_id}","starred_url":"https://api.github.com/users/CodeNarc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CodeNarc/subscriptions","organizations_url":"https://api.github.com/users/CodeNarc/orgs","repos_url":"https://api.github.com/users/CodeNarc/repos","events_url":"https://api.github.com/users/CodeNarc/events{/privacy}","received_events_url":"https://api.github.com/users/CodeNarc/received_events","type":"Organization","site_admin":false},"repo":{"id":5224329,"name":"CodeNarc","full_name":"CodeNarc/CodeNarc","owner":{"login":"CodeNarc","id":2061054,"avatar_url":"https://avatars.githubusercontent.com/u/2061054?v=3","gravatar_id":"","url":"https://api.github.com/users/CodeNarc","html_url":"https://github.com/CodeNarc","followers_url":"https://api.github.com/users/CodeNarc/followers","following_url":"https://api.github.com/users/CodeNarc/following{/other_user}","gists_url":"https://api.github.com/users/CodeNarc/gists{/gist_id}","starred_url":"https://api.github.com/users/CodeNarc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CodeNarc/subscriptions","organizations_url":"https://api.github.com/users/CodeNarc/orgs","repos_url":"https://api.github.com/users/CodeNarc/repos","events_url":"https://api.github.com/users/CodeNarc/events{/privacy}","received_events_url":"https://api.github.com/users/CodeNarc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CodeNarc/CodeNarc","description":"CodeNarc source","fork":false,"url":"https://api.github.com/repos/CodeNarc/CodeNarc","forks_url":"https://api.github.com/repos/CodeNarc/CodeNarc/forks","keys_url":"https://api.github.com/repos/CodeNarc/CodeNarc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CodeNarc/CodeNarc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CodeNarc/CodeNarc/teams","hooks_url":"https://api.github.com/repos/CodeNarc/CodeNarc/hooks","issue_events_url":"https://api.github.com/repos/CodeNarc/CodeNarc/issues/events{/number}","events_url":"https://api.github.com/repos/CodeNarc/CodeNarc/events","assignees_url":"https://api.github.com/repos/CodeNarc/CodeNarc/assignees{/user}","branches_url":"https://api.github.com/repos/CodeNarc/CodeNarc/branches{/branch}","tags_url":"https://api.github.com/repos/CodeNarc/CodeNarc/tags","blobs_url":"https://api.github.com/repos/CodeNarc/CodeNarc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CodeNarc/CodeNarc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CodeNarc/CodeNarc/git/refs{/sha}","trees_url":"https://api.github.com/repos/CodeNarc/CodeNarc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CodeNarc/CodeNarc/statuses/{sha}","languages_url":"https://api.github.com/repos/CodeNarc/CodeNarc/languages","stargazers_url":"https://api.github.com/repos/CodeNarc/CodeNarc/stargazers","contributors_url":"https://api.github.com/repos/CodeNarc/CodeNarc/contributors","subscribers_url":"https://api.github.com/repos/CodeNarc/CodeNarc/subscribers","subscription_url":"https://api.github.com/repos/CodeNarc/CodeNarc/subscription","commits_url":"https://api.github.com/repos/CodeNarc/CodeNarc/commits{/sha}","git_commits_url":"https://api.github.com/repos/CodeNarc/CodeNarc/git/commits{/sha}","comments_url":"https://api.github.com/repos/CodeNarc/CodeNarc/comments{/number}","issue_comment_url":"https://api.github.com/repos/CodeNarc/CodeNarc/issues/comments{/number}","contents_url":"https://api.github.com/repos/CodeNarc/CodeNarc/contents/{+path}","compare_url":"https://api.github.com/repos/CodeNarc/CodeNarc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CodeNarc/CodeNarc/merges","archive_url":"https://api.github.com/repos/CodeNarc/CodeNarc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CodeNarc/CodeNarc/downloads","issues_url":"https://api.github.com/repos/CodeNarc/CodeNarc/issues{/number}","pulls_url":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls{/number}","milestones_url":"https://api.github.com/repos/CodeNarc/CodeNarc/milestones{/number}","notifications_url":"https://api.github.com/repos/CodeNarc/CodeNarc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CodeNarc/CodeNarc/labels{/name}","releases_url":"https://api.github.com/repos/CodeNarc/CodeNarc/releases{/id}","created_at":"2012-07-29T18:03:23Z","updated_at":"2015-12-11T07:41:42Z","pushed_at":"2015-12-27T13:09:23Z","git_url":"git://github.com/CodeNarc/CodeNarc.git","ssh_url":"git@github.com:CodeNarc/CodeNarc.git","clone_url":"https://github.com/CodeNarc/CodeNarc.git","svn_url":"https://github.com/CodeNarc/CodeNarc","homepage":null,"size":10141,"stargazers_count":72,"watchers_count":72,"language":"Groovy","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":37,"mirror_url":null,"open_issues_count":15,"forks":37,"open_issues":15,"watchers":72,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131"},"html":{"href":"https://github.com/CodeNarc/CodeNarc/pull/131"},"issue":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/issues/131"},"comments":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/issues/131/comments"},"review_comments":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131/comments"},"review_comment":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/pulls/131/commits"},"statuses":{"href":"https://api.github.com/repos/CodeNarc/CodeNarc/statuses/cb6a9bcd2d6e81191b48fca825b9d2656307e90a"}}}}