Skip to content

Commit

Permalink
feat: add 'files-not-contents' rule to make string detection rulesets…
Browse files Browse the repository at this point in the history
… easier to maintain.
  • Loading branch information
Neil Zhao committed Sep 26, 2022
1 parent e31bfb3 commit 093afc3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
38 changes: 38 additions & 0 deletions rules/files-not-contents-config.json
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"] }]
}
47 changes: 47 additions & 0 deletions rules/files-not-contents.js
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

0 comments on commit 093afc3

Please sign in to comment.