Skip to content

Commit 093afc3

Browse files
author
Neil Zhao
committed
feat: add 'files-not-contents' rule to make string detection rulesets easier to maintain.
1 parent e31bfb3 commit 093afc3

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

rules/files-not-contents-config.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/file-not-contents-list-with-lines-config.json",
4+
"type": "object",
5+
"properties": {
6+
"nocase": {
7+
"type": "boolean",
8+
"default": false
9+
},
10+
"globsAll": {
11+
"type": "array",
12+
"items": { "type": "string" }
13+
},
14+
"contents": {
15+
"type": "array",
16+
"items": { "type": "string" }
17+
},
18+
"contextLength": {
19+
"type": "number",
20+
"default": 50
21+
},
22+
"flags": {
23+
"type": "string"
24+
},
25+
"human-readable-content": { "type": "string" },
26+
"fail-on-non-existent": {
27+
"type": "boolean",
28+
"default": false
29+
},
30+
"display-line-numbers": {"type": "boolean", "default": false},
31+
"context-length": {
32+
"type": "number",
33+
"default": 50
34+
}
35+
},
36+
"required": ["contents"],
37+
"oneOf": [{ "required": ["globsAll"] }, { "required": ["files"] }]
38+
}

rules/files-not-contents.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2022 TODO Group. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// eslint-disable-next-line no-unused-vars
5+
const Result = require('../lib/result')
6+
// eslint-disable-next-line no-unused-vars
7+
const FileSystem = require('../lib/file_system')
8+
const fileNotContents = require('./file-not-contents')
9+
10+
/**
11+
* Check if a list of files contains a list of regular expressions.
12+
*
13+
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories
14+
* @param {object} options The rule configuration
15+
* @returns {Promise<Result>} The lint rule result
16+
*/
17+
async function filesNotContents(fs, options) {
18+
const results = await Promise.all(
19+
options.contents.map(content => {
20+
const singleOption = { ...options }
21+
delete singleOption.contents
22+
singleOption.content = content
23+
return fileNotContents(fs, singleOption)
24+
})
25+
)
26+
27+
const filteredResults = results.filter(r => r !== null)
28+
console.log(filteredResults)
29+
const passed = !filteredResults.find(r => !r.passed)
30+
const aggregatedTargets = filteredResults
31+
.reduce((previous, current) => {
32+
console.log(current.targets)
33+
return previous.concat(current.targets)
34+
}, [])
35+
.filter(r => !r.passed)
36+
37+
if (passed) {
38+
return new Result(
39+
'Did not find content matching specified patterns',
40+
aggregatedTargets,
41+
passed
42+
)
43+
}
44+
return new Result('', aggregatedTargets, passed)
45+
}
46+
47+
module.exports = filesNotContents

0 commit comments

Comments
 (0)