Skip to content

Commit

Permalink
Add tests, finalize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Deis authored and Nicholas Deis committed Nov 30, 2024
1 parent eb2db41 commit c362b91
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 75 deletions.
70 changes: 58 additions & 12 deletions dist/no-pattern-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ function globalizeAllRegularExps(patterns) {
globalizeRegularExpression(pattern),
]));
}
function parseAndValidateOptions({ additionalPatterns }) {
const compiledRegexes = (0, utils_1.validateRecordOfRegex)((0, utils_1.plainObjectOption)(additionalPatterns, "additionalPatterns", utils_1.DEFAULT_ADDTIONAL_REGEXES));
function parseAndValidateOptions({ patterns }) {
const compiledRegexes = (0, utils_1.validateRecordOfRegex)((0, utils_1.plainObjectOption)(patterns, "patterns", utils_1.DEFAULT_ADDTIONAL_REGEXES));
return {
additionalPatterns: compiledRegexes,
patterns: compiledRegexes,
};
}
function findAllNewLines(text) {
Expand Down Expand Up @@ -76,31 +76,77 @@ function findLineAndColNoFromMatchIdx(startIdx, linesIdx, matchLength) {
}
return { endIdx, startIdx, lineSelections };
}
function serializeTextSelections(textAreaSelection) {
return textAreaSelection.lineSelections
.map((line) => {
return `${line.lineNo}:${line.startCol}-${line.endCol}`;
})
.join(",");
}
function findStartAndEndTextSelection(textAreaSelection) {
const start = {
column: Infinity,
line: Infinity,
};
const end = {
line: 0,
column: 0,
};
for (const line of textAreaSelection.lineSelections) {
const min = Math.min(line.lineNo, start.line);
if (line.lineNo === min) {
start.line = min;
start.column = line.startCol;
}
const max = Math.max(line.lineNo, end.line);
if (line.lineNo === max) {
end.line = max;
end.column = line.endCol;
}
}
return {
start,
end,
};
}
const FULL_TEXT_MATCH_MESSAGE = `Found text that matches the pattern "{{ patternName }}": {{ textMatch }}`;
const noPatternMatch = {
meta: {
schema: false,
messages: {
[utils_1.FULL_TEXT_MATCH]: FULL_TEXT_MATCH_MESSAGE,
},
docs: {
description: "An eslint rule that does pattern matching against an entire file",
category: "Best Practices",
},
},
create(context) {
var _a;
const { additionalPatterns } = parseAndValidateOptions(context.options[0] || {});
const { patterns } = parseAndValidateOptions(context.options[0] || {});
const sourceCode = ((_a = context === null || context === void 0 ? void 0 : context.getSourceCode) === null || _a === void 0 ? void 0 : _a.call(context)) || context.sourceCode;
const patterns = Object.entries(additionalPatterns);
const patternList = Object.entries(patterns);
const text = sourceCode.text;
const newLinePos = findAllNewLines(text);
for (const [name, pattern] of patterns) {
const matches = patternList
.map(([name, pattern]) => {
const globalPattern = globalizeRegularExpression(pattern);
const matches = Array.from(text.matchAll(globalPattern));
for (const m of matches) {
return matches.map((m) => {
const idx = m.index;
const match = m[0];
const meta = findLineAndColNoFromMatchIdx(idx, newLinePos, match.length);
//console.dir({ match, meta }, { depth: 3 });
}
}
const textMatch = m[0];
const lineAndColNumbers = findLineAndColNoFromMatchIdx(idx, newLinePos, textMatch.length);
return { lineAndColNumbers, textMatch, patternName: name };
});
})
.flat();
matches.forEach(({ patternName, textMatch, lineAndColNumbers }) => {
context.report({
data: { patternName, textMatch },
messageId: utils_1.FULL_TEXT_MATCH,
loc: findStartAndEndTextSelection(lineAndColNumbers),
});
});
return {};
},
};
Expand Down
3 changes: 2 additions & 1 deletion dist/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PATTERN_MATCH = exports.HIGH_ENTROPY = exports.DEFAULT_ADDTIONAL_REGEXES = void 0;
exports.FULL_TEXT_MATCH = exports.PATTERN_MATCH = exports.HIGH_ENTROPY = exports.DEFAULT_ADDTIONAL_REGEXES = void 0;
exports.isPlainObject = isPlainObject;
exports.plainObjectOption = plainObjectOption;
exports.validateRecordOfRegex = validateRecordOfRegex;
Expand Down Expand Up @@ -164,3 +164,4 @@ function getAssignmentName(node) {
}
exports.HIGH_ENTROPY = "HIGH_ENTROPY";
exports.PATTERN_MATCH = "PATTERN_MATCH";
exports.FULL_TEXT_MATCH = "FULL_TEXT_MATCH";
95 changes: 69 additions & 26 deletions src/no-pattern-match.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Rule } from "eslint";
import {
DEFAULT_ADDTIONAL_REGEXES,
FULL_TEXT_MATCH,
plainObjectOption,
validateRecordOfRegex,
} from "./utils";
Expand All @@ -26,16 +27,12 @@ function globalizeAllRegularExps(
);
}

