Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Commit

Permalink
Adding sample on re-using existing tslint output
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablissimo committed Feb 5, 2017
1 parent 25a8523 commit 5efdbe1
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 0 deletions.
6 changes: 6 additions & 0 deletions samples/using-existing-tslint-output/.gitignore
@@ -0,0 +1,6 @@
node_modules/
tmp/
src/*.js
test/*.js
*.map
html/
35 changes: 35 additions & 0 deletions samples/using-existing-tslint-output/README.md
@@ -0,0 +1,35 @@
#Using existing tslint output example
This sample project can be analysed by SonarQube to demonstrate re-using the output of a build-step ```tslint``` pass, instead of having the plugin perform the analysis itself.

You can see a live example of the results of analysing this project at [https://sonar.pablissimo.com](https://sonar.pablissimo.com/dashboard?id=com.pablissimo.sonar%3Ausing-existing-tslint-output).

This sample is identical to the basic-setup sample but with reuse of tslint output.

##Building and analysing

If you want, you can just analyse this project directly as the tslint output has already been generated. However, to rebuild fully:

* Run ```npm install``` from the cloned repo folder
* Run ```npm test``` to run unit tests and ```tslint``` analysis, building the ```issues.json``` output file.

To analyse with SonarQube just run ```sonar-scanner -X``` from the cloned repo folder.

* The -X flag will give us diagnostic information during the run, so you can see what the plugin is up to

##Breaking down the sonar-project.properties file

The sample has a ```sonar-project.properties``` file that controls how the analysis gets run. This file differs from the basic-setup example only in one interesting respect:

<table>
<thead><tr><th>Line</th><th>Description</th></tr></thead>
<tbody>
<tr><td>sonar.ts.tslint.outputPath=issues.json</td><td>Tells the plugin to skip running tslint itself, and instead just parse the output of the previous tslint path found in the issues.json file</td></tr>
</tbody>
</table>

See the basic-setup example for detail on the other configured settings.

##Retionale
It's possible that your CI build already performs a ```tslint``` path, since a tslint failure might be considered important enough to break your build (or at least otherwise report on). Since a ```tslint``` path on a large project can take a while, the ```sonar.ts.tslint.outputPath``` setting can be set to reuse the output of the CI call to ```tslint```, reducing the time it takes to perform analysis.

It also allows you to call ```tslint``` with parameters or configuration that the plugin may not easily handle, or otherwise filter or transform the ```tslint``` output before it gets consumed by the plugin.
55 changes: 55 additions & 0 deletions samples/using-existing-tslint-output/coverage/lcov.info
@@ -0,0 +1,55 @@
TN:
SF:C:\Projects\SonarTsPlugin\samples\using-existing-tslint-output\src\MyApp.ts
FN:1,(anonymous_3)
FN:7,(anonymous_4)
FN:8,MyStringUtils
FN:11,(anonymous_6)
FN:24,(anonymous_7)
FN:25,(anonymous_9)
FNF:6
FNH:5
FNDA:1,(anonymous_3)
FNDA:1,(anonymous_4)
FNDA:1,MyStringUtils
FNDA:1,(anonymous_6)
FNDA:1,(anonymous_7)
FNDA:0,(anonymous_9)
DA:1,2
DA:7,1
DA:8,1
DA:11,1
DA:12,1
DA:13,1
DA:16,0
DA:18,1
DA:24,2
DA:25,1
DA:26,0
DA:29,1
LF:12
LH:10
BRDA:12,1,0,1
BRDA:12,1,1,0
BRDA:1,2,0,1
BRDA:1,2,1,1
BRF:4
BRH:3
end_of_record
TN:
SF:C:\Projects\SonarTsPlugin\samples\using-existing-tslint-output\test\MyAppTests.ts
FN:3,(anonymous_3)
FN:4,(anonymous_4)
FNF:2
FNH:2
FNDA:1,(anonymous_3)
FNDA:1,(anonymous_4)
DA:1,1
DA:3,1
DA:4,1
DA:5,1
DA:10,1
LF:5
LH:5
BRF:0
BRH:0
end_of_record
1 change: 1 addition & 0 deletions samples/using-existing-tslint-output/issues.json
@@ -0,0 +1 @@
[{"endPosition":{"character":29,"line":15,"position":497},"failure":"else statements must be braced","name":"src/MyApp.ts","ruleName":"curly","startPosition":{"character":12,"line":14,"position":462}},{"endPosition":{"character":13,"line":0,"position":13},"failure":"The internal 'module' syntax is deprecated, use the 'namespace' keyword instead.","name":"src/MyApp.ts","ruleName":"no-internal-module","startPosition":{"character":7,"line":0,"position":7}},{"endPosition":{"character":21,"line":11,"position":397},"failure":"== should be ===","name":"src/MyApp.ts","ruleName":"triple-equals","startPosition":{"character":19,"line":11,"position":395}},{"endPosition":{"character":36,"line":24,"position":731},"failure":"missing whitespace","fix":{"innerRuleName":"whitespace","innerReplacements":[{"innerStart":730,"innerLength":0,"innerText":" "}]},"name":"src/MyApp.ts","ruleName":"whitespace","startPosition":{"character":35,"line":24,"position":730}},{"endPosition":{"character":43,"line":24,"position":738},"failure":"missing whitespace","fix":{"innerRuleName":"whitespace","innerReplacements":[{"innerStart":737,"innerLength":0,"innerText":" "}]},"name":"src/MyApp.ts","ruleName":"whitespace","startPosition":{"character":42,"line":24,"position":737}},{"endPosition":{"character":11,"line":4,"position":155},"failure":"Forbidden 'var' keyword, use 'let' or 'const' instead","fix":{"innerRuleName":"no-var-keyword","innerReplacements":[{"innerStart":152,"innerLength":3,"innerText":"let"}]},"name":"test/MyAppTests.ts","ruleName":"no-var-keyword","startPosition":{"character":8,"line":4,"position":152}}]
25 changes: 25 additions & 0 deletions samples/using-existing-tslint-output/karma.conf.js
@@ -0,0 +1,25 @@
module.exports = function (config) {
config.set({
frameworks: ["jasmine", "karma-typescript"],
files: [
{ pattern: "src/**/*.ts" },
{ pattern: "test/**/*.ts" }
],
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
reporters: ["progress", "karma-typescript"],
browsers: ["PhantomJS"],

