Skip to content

Commit

Permalink
Merge pull request #107 from snyk/feat/ignore-comments
Browse files Browse the repository at this point in the history
feat: ignore single-line comments in plugin search
  • Loading branch information
Jdunsby committed Jul 19, 2022
2 parents ebf261f + 4ff22d0 commit c6a1177
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -19,9 +19,9 @@ test_matrix: &test_matrix
matrix:
parameters:
node_version:
- "10"
- "12"
- "14"
- "16"
jdk_version:
- "8.0.292.j9-adpt"
- "11.0.11.j9-adpt"
Expand Down
7 changes: 6 additions & 1 deletion lib/plugin-search.ts
Expand Up @@ -55,6 +55,11 @@ function sbtFiles(basePath) {
}

function searchWithFs(filename, word) {
const buffer = fs.readFileSync(filename);
let buffer = fs.readFileSync(filename, {encoding: 'utf8'});

// remove single-line and multi-line comments
const singleLineCommentPattern = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm;
buffer = buffer.replace(singleLineCommentPattern, '');

return buffer.indexOf(word) > -1;
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"tslint": "5.16.0",
"typescript": "3.9.7"
"typescript": "^4.7.4"
},
"dependencies": {
"debug": "^4.1.1",
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/multi-line-comments/build.sbt
@@ -0,0 +1,10 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

lazy val hello = (project in file("."))
.settings(
name := "Hello",
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.2",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.7" % Test,
)
5 changes: 5 additions & 0 deletions test/fixtures/multi-line-comments/project/Dependencies.scala
@@ -0,0 +1,5 @@
import sbt._

object Dependencies {
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
}
1 change: 1 addition & 0 deletions test/fixtures/multi-line-comments/project/build.properties
@@ -0,0 +1 @@
sbt.version=1.2.7
13 changes: 13 additions & 0 deletions test/fixtures/multi-line-comments/project/site.sbt
@@ -0,0 +1,13 @@
/* addDependencyTreePlugin
hello,
this is the comment that we want to ignore -> addDependencyTreePlugin <- Did it work?
we shouldn't be taking these words into account (addDependencyTreePlugin)
*/
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.8.2") /* addDependencyTreePlugin
//// addDependencyTreePlugin
*/
"http://nothing-to-see.com/uh-oh" /* addDependencyTreePlugin */
"http://nothing-to-see.com/uh-oh" /* addDependencyTreePlugin
addDependencyTreePlugin
*/
@@ -0,0 +1,9 @@
package example

object Hello extends Greeting with App {
println(greeting)
}

trait Greeting {
lazy val greeting: String = "hello"
}
@@ -0,0 +1,9 @@
package example

import org.scalatest._

class HelloSpec extends FlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
}
10 changes: 10 additions & 0 deletions test/fixtures/single-line-comments/build.sbt
@@ -0,0 +1,10 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

lazy val hello = (project in file("."))
.settings(
name := "Hello",
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.2",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.7" % Test,
)
5 changes: 5 additions & 0 deletions test/fixtures/single-line-comments/project/Dependencies.scala
@@ -0,0 +1,5 @@
import sbt._

object Dependencies {
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
}
@@ -0,0 +1 @@
sbt.version=1.2.7
5 changes: 5 additions & 0 deletions test/fixtures/single-line-comments/project/site.sbt
@@ -0,0 +1,5 @@
// hello, this is the comment that we want to ignore -> addDependencyTreePlugin <- Did it work?
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.8.2") // addDependencyTreePlugin
//// addDependencyTreePlugin

"http://nothing-to-see.com/uh-oh" //addDependencyTreePlugin
@@ -0,0 +1,9 @@
package example

object Hello extends Greeting with App {
println(greeting)
}

trait Greeting {
lazy val greeting: String = "hello"
}
@@ -0,0 +1,9 @@
package example

import org.scalatest._

class HelloSpec extends FlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
}
12 changes: 12 additions & 0 deletions test/functional/plugin-search.spec.ts
Expand Up @@ -24,6 +24,18 @@ describe('plugin-search test', () => {
const received = await isPluginInstalled(root, targetFile, 'will.not.find')
expect(received).toBe(false);
});
it('returns false if the project directory has sbt file with plugin name in a single-line comment', async () => {
const root = path.join(__dirname, '..', 'fixtures');
const targetFile = path.join('single-line-comments', 'build.sbt');
const received = await isPluginInstalled(root, targetFile, 'addDependencyTreePlugin')
expect(received).toBe(false);
});
it('returns false if the project directory has sbt file with plugin name in a multi-line comment', async () => {
const root = path.join(__dirname, '..', 'fixtures');
const targetFile = path.join('multi-line-comments', 'build.sbt');
const received = await isPluginInstalled(root, targetFile, 'addDependencyTreePlugin')
expect(received).toBe(false);
});
});
describe('in local project/project folder', () => {
it('returns true if the project/project directory has sbt file with given plugin name', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/system/plugin.test.ts
Expand Up @@ -104,7 +104,7 @@ test('run inspect() with commented out coursier on 0.13', async (t) => {
const result: any = await plugin.inspect(path.join(
__dirname, '..', 'fixtures', 'testproj-faux-coursier-0.13'),
'build.sbt', {});
t.equal(result.plugin.name, 'bundled:sbt', 'correct handler');
t.equal(result.plugin.name, 'snyk:sbt', 'correct handler');
// TODO: fix to get the project name from build.sbt
// t.equal(result.package.version, '0.1.0-SNAPSHOT');
t.match(result.package.name, 'hello');
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -11,6 +11,7 @@
"allowJs": true,
"sourceMap": true,
"strict": true,
"useUnknownInCatchVariables": false,
// "declaration": true,
"importHelpers": true,
"noImplicitAny": false // Needed to compile tap and ansi-escapes
Expand Down

0 comments on commit c6a1177

Please sign in to comment.