function parseAndValidateOptions({ additionalPatterns }) {
function parseAndValidateOptions({ patterns }) {
const compiledRegexes = validateRecordOfRegex(
plainObjectOption(
additionalPatterns,
"additionalPatterns",
DEFAULT_ADDTIONAL_REGEXES
)
plainObjectOption(patterns, "patterns", DEFAULT_ADDTIONAL_REGEXES)
);
return {
additionalPatterns: compiledRegexes,
patterns: compiledRegexes,
};
}

Expand Down Expand Up @@ -107,39 +104,85 @@ function findLineAndColNoFromMatchIdx(
return { endIdx, startIdx, lineSelections };
}

function serializeTextSelections(textAreaSelection: TextAreaSelection) {
return textAreaSelection.lineSelections
.map((line) => {
return `${line.lineNo}:${line.startCol}-${line.endCol}`;
})
.join(",");
}

function findStartAndEndTextSelection(textAreaSelection: TextAreaSelection) {
const start = {
column: Infinity,
line: Infinity,
};
const end = {
line: 0,
column: 0,
};
for (const line of textAreaSelection.lineSelections) {
const min = Math.min(line.lineNo, start.line);
if (line.lineNo === min) {
start.line = min;
start.column = line.startCol;
}
const max = Math.max(line.lineNo, end.line);
if (line.lineNo === max) {
end.line = max;
end.column = line.endCol;
}
}
return {
start,
end,
};
}

const FULL_TEXT_MATCH_MESSAGE = `Found text that matches the pattern "{{ patternName }}": {{ textMatch }}`;

const noPatternMatch: Rule.RuleModule = {
meta: {
schema: false,
messages: {
[FULL_TEXT_MATCH]: FULL_TEXT_MATCH_MESSAGE,
},
docs: {
description:
"An eslint rule that does pattern matching against an entire file",
category: "Best Practices",
},
},
create(context) {
const { additionalPatterns } = parseAndValidateOptions(
context.options[0] || {}
);
const { patterns } = parseAndValidateOptions(context.options[0] || {});
const sourceCode = context?.getSourceCode?.() || context.sourceCode;
const patterns = Object.entries(additionalPatterns);
const patternList = Object.entries(patterns);
const text = sourceCode.text;
const newLinePos = findAllNewLines(text);
const matches = patternList
.map(([name, pattern]) => {
const globalPattern = globalizeRegularExpression(pattern);
const matches = Array.from(text.matchAll(globalPattern));
return matches.map((m) => {
const idx = m.index;
const textMatch = m[0];
const lineAndColNumbers = findLineAndColNoFromMatchIdx(
idx,
newLinePos,
textMatch.length
);

for (const [name, pattern] of patterns) {
const globalPattern = globalizeRegularExpression(pattern);
const matches = Array.from(text.matchAll(globalPattern));
for (const m of matches) {
const idx = m.index;
const match = m[0];
const meta = findLineAndColNoFromMatchIdx(
idx,
newLinePos,
match.length
);
//console.dir({ match, meta }, { depth: 3 });
}
}

return { lineAndColNumbers, textMatch, patternName: name };
});
})
.flat();
matches.forEach(({ patternName, textMatch, lineAndColNumbers }) => {
context.report({
data: { patternName, textMatch },
messageId: FULL_TEXT_MATCH,
loc: findStartAndEndTextSelection(lineAndColNumbers),
});
});
return {};
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,5 @@ function getAssignmentName(node) {
export const HIGH_ENTROPY = "HIGH_ENTROPY";

export const PATTERN_MATCH = "PATTERN_MATCH";

export const FULL_TEXT_MATCH = "FULL_TEXT_MATCH";
16 changes: 6 additions & 10 deletions staging/jsonc.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module.exports = {
"extends": [
"plugin:jsonc/base"
],
"plugins": [
"self"
],
"rules": {
"self/no-secrets": "error"
}
}
extends: ["plugin:jsonc/base"],
plugins: ["self"],
rules: {
"self/no-secrets": "error",
},
};
18 changes: 7 additions & 11 deletions staging/mixed.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module.exports = {
env: { es6: true },
"extends": [
"plugin:jsonc/base"
],
"plugins": [
"self"
],
"rules": {
"self/no-secrets": "error"
}
}
env: { es6: true },
extends: ["plugin:jsonc/base"],
plugins: ["self"],
rules: {
"self/no-secrets": "error",
},
};
14 changes: 6 additions & 8 deletions staging/normal.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module.exports = {
env: { es6: true },
"plugins": [
"self"
],
"rules": {
"self/no-secrets": "error"
}
}
env: { es6: true },
plugins: ["self"],
rules: {
"self/no-secrets": "error",
},
};
36 changes: 29 additions & 7 deletions tests/lib/rules/no-pattern-match.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
import { rules } from "../../../src/index";
import { FULL_TEXT_MATCH } from "../../../src/utils";
import RULE_TESTERS from "./rule-testers";
const noPatternMatch = rules["no-pattern-match"];

const FULL_TEXT = `
const FULL_TEXT_NO_SECRETS = `
/**Not a problem**/
const A = "Not a problem";
`;

const FULL_TEXT_SECRETS = `
/**SECRET**/
const VAULT = {
token:"secret secret SECRET"
};
`;

const patterns = {
Test: /secret/i,
MultiLine: /VAULT = {[\n.\s\t]*to/im,
};

const FULL_TEXT_MATCH_MSG = {
messageId: FULL_TEXT_MATCH,
};

function createTests(_flatConfig = false) {
return {
valid: [
{
code: FULL_TEXT,
code: FULL_TEXT_NO_SECRETS,
options: [
{
patterns,
},
],
},
],
invalid: [
{
code: FULL_TEXT_SECRETS,
options: [
{
additionalPatterns: {
Test: /secret/i,
MultiLine: /VAULT = {[\n.\s\t]*to/im,
},
patterns,
},
],
errors: Array(5).fill(FULL_TEXT_MATCH_MSG),
},
],
invalid: [],
};
}

Expand Down

0 comments on commit c362b91

Please sign in to comment.