karmaTypescriptConfig: {
reports: {
"lcovonly": {
directory: "coverage",
subdirectory: ".",
filename: "lcov.info"
}
},
tsconfig: "./tsconfig.json"
}
});
};
25 changes: 25 additions & 0 deletions samples/using-existing-tslint-output/package.json
@@ -0,0 +1,25 @@
{
"name": "basic-app",
"version": "1.0.0",

"scripts": {
"test": "karma start --single-run --code-coverage && tslint --force --format json --config tslint.json --out issues.json src/MyApp.ts test/MyAppTests.ts"
},

"dependencies": {
"@types/jasmine": "^2.5.35",
"jasmine-core": "^2.5.2",
"karma": "^1.4.1",
"karma-phantomjs-launcher": "^1.0.2",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.1.0",
"karma-typescript": "^2.1.7",
"typescript": "latest",
"tslint": "^4.4.2"
},

"devDependencies": {
"gulp": "^3.9.1",
"karma-typescript": "^2.1.7"
}
}
13 changes: 13 additions & 0 deletions samples/using-existing-tslint-output/sonar-project.properties
@@ -0,0 +1,13 @@
sonar.projectKey=com.pablissimo.sonar:using-existing-tslint-output
sonar.projectName=Simulating reusing the output of a build-step tslint pass
sonar.projectVersion=1.0

sonar.sources=./src
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**

sonar.tests=./test

sonar.ts.tslint.outputPath=issues.json
# To import the LCOV report
sonar.ts.coverage.lcovReportPath=coverage/lcov.info
30 changes: 30 additions & 0 deletions samples/using-existing-tslint-output/src/MyApp.ts
@@ -0,0 +1,30 @@
export module MyApp {
/*
Contains a couple of methods that our test class will call, and
some awful code that will cause tslint to flag a couple of basic
issues
*/
export class MyStringUtils {
constructor(public dummy1: string, public dummy2: string) {
}

public areStringsEqual(s1: string, s2: string) {
if (s1 == s2) {
return true;
}
else
return false;
}
}

/*
Contains a method that none of the test code hits, and
that tslint will flag another couple of issues on
*/
export class MyUncoveredClass {
public justReturn42(param1:string,param2: number) {
return 42;
}

}
}
12 changes: 12 additions & 0 deletions samples/using-existing-tslint-output/test/MyAppTests.ts
@@ -0,0 +1,12 @@
import { MyApp } from "../src/MyApp";

describe("areStringsEqual", () => {
it("should return true if two strings are the same", () => {
var result =
new MyApp
.MyStringUtils("whatever", "whoever")
.areStringsEqual("t", "t");

expect(result).toEqual(true);
});
});
16 changes: 16 additions & 0 deletions samples/using-existing-tslint-output/tsconfig.json
@@ -0,0 +1,16 @@
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"outDir": "tmp",
"sourceMap": true,
"target": "ES5",
"types": [
"jasmine"
]
},
"exclude": [
"node_modules"
]
}
102 changes: 102 additions & 0 deletions samples/using-existing-tslint-output/tslint.json
@@ -0,0 +1,102 @@
{
"jsRules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-duplicate-variable": true,
"no-eval": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"double"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
},
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"curly": true,
"indent": [
true,
"spaces"
],
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"no-var-keyword": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"double"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}

0 comments on commit 5efdbe1

Please sign in to comment.