forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 'files-not-contents' rule to make string detection rulesets…
… easier to maintain.
- Loading branch information
Neil Zhao
committed
Sep 26, 2022
1 parent
e31bfb3
commit 093afc3
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/file-not-contents-list-with-lines-config.json", | ||
"type": "object", | ||
"properties": { | ||
"nocase": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"globsAll": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"contents": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"contextLength": { | ||
"type": "number", | ||
"default": 50 | ||
}, | ||
"flags": { | ||
"type": "string" | ||
}, | ||
"human-readable-content": { "type": "string" }, | ||
"fail-on-non-existent": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"display-line-numbers": {"type": "boolean", "default": false}, | ||
"context-length": { | ||
"type": "number", | ||
"default": 50 | ||
} | ||
}, | ||
"required": ["contents"], | ||
"oneOf": [{ "required": ["globsAll"] }, { "required": ["files"] }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2022 TODO Group. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const Result = require('../lib/result') | ||
// eslint-disable-next-line no-unused-vars | ||
const FileSystem = require('../lib/file_system') | ||
const fileNotContents = require('./file-not-contents') | ||
|
||
/** | ||
* Check if a list of files contains a list of regular expressions. | ||
* | ||
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories | ||
* @param {object} options The rule configuration | ||
* @returns {Promise<Result>} The lint rule result | ||
*/ | ||
async function filesNotContents(fs, options) { | ||
const results = await Promise.all( | ||
options.contents.map(content => { | ||
const singleOption = { ...options } | ||
delete singleOption.contents | ||
singleOption.content = content | ||
return fileNotContents(fs, singleOption) | ||
}) | ||
) | ||
|
||
const filteredResults = results.filter(r => r !== null) | ||
console.log(filteredResults) | ||
const passed = !filteredResults.find(r => !r.passed) | ||
const aggregatedTargets = filteredResults | ||
.reduce((previous, current) => { | ||
console.log(current.targets) | ||
return previous.concat(current.targets) | ||
}, []) | ||
.filter(r => !r.passed) | ||
|
||
if (passed) { | ||
return new Result( | ||
'Did not find content matching specified patterns', | ||
aggregatedTargets, | ||
passed | ||
) | ||
} | ||
return new Result('', aggregatedTargets, passed) | ||
} | ||
|
||
module.exports = filesNotContents |