Skip to content

Commit 7a6bdf9

Browse files
author
ola magdziarek
authored
feat: format and lint with new rules (#124)
* feat: format and lint with new rules
1 parent 5662a26 commit 7a6bdf9

18 files changed

Lines changed: 292 additions & 216 deletions

.circleci/config.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ params: &params
44
parameters:
55
node_version:
66
type: string
7-
default: "14"
7+
default: '14'
88
jdk_version:
99
type: string
10-
default: "8.0.292.j9-adpt"
10+
default: '8.0.292.j9-adpt'
1111
sbt_version:
1212
type: string
13-
default: "1.5.5"
13+
default: '1.5.5'
1414
semantic_release_version:
1515
type: string
16-
default: "17"
16+
default: '17'
1717

1818
test_matrix: &test_matrix
1919
matrix:
2020
parameters:
2121
node_version:
22-
- "12"
23-
- "14"
24-
- "16"
22+
- '12'
23+
- '14'
24+
- '16'
2525
jdk_version:
26-
- "8.0.292.j9-adpt"
27-
- "11.0.11.j9-adpt"
26+
- '8.0.292.j9-adpt'
27+
- '11.0.11.j9-adpt'
2828
sbt_version:
29-
- "1.5.5"
30-
- "1.7.0"
29+
- '1.5.5'
30+
- '1.7.0'
3131
jobs:
3232
test:
3333
<<: *params

.eslintrc

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
{
22
"parser": "@typescript-eslint/parser",
33
"parserOptions": {
4-
"ecmaVersion": 6
4+
"project": "./tsconfig.json"
55
},
66
"env": {
7-
"node": true,
8-
"es6": true
7+
"es2019": true,
8+
"node": true
99
},
1010
"plugins": ["@typescript-eslint"],
11-
"extends": [
12-
"eslint:recommended",
13-
"plugin:@typescript-eslint/eslint-recommended",
14-
"plugin:@typescript-eslint/recommended",
15-
"prettier",
16-
"prettier/@typescript-eslint"
17-
],
11+
"extends": ["eslint:recommended", "prettier"],
1812
"rules": {
1913
"@typescript-eslint/explicit-function-return-type": "off",
2014
"@typescript-eslint/no-explicit-any": "off",
@@ -27,16 +21,20 @@
2721
"@typescript-eslint/no-inferrable-types": "off",
2822
"@typescript-eslint/no-var-requires": "off",
2923
"@typescript-eslint/no-use-before-define": "off",
30-
"@typescript-eslint/no-unused-vars": "error",
24+
"@typescript-eslint/no-unused-vars": "warn",
25+
"no-control-regex": "off",
26+
"no-useless-escape": "off",
3127
"no-prototype-builtins": "off",
3228
"require-atomic-updates": "off",
29+
"no-unused-vars": "warn",
30+
"no-import-assign": "warn",
3331
"no-buffer-constructor": "error"
3432
},
3533
"overrides": [
3634
{
3735
"files": ["*.ts"],
3836
"rules": {
39-
"id-blacklist": ["error", "exports"] // in TS, use "export" instead of Node's "module.exports"
37+
"id-denylist": ["error", "exports"] // in TS, use "export" instead of Node's "module.exports"
4038
}
4139
}
4240
]

lib/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import * as debugModule from 'debug';
66
// To enable debugging output, run the CLI as `DEBUG=snyk-sbt-plugin snyk ...`
77
const debug = debugModule('snyk-sbt-plugin');
88

9-
import { sbtCoursierPluginName, sbtDependencyGraphPluginName, sbtDependencyGraphPluginNameNew } from './constants';
9+
import {
10+
sbtCoursierPluginName,
11+
sbtDependencyGraphPluginName,
12+
sbtDependencyGraphPluginNameNew,
13+
} from './constants';
1014
import * as subProcess from './sub-process';
1115
import * as parser from './parse-sbt';
1216
import * as types from './types';
@@ -42,8 +46,11 @@ export async function inspect(
4246
targetFile,
4347
sbtDependencyGraphPluginName,
4448
);
45-
const isNewSbtDependencyGraphPresent = await isPluginInstalled(root,
46-
targetFile, sbtDependencyGraphPluginNameNew);
49+
const isNewSbtDependencyGraphPresent = await isPluginInstalled(
50+
root,
51+
targetFile,
52+
sbtDependencyGraphPluginNameNew,
53+
);
4754

4855
debug(`isCoursierPresent: ${isCoursierPresent}, isSbtDependencyGraphPresent: ${isSbtDependencyGraphPresent},
4956
isNewSbtDependencyGraphPresent: ${isNewSbtDependencyGraphPresent}`);
@@ -129,7 +136,9 @@ async function injectSbtScript(
129136
// The Node filesystem in that case is not real: https://github.com/zeit/pkg#snapshot-filesystem
130137
// Copying the injectable script into a temp file.
131138
let projectFolderPath = path.resolve(targetFolderPath, 'project/');
132-
debug(`injectSbtScript: injecting snyk sbt plugin "${sbtPluginPath}" in "${projectFolderPath}"`);
139+
debug(
140+
`injectSbtScript: injecting snyk sbt plugin "${sbtPluginPath}" in "${projectFolderPath}"`,
141+
);
133142
if (!fs.existsSync(projectFolderPath)) {
134143
debug(`injectSbtScript: "${projectFolderPath}" does not exist`);
135144
projectFolderPath = path.resolve(targetFolderPath, '..', 'project/');
@@ -211,7 +220,7 @@ async function pluginInspect(
211220
} catch (error) {
212221
debug(
213222
'Failed to produce dependency tree with custom snyk plugin due to error: ' +
214-
error.message,
223+
error.message,
215224
);
216225
return null;
217226
} finally {

lib/parse-sbt.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import {DepDict, DepTree} from './types';
1+
import { DepDict, DepTree } from './types';
22

33
const tabdown = require('./tabdown');
44
import * as types from './types';
55

6-
export {
7-
parse,
8-
parseSbtPluginResults,
9-
};
6+
export { parse, parseSbtPluginResults };
107

118
function convertStrToTree(dependenciesTextTree) {
129
const lines = dependenciesTextTree.toString().split('\n') || [];
@@ -69,25 +66,25 @@ function convertCoursierStrToTree(dependenciesTextTree) {
6966
function walkInTree(toNode, fromNode) {
7067
if (fromNode.children && fromNode.children.length > 0) {
7168
for (const j of Object.keys(fromNode.children)) {
72-
const externalNode = getPackageNameAndVersion(
73-
fromNode.children[j].data);
69+
const externalNode = getPackageNameAndVersion(fromNode.children[j].data);
7470
if (externalNode) {
7571
const newNode = {
7672
version: externalNode.version,
7773
name: externalNode.name,
7874
dependencies: [],
7975
};
8076
toNode.dependencies.push(newNode);
81-
walkInTree(toNode.dependencies[toNode.dependencies.length - 1],
82-
fromNode.children[j]);
77+
walkInTree(
78+
toNode.dependencies[toNode.dependencies.length - 1],
79+
fromNode.children[j],
80+
);
8381
}
8482
}
8583
}
8684
delete toNode.parent;
8785
}
8886

8987
function getPackageNameAndVersion(packageDependency) {
90-
let splited;
9188
let version;
9289
let app;
9390
if (packageDependency.indexOf('(evicted by:') > -1) {
@@ -96,13 +93,13 @@ function getPackageNameAndVersion(packageDependency) {
9693
if (packageDependency.indexOf('->') > -1) {
9794
return null;
9895
}
99-
splited = packageDependency.split(':');
96+
const splited = packageDependency.split(':');
10097
version = splited[splited.length - 1];
10198
app = splited[0] + ':' + splited[1];
10299
app = app.split('\t').join('');
103100
app = app.trim();
104101
version = version.trim();
105-
return {name: app, version};
102+
return { name: app, version };
106103
}
107104

108105
function convertDepArrayToObject(depsArr) {
@@ -145,7 +142,7 @@ function createSnykTree(rootTree, name, version) {
145142

146143
function getProjectName(root) {
147144
const app = root.split(' ')[0].trim();
148-
return {name: app};
145+
return { name: app };
149146
}
150147

151148
function createCoursierSnykTree(rootTree, name, version) {
@@ -189,18 +186,26 @@ function parse(text, name, version, isCoursier): DepTree {
189186
return createSnykTree(rootTree, name, version);
190187
}
191188

192-
function parseSbtPluginResults(sbtOutput: string, packageName: string, packageVersion: string): DepTree {
189+
function parseSbtPluginResults(
190+
sbtOutput: string,
191+
packageName: string,
192+
packageVersion: string,
193+
): DepTree {
193194
// remove all other output
194195
const outputStart = 'Snyk Output Start';
195196
const outputEnd = 'Snyk Output End';
196197
const sbtProjectOutput = sbtOutput.substring(
197198
sbtOutput.indexOf(outputStart) + outputStart.length,
198-
sbtOutput.indexOf(outputEnd));
199+
sbtOutput.indexOf(outputEnd),
200+
);
199201
const sbtOutputJson: types.SbtModulesGraph = JSON.parse(sbtProjectOutput);
200202

201203
if (Object.keys(sbtOutputJson).length === 1) {
202204
const project = Object.keys(sbtOutputJson)[0];
203-
return parseSbtPluginProjectResultToDepTree(project, sbtOutputJson[project]);
205+
return parseSbtPluginProjectResultToDepTree(
206+
project,
207+
sbtOutputJson[project],
208+
);
204209
}
205210

206211
const depTree = {
@@ -211,7 +216,10 @@ function parseSbtPluginResults(sbtOutput: string, packageName: string, packageVe
211216

212217
// iterating over different project
213218
for (const project of Object.keys(sbtOutputJson)) {
214-
depTree.dependencies[project] = parseSbtPluginProjectResultToDepTree(project, sbtOutputJson[project]);
219+
depTree.dependencies[project] = parseSbtPluginProjectResultToDepTree(
220+
project,
221+
sbtOutputJson[project],
222+
);
215223
}
216224

217225
return depTree;
@@ -221,12 +229,13 @@ const PRODUCTION_SCOPES: string[] = ['compile', 'runtime', 'provided'];
221229

222230
function parseSbtPluginProjectResultToDepTree(
223231
projectKey: string,
224-
sbtProjectOutput: types.SbtModulesGraph): DepTree {
225-
226-
const pkgs = Object.keys(sbtProjectOutput.modules)
227-
.filter((module) => {
228-
return sbtProjectOutput.modules[module].configurations.some((c) => PRODUCTION_SCOPES.includes(c));
229-
});
232+
sbtProjectOutput: types.SbtModulesGraph,
233+
): DepTree {
234+
const pkgs = Object.keys(sbtProjectOutput.modules).filter((module) => {
235+
return sbtProjectOutput.modules[module].configurations.some((c) =>
236+
PRODUCTION_SCOPES.includes(c),
237+
);
238+
});
230239

231240
const getDependenciesFor = (name: string): DepTree => {
232241
if (!sbtProjectOutput.dependencies[name]) {
@@ -237,7 +246,8 @@ function parseSbtPluginProjectResultToDepTree(
237246
}
238247
const dependencies: DepDict = {};
239248
for (const subDepName of sbtProjectOutput.dependencies[name]) {
240-
if (pkgs.indexOf(subDepName) > -1) { // dependency is in production configuration
249+
if (pkgs.indexOf(subDepName) > -1) {
250+
// dependency is in production configuration
241251
dependencies[subDepName] = getDependenciesFor(subDepName);
242252
}
243253
}

lib/plugin-search.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,35 @@ export async function isPluginInstalled(
1414
}
1515

1616
// search project and project/project relative to the root
17-
function searchProjectFiles(root: string, targetFile: string, plugin: string): boolean {
17+
function searchProjectFiles(
18+
root: string,
19+
targetFile: string,
20+
plugin: string,
21+
): boolean {
1822
const basePath = path.dirname(path.resolve(root, targetFile));
19-
const sbtFileList = sbtFiles(path.join(basePath, 'project'))
20-
.concat(sbtFiles(path.join(basePath, 'project', 'project')));
21-
const searchResults = sbtFileList.map ((file) => {
23+
const sbtFileList = sbtFiles(path.join(basePath, 'project')).concat(
24+
sbtFiles(path.join(basePath, 'project', 'project')),
25+
);
26+
const searchResults = sbtFileList.map((file) => {
2227
return searchWithFs(file, plugin);
2328
});
2429
return searchResults.filter(Boolean).length > 0;
2530
}
2631

2732
// search globally installed plugins (~/.sbt)
28-
async function searchGlobalFiles(root: string, targetFile: string, plugin: string): Promise<boolean> {
33+
async function searchGlobalFiles(
34+
root: string,
35+
targetFile: string,
36+
plugin: string,
37+
): Promise<boolean> {
2938
const homedir = os.homedir();
3039
const sbtVersion = await getSbtVersion(root, targetFile);
3140
// https://www.scala-sbt.org/1.x/docs/Using-Plugins.html#Global+plugins
32-
const pluginsPath = semver.lt(sbtVersion, '1.0.0') ?
33-
path.join(homedir, '.sbt', '0.13', 'plugins') :
34-
path.join(homedir, '.sbt', '1.0', 'plugins');
41+
const pluginsPath = semver.lt(sbtVersion, '1.0.0')
42+
? path.join(homedir, '.sbt', '0.13', 'plugins')
43+
: path.join(homedir, '.sbt', '1.0', 'plugins');
3544
const sbtFileList = sbtFiles(pluginsPath);
36-
const searchResults = sbtFileList.map ((file) => {
45+
const searchResults = sbtFileList.map((file) => {
3746
return searchWithFs(file, plugin);
3847
});
3948
return searchResults.filter(Boolean).length > 0;
@@ -55,7 +64,7 @@ function sbtFiles(basePath) {
5564
}
5665

5766
function searchWithFs(filename, word) {
58-
let buffer = fs.readFileSync(filename, {encoding: 'utf8'});
67+
let buffer = fs.readFileSync(filename, { encoding: 'utf8' });
5968

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

0 commit comments

Comments
 (0)