Skip to content

Commit

Permalink
Many linting tweaks
Browse files Browse the repository at this point in the history
Adds regexp linter.  Addresses many issues reported by CodeQL and regexp linter.
  • Loading branch information
lauckhart committed Jan 24, 2025
1 parent 44540cd commit b729d21
Show file tree
Hide file tree
Showing 57 changed files with 212 additions and 245 deletions.
136 changes: 0 additions & 136 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion codegen/src/clusters/TlvGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class TlvGenerator {
* Get the filename for global datatypes.
*/
static filenameFor(model: Model) {
const name = model.name.replace(/(Enum|Bitmap|Struct)$/, "");
const name = model.name.replace(/Enum|Bitmap|Struct$/, "");
return camelize(name, true);
}

Expand Down
6 changes: 3 additions & 3 deletions codegen/src/mom/spec/add-documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ function extractUsefulDocumentation(p: HTMLElement) {
.replace(/ such that:$/, "")
.replace(/, derived from \w+,/, "")
.replace(/\([^)]*$/, "")
.replace(/\s\s+/, " ")
.replace(/\s{2,}/, " ")
.replace(/This attribute shall (?:indicate|represent)/, "Indicates")
.replace(/This attribute shall be null/, "Null")
.replace(/The following tags are defined in this namespace\./, "")
.replace(/This section contains the (.*) as part of the semantic tag feature\./i, "")
.replace(/This section contains the .* as part of the semantic tag feature\./i, "")
.replace(
/The table below lists the changes relative to the Mode Base Cluster for the fields of the ModeOptionStruct type\./,
"",
Expand All @@ -70,7 +70,7 @@ function extractUsefulDocumentation(p: HTMLElement) {
function mergeSplitParagraphs(paragraphs: string[]) {
for (let i = 0; i < paragraphs.length - 1; i++) {
const trailing = paragraphs[i].replace(/^.*\s(\w+)$/, "$1").toLowerCase();
const leading = paragraphs[i + 1].replace(/^(\w+).*$/, "$1").toLowerCase();
const leading = paragraphs[i + 1].replace(/^(\w+)\W.*$/, "$1").toLowerCase();
if (Words.has(leading) && !NotWords.has(leading) && Words.has(trailing) && !NotWords.has(trailing)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/mom/spec/doc-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ export function identifyDocument(path: string): IndexDetail {

let version;
const titleAndVersion = title.split(/ version /i);
if (titleAndVersion.length === 2 && titleAndVersion[1].match(/(?:\d\.)+/i)) {
if (titleAndVersion.length === 2 && titleAndVersion[1].match(/(?:\d\.)+/)) {
title = titleAndVersion[0];
version = titleAndVersion[1];
} else {
const versionEl = titleEl.nextElementSibling;
if (!versionEl || !versionEl.textContent || !versionEl.textContent.match(/version (?:\d\.)+/i)) {
throw new Error(`No version found for ${title} in ${path}`);
}
version = versionEl.textContent.replace(/.*version ([\d.]+).*/i, "$1");
version = versionEl.textContent.replace(/.*version ([\d.]).*/i, "$1");
}

// Drop dotted elements except the first two. To date these have represented trivial changes
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/mom/spec/html-translators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const Str = (el: HTMLElement) => {
// P starts with text
child.firstChild?.nodeType === 3 /** TEXT_CONTENT */ &&
// Containing a single digit
child.firstChild.textContent?.match(/^[0-9]$/) &&
child.firstChild.textContent?.match(/^\d$/) &&
// Followed by a span
(child.firstChild.nextSibling as Element)?.tagName === "SPAN" &&
// That doesn't indicate numeric arity
Expand Down Expand Up @@ -146,7 +146,7 @@ export const Code = (el: HTMLElement) => {
const beginning = parts[i].replace(/^.*([A-Z])/, "$1");

// Get ending of word from next part
const ending = parts[i + 1].replace(/^([a-z]+).*/, "$1");
const ending = parts[i + 1].replace(/^([a-z]).*/, "$1");

// If the concatenation is a word, assume it should be joined
if (Words.has(`${beginning}${ending}`.toLowerCase())) {
Expand Down Expand Up @@ -180,7 +180,7 @@ export const Identifier = (el: HTMLElement) => {
let str = Code(el);

// Strip everything following a subset of characters known to be inside what is properly a "key"
str = str.replace(/^([a-z0-9 _:.,/\-$]+).*/i, "$1");
str = str.replace(/^([\w :.,/\-$]+)/, "$1");

return camelize(str, true);
};
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/mom/spec/load-clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export function* loadClusters(clusters: HtmlReference): Generator<ClusterReferen

if (!ref.tables) {
// Sometimes there's a section with no table to indicate no elements
if (ref.prose?.[0]?.textContent?.match(/(?:this cluster has no|no cluster specific)/i)) {
if (ref.prose?.[0]?.textContent?.match(/this cluster has no|no cluster specific/i)) {
return;
}
logger.warn("no defining table in definition of", name, "for", ref.name, `(${ref.path})`);
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/mom/spec/repairs/default-value-repairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function repairDefaultValue(record: { default?: string; type?: string })
default:
// Sometimes enum values are suffixed with enum name in parenthesis
if (def.match(/^\d+\(.*\)$/)) {
record.default = def.replace(/^(\d+).*/, "$1");
record.default = def.replace(/^(\d).*/, "$1");
}

// Sometimes default value strings have "" around them
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/mom/spec/scan-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function* scanDocument(docRef: HtmlReference) {
const text = Str(element).replace(/\s*\([^)]+\)\s*/g, " ");

// Test for "heading" shapes with a numerical prefix followed by a label
if (text?.match(/^\d+\.(\d+\.)+ [ a-zA-Z0-9]+$/) || text?.match(/^\d+\.(\d+\.)+ .+ \(.+ type\)/i)) {
if (text?.match(/^\d+\.(\d+\.)+ [ a-z0-9]+$/i) || text?.match(/^\d+\.(\d+\.)+ .+ \(.+ type\)/i)) {
let possibleHeading = parseHeading(element);

// Ignore links elsewhere. This occurs in TOC entries. If this gets too fiddly then we can
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/mom/spec/scan-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function convertTable(el: HTMLTableElement, previous: Table | undefined) {
if (!table.fields.length) {
cells.forEach(cell => {
let key = cell.textContent || "";
key = key.replace(/[\W]/g, "").toLowerCase();
key = key.replace(/\W/g, "").toLowerCase();
table?.fields.push(key);
});
continue;
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/mom/spec/translate-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function translateInvokable(definition: ClusterReference, children: Array<Cluste
const commands = translateRecordsToMatter("command", records, r => {
let direction: CommandElement.Direction | undefined;

if (r.direction?.match(/client.*.*server/i)) {
if (r.direction?.match(/client[^]*[^]*server/i)) {
direction = CommandElement.Direction.Response;
} else if (r.direction?.match(/client.*server/i)) {
direction = CommandElement.Direction.Request;
Expand Down
10 changes: 4 additions & 6 deletions codegen/src/mom/spec/translate-datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,22 @@ export function translateDatatype(definition: HtmlReference): DatatypeElement |
}

// Up through 1.1 prose was informal but remarkably consistent; "derived from" always matches
let match = text?.match(/derived from ([a-z0-9\-_]+)/i);
let match = text?.match(/derived from ([\w-]+)/i);

// This now applies to cluster 1.2 § 1.14.15.1, because consistency is overrated
if (!match) {
match = text?.match(/data type shall be a ([a-z0-9\-_]+)/i);
match = text?.match(/data type shall be a ([\w-]+)/i);
}

// Applies to a handful of overrides of ModeOptionStruct in cluster 1.2
if (!match) {
match = text?.match(
/lists the changes relative to the [a-z0-9\-_ ]+ cluster for the fields of the ([a-z0-9\-_]+) type/i,
);
match = text?.match(/lists the changes relative to the [\w\- ]+ cluster for the fields of the ([\w-]+) type/i);
}

// And 1.3 throws in this beaut
let constraint: string | undefined;
if (!match) {
match = text?.match(/This data type is an? ([a-z0-9\-_]+) of fixed length (\d+)/i);
match = text?.match(/This data type is an? ([\w-]+) of fixed length (\d+)/i);
constraint = match?.[2];
}

Expand Down
10 changes: 4 additions & 6 deletions codegen/src/util/TsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class Block extends Entry {
let m = s.match(/^(\w+):/);
if (!m) {
m = s.match(
/\s*(?:(?:export|public|private|const)\s+)*(?:(?:function|enum|class|interface|const|var|let)\s+)(\w+)/,
/\s*(?:(?:export|public|private|const)\s+)*(?:function|enum|class|interface|const|var|let)\s+(\w+)/,
);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of 'export '.
}
if (m) {
Expand Down Expand Up @@ -408,9 +408,7 @@ export class Block extends Entry {

protected delimiterAfter(entry: Entry, serialized: string): string {
if (
serialized.match(
/^(?:\s*(?:\/\*.*\*\/|export|const))*\s*(?:export)?\s*(?:enum|function|namespace|interface|class)/m,
)
serialized.match(/^\s*(?:\/\*(?!\*\/)\*\/\s*)?(?:export\s*)?(?:enum|function|namespace|interface|class)\s/m)
) {
// Do not delimit functions structures that eslint will complain about
return "";
Expand Down Expand Up @@ -497,7 +495,7 @@ function chooseExpressionLayout(lineLength: number, prefix: string, serializedEn
const multiline = entry.indexOf("\n") !== -1;

// Any comment or assignment automatically forces verbose layout mode
if (entry.match(/(?:\/\*|\/\/| = )/)) {
if (entry.match(/\/\*|\/\/| = /)) {
return ExpressionLayout.Verbose;
}

Expand Down Expand Up @@ -740,7 +738,7 @@ export class TsFile extends Block {
} else if (filename.startsWith("@matter/")) {
// For @matter/package we assume an alias of "#package"; for "@matter/package/submodule" we assume an
// alias of "#submodule"
return filename.replace(/^@matter\/(?:[^/]+\/)/, "#");
return filename.replace(/^@matter\/[^/]+\//, "#");
} else {
throw new InternalError(`Absolute import of ${filename} must start with "@matter"`);
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { camelize, describeList, serialize } from "#general";
*/
export function asObjectKey(label: any) {
let str = `${label}`;
if (!str.match(/^[$_a-z][$_a-z0-9]*$/i)) {
if (!str.match(/^[$_a-z][$\w]*$/i)) {
str = JSON.stringify(label);
}
return str;
Expand Down
7 changes: 7 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @license
* Copyright 2022-2025 Project CHIP Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { fixupConfigRules, fixupPluginRules } from "@eslint/compat";
import { FlatCompat } from "@eslint/eslintrc";
import js from "@eslint/js";
Expand Down Expand Up @@ -37,6 +43,7 @@ export default [
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:regexp/recommended",
),
),
{
Expand Down
Loading

0 comments on commit b729d21

Please sign in to comment.