Skip to content

Commit

Permalink
feat: format and lint with new rules (#124)
Browse files Browse the repository at this point in the history
* feat: format and lint with new rules
  • Loading branch information
ola magdziarek committed Dec 6, 2022
1 parent 5662a26 commit 7a6bdf9
Show file tree
Hide file tree
Showing 18 changed files with 292 additions and 216 deletions.
22 changes: 11 additions & 11 deletions .circleci/config.yml
Expand Up @@ -4,30 +4,30 @@ params: &params
parameters:
node_version:
type: string
default: "14"
default: '14'
jdk_version:
type: string
default: "8.0.292.j9-adpt"
default: '8.0.292.j9-adpt'
sbt_version:
type: string
default: "1.5.5"
default: '1.5.5'
semantic_release_version:
type: string
default: "17"
default: '17'

test_matrix: &test_matrix
matrix:
parameters:
node_version:
- "12"
- "14"
- "16"
- '12'
- '14'
- '16'
jdk_version:
- "8.0.292.j9-adpt"
- "11.0.11.j9-adpt"
- '8.0.292.j9-adpt'
- '11.0.11.j9-adpt'
sbt_version:
- "1.5.5"
- "1.7.0"
- '1.5.5'
- '1.7.0'
jobs:
test:
<<: *params
Expand Down
22 changes: 10 additions & 12 deletions .eslintrc
@@ -1,20 +1,14 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6
"project": "./tsconfig.json"
},
"env": {
"node": true,
"es6": true
"es2019": true,
"node": true
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"extends": ["eslint:recommended", "prettier"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand All @@ -27,16 +21,20 @@
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-unused-vars": "warn",
"no-control-regex": "off",
"no-useless-escape": "off",
"no-prototype-builtins": "off",
"require-atomic-updates": "off",
"no-unused-vars": "warn",
"no-import-assign": "warn",
"no-buffer-constructor": "error"
},
"overrides": [
{
"files": ["*.ts"],
"rules": {
"id-blacklist": ["error", "exports"] // in TS, use "export" instead of Node's "module.exports"
"id-denylist": ["error", "exports"] // in TS, use "export" instead of Node's "module.exports"
}
}
]
Expand Down
19 changes: 14 additions & 5 deletions lib/index.ts
Expand Up @@ -6,7 +6,11 @@ import * as debugModule from 'debug';
// To enable debugging output, run the CLI as `DEBUG=snyk-sbt-plugin snyk ...`
const debug = debugModule('snyk-sbt-plugin');

import { sbtCoursierPluginName, sbtDependencyGraphPluginName, sbtDependencyGraphPluginNameNew } from './constants';
import {
sbtCoursierPluginName,
sbtDependencyGraphPluginName,
sbtDependencyGraphPluginNameNew,
} from './constants';
import * as subProcess from './sub-process';
import * as parser from './parse-sbt';
import * as types from './types';
Expand Down Expand Up @@ -42,8 +46,11 @@ export async function inspect(
targetFile,
sbtDependencyGraphPluginName,
);
const isNewSbtDependencyGraphPresent = await isPluginInstalled(root,
targetFile, sbtDependencyGraphPluginNameNew);
const isNewSbtDependencyGraphPresent = await isPluginInstalled(
root,
targetFile,
sbtDependencyGraphPluginNameNew,
);

debug(`isCoursierPresent: ${isCoursierPresent}, isSbtDependencyGraphPresent: ${isSbtDependencyGraphPresent},
isNewSbtDependencyGraphPresent: ${isNewSbtDependencyGraphPresent}`);
Expand Down Expand Up @@ -129,7 +136,9 @@ async function injectSbtScript(
// The Node filesystem in that case is not real: https://github.com/zeit/pkg#snapshot-filesystem
// Copying the injectable script into a temp file.
let projectFolderPath = path.resolve(targetFolderPath, 'project/');
debug(`injectSbtScript: injecting snyk sbt plugin "${sbtPluginPath}" in "${projectFolderPath}"`);
debug(
`injectSbtScript: injecting snyk sbt plugin "${sbtPluginPath}" in "${projectFolderPath}"`,
);
if (!fs.existsSync(projectFolderPath)) {
debug(`injectSbtScript: "${projectFolderPath}" does not exist`);
projectFolderPath = path.resolve(targetFolderPath, '..', 'project/');
Expand Down Expand Up @@ -211,7 +220,7 @@ async function pluginInspect(
} catch (error) {
debug(
'Failed to produce dependency tree with custom snyk plugin due to error: ' +
error.message,
error.message,
);
return null;
} finally {
Expand Down
58 changes: 34 additions & 24 deletions lib/parse-sbt.ts
@@ -1,12 +1,9 @@
import {DepDict, DepTree} from './types';
import { DepDict, DepTree } from './types';

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

export {
parse,
parseSbtPluginResults,
};
export { parse, parseSbtPluginResults };

function convertStrToTree(dependenciesTextTree) {
const lines = dependenciesTextTree.toString().split('\n') || [];
Expand Down Expand Up @@ -69,25 +66,25 @@ function convertCoursierStrToTree(dependenciesTextTree) {
function walkInTree(toNode, fromNode) {
if (fromNode.children && fromNode.children.length > 0) {
for (const j of Object.keys(fromNode.children)) {
const externalNode = getPackageNameAndVersion(
fromNode.children[j].data);
const externalNode = getPackageNameAndVersion(fromNode.children[j].data);
if (externalNode) {
const newNode = {
version: externalNode.version,
name: externalNode.name,
dependencies: [],
};
toNode.dependencies.push(newNode);
walkInTree(toNode.dependencies[toNode.dependencies.length - 1],
fromNode.children[j]);
walkInTree(
toNode.dependencies[toNode.dependencies.length - 1],
fromNode.children[j],
);
}
}
}
delete toNode.parent;
}

function getPackageNameAndVersion(packageDependency) {
let splited;
let version;
let app;
if (packageDependency.indexOf('(evicted by:') > -1) {
Expand All @@ -96,13 +93,13 @@ function getPackageNameAndVersion(packageDependency) {
if (packageDependency.indexOf('->') > -1) {
return null;
}
splited = packageDependency.split(':');
const splited = packageDependency.split(':');
version = splited[splited.length - 1];
app = splited[0] + ':' + splited[1];
app = app.split('\t').join('');
app = app.trim();
version = version.trim();
return {name: app, version};
return { name: app, version };
}

function convertDepArrayToObject(depsArr) {
Expand Down Expand Up @@ -145,7 +142,7 @@ function createSnykTree(rootTree, name, version) {

function getProjectName(root) {
const app = root.split(' ')[0].trim();
return {name: app};
return { name: app };
}

function createCoursierSnykTree(rootTree, name, version) {
Expand Down Expand Up @@ -189,18 +186,26 @@ function parse(text, name, version, isCoursier): DepTree {
return createSnykTree(rootTree, name, version);
}

function parseSbtPluginResults(sbtOutput: string, packageName: string, packageVersion: string): DepTree {
function parseSbtPluginResults(
sbtOutput: string,
packageName: string,
packageVersion: string,
): DepTree {
// remove all other output
const outputStart = 'Snyk Output Start';
const outputEnd = 'Snyk Output End';
const sbtProjectOutput = sbtOutput.substring(
sbtOutput.indexOf(outputStart) + outputStart.length,
sbtOutput.indexOf(outputEnd));
sbtOutput.indexOf(outputEnd),
);
const sbtOutputJson: types.SbtModulesGraph = JSON.parse(sbtProjectOutput);

if (Object.keys(sbtOutputJson).length === 1) {
const project = Object.keys(sbtOutputJson)[0];
return parseSbtPluginProjectResultToDepTree(project, sbtOutputJson[project]);
return parseSbtPluginProjectResultToDepTree(
project,
sbtOutputJson[project],
);
}

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

// iterating over different project
for (const project of Object.keys(sbtOutputJson)) {
depTree.dependencies[project] = parseSbtPluginProjectResultToDepTree(project, sbtOutputJson[project]);
depTree.dependencies[project] = parseSbtPluginProjectResultToDepTree(
project,
sbtOutputJson[project],
);
}

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

function parseSbtPluginProjectResultToDepTree(
projectKey: string,
sbtProjectOutput: types.SbtModulesGraph): DepTree {

const pkgs = Object.keys(sbtProjectOutput.modules)
.filter((module) => {
return sbtProjectOutput.modules[module].configurations.some((c) => PRODUCTION_SCOPES.includes(c));
});
sbtProjectOutput: types.SbtModulesGraph,
): DepTree {
const pkgs = Object.keys(sbtProjectOutput.modules).filter((module) => {
return sbtProjectOutput.modules[module].configurations.some((c) =>
PRODUCTION_SCOPES.includes(c),
);
});

const getDependenciesFor = (name: string): DepTree => {
if (!sbtProjectOutput.dependencies[name]) {
Expand All @@ -237,7 +246,8 @@ function parseSbtPluginProjectResultToDepTree(
}
const dependencies: DepDict = {};
for (const subDepName of sbtProjectOutput.dependencies[name]) {
if (pkgs.indexOf(subDepName) > -1) { // dependency is in production configuration
if (pkgs.indexOf(subDepName) > -1) {
// dependency is in production configuration
dependencies[subDepName] = getDependenciesFor(subDepName);
}
}
Expand Down
29 changes: 19 additions & 10 deletions lib/plugin-search.ts
Expand Up @@ -14,26 +14,35 @@ export async function isPluginInstalled(
}

// search project and project/project relative to the root
function searchProjectFiles(root: string, targetFile: string, plugin: string): boolean {
function searchProjectFiles(
root: string,
targetFile: string,
plugin: string,
): boolean {
const basePath = path.dirname(path.resolve(root, targetFile));
const sbtFileList = sbtFiles(path.join(basePath, 'project'))
.concat(sbtFiles(path.join(basePath, 'project', 'project')));
const searchResults = sbtFileList.map ((file) => {
const sbtFileList = sbtFiles(path.join(basePath, 'project')).concat(
sbtFiles(path.join(basePath, 'project', 'project')),
);
const searchResults = sbtFileList.map((file) => {
return searchWithFs(file, plugin);
});
return searchResults.filter(Boolean).length > 0;
}

// search globally installed plugins (~/.sbt)
async function searchGlobalFiles(root: string, targetFile: string, plugin: string): Promise<boolean> {
async function searchGlobalFiles(
root: string,
targetFile: string,
plugin: string,
): Promise<boolean> {
const homedir = os.homedir();
const sbtVersion = await getSbtVersion(root, targetFile);
// https://www.scala-sbt.org/1.x/docs/Using-Plugins.html#Global+plugins
const pluginsPath = semver.lt(sbtVersion, '1.0.0') ?
path.join(homedir, '.sbt', '0.13', 'plugins') :
path.join(homedir, '.sbt', '1.0', 'plugins');
const pluginsPath = semver.lt(sbtVersion, '1.0.0')
? path.join(homedir, '.sbt', '0.13', 'plugins')
: path.join(homedir, '.sbt', '1.0', 'plugins');
const sbtFileList = sbtFiles(pluginsPath);
const searchResults = sbtFileList.map ((file) => {
const searchResults = sbtFileList.map((file) => {
return searchWithFs(file, plugin);
});
return searchResults.filter(Boolean).length > 0;
Expand All @@ -55,7 +64,7 @@ function sbtFiles(basePath) {
}

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

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

0 comments on commit 7a6bdf9

Please sign in to comment.