Skip to content

Commit

Permalink
refactor: cleans up some of the regular expressions
Browse files Browse the repository at this point in the history
- where possible use .test instead of .match
- where groups aren't used for capturing use non-capturing groups
- where groups _are_ used for capturing, use named capturing groups to enhance readability
  • Loading branch information
sverweij committed Apr 14, 2024
1 parent 48dcb5c commit 712c5be
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 28 deletions.
21 changes: 8 additions & 13 deletions src/extract/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export function detectPreCompilationNess(pTSDependencies, pJSDependencies) {
);
}

/* eslint-disable security/detect-object-injection */

/**
* Given a module string returns in an object
* - the module name
Expand All @@ -42,25 +40,22 @@ export function detectPreCompilationNess(pTSDependencies, pJSDependencies) {
* (if there's a default node API that does this I'm all ears)
*
* @param {string} pString
* @returns {any}
* @returns {{module:string; protocol?:string; mimeType?:string}}
*/
export function extractModuleAttributes(pString) {
let lReturnValue = { module: pString };
const lModuleAttributes = pString.match(
// eslint-disable-next-line security/detect-unsafe-regex
/^((node:|file:|data:)(([^,]+),)?)(.+)$/,
/^(?:(?<protocol>node:|file:|data:)(?:(?<mimeType>[^,]+),)?)(?<module>.+)$/,
);
const lProtocolPosition = 2;
const lMimeTypePosition = 4;
const lModulePosition = 5;

if (lModuleAttributes) {
lReturnValue.module = lModuleAttributes[lModulePosition];
if (lModuleAttributes[lProtocolPosition]) {
lReturnValue.protocol = lModuleAttributes[lProtocolPosition];
if (lModuleAttributes?.groups) {
lReturnValue.module = lModuleAttributes?.groups.module;
if (lModuleAttributes?.groups?.protocol) {
lReturnValue.protocol = lModuleAttributes.groups.protocol;
}
if (lModuleAttributes[lMimeTypePosition]) {
lReturnValue.mimeType = lModuleAttributes[lMimeTypePosition];
if (lModuleAttributes?.groups?.mimeType) {
lReturnValue.mimeType = lModuleAttributes.groups.mimeType;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/extract/resolve/module-classifiers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function isSubpathImport(pModuleName, pManifest) {
const lMatchREasString = pImportLHS.replace(/\*/g, ".+");
// eslint-disable-next-line security/detect-non-literal-regexp
const lMatchRE = new RegExp(`^${lMatchREasString}$`);
return Boolean(pModuleName.match(lMatchRE));
return lMatchRE.test(pModuleName);
})
);
}
Expand Down Expand Up @@ -217,7 +217,7 @@ function matchesTSConfigPaths(pModuleName, pPaths) {
return Object.keys(pPaths).some((pPathLHS) => {
// eslint-disable-next-line security/detect-non-literal-regexp
const lMatchRE = new RegExp(`^${pPathLHS.replace(/\*/g, ".+")}$`);
return Boolean(pModuleName.match(lMatchRE));
return lMatchRE.test(pModuleName);
});
}

Expand Down
9 changes: 4 additions & 5 deletions src/extract/transpile/try-import-available.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { coerce, satisfies } from "semver";
const require = createRequire(import.meta.url);

const PACKAGE_RE = "[^/]+";
const SCOPED_PACKAGE_RE = "@[^/]+(/[^/]+)";
const SCOPED_PACKAGE_RE = "@[^/]+(?:/[^/]+)";
const ROOT_MODULE_RE = new RegExp(`^(${SCOPED_PACKAGE_RE}|${PACKAGE_RE})`, "g");

function extractRootModuleName(pModuleName) {
Expand All @@ -17,10 +17,9 @@ function getVersion(pModuleName) {
// (yo, you're import-ing 'json'!), but that's _experimental_, printing scary
// messages to stderr so: ¯\_(ツ)_/¯
// eslint-disable-next-line import/no-dynamic-require, security/detect-non-literal-require
const lManifest = require(path.join(
extractRootModuleName(pModuleName),
"package.json"
));
const lManifest = require(
path.join(extractRootModuleName(pModuleName), "package.json"),
);
return lManifest.version;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/options/assert-validity.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import merge from "lodash/merge.js";
import safeRegex from "safe-regex";
import report from "#report/index.mjs";

const MODULE_SYSTEM_LIST_RE = /^((cjs|amd|es6|tsd)(,|$))+$/gi;
const MODULE_SYSTEM_LIST_RE = /^(?:(?:cjs|amd|es6|tsd)(?:,|$)){1,4}/gi;
const VALID_DEPTH_RE = /^\d{1,2}$/g;

function assertModuleSystemsValid(pModuleSystems) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/rule-set/normalize.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { normalizeREProperties, normalizeToREAsString } from "../helpers.mjs";

const VALID_SEVERITIES = /^(error|warn|info|ignore)$/;
const VALID_SEVERITIES = /^(?:error|warn|info|ignore)$/;
const DEFAULT_SEVERITY = "warn";
const DEFAULT_RULE = "unnamed";
const DEFAULT_SCOPE = "module";

function normalizeSeverity(pSeverity) {
const lSeverity = pSeverity ? pSeverity : DEFAULT_SEVERITY;

return Boolean(lSeverity.match(VALID_SEVERITIES))
? lSeverity
: DEFAULT_SEVERITY;
return VALID_SEVERITIES.test(lSeverity) ? lSeverity : DEFAULT_SEVERITY;
}
/**
*
Expand Down
2 changes: 1 addition & 1 deletion src/report/anon/anonymize-path.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { anonymizePathElement } from "./anonymize-path-element.mjs";

export const WHITELIST_RE =
// eslint-disable-next-line security/detect-unsafe-regex
/^(|[.]+|~|bin|apps?|cli|src|libs?|configs?|components?|fixtures?|helpers?|i18n|index\.(jsx?|[mc]js|d\.ts|tsx?|vue|coffee|ls)|_?_?mocks?_?_?|node_modules|packages?|package\.json|scripts?|services?|sources?|specs?|_?_?tests?_?_?|types?|uti?ls?|tools)$/;
/^(?:|[.]+|~|bin|apps?|cli|src|libs?|configs?|components?|fixtures?|helpers?|i18n|index\.(?:jsx?|[mc]js|d\.ts|tsx?|vue|coffee|ls)|_?_?mocks?_?_?|node_modules|packages?|package\.json|scripts?|services?|sources?|specs?|_?_?tests?_?_?|types?|uti?ls?|tools)$/;

/**
* Kind of smartly anonymizes paths, by
Expand Down
2 changes: 1 addition & 1 deletion src/report/utl/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function formatViolation(
* @return {string} prefix and filename concatenated
*/
function smartURIConcat(pPrefix, pSource) {
if (pPrefix.match(PROTOCOL_PREFIX_RE)) {
if (PROTOCOL_PREFIX_RE.test(pPrefix)) {
return `${pPrefix}${pSource}`;
} else {
return join(pPrefix, pSource);
Expand Down
2 changes: 1 addition & 1 deletion src/utl/get-extension.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
import { extname } from "node:path";

const EXTENSION_RE = /(?<extension>((\.d\.(c|m)?ts)|\.coffee\.md)$)/;
const EXTENSION_RE = /(?<extension>(?:(?:\.d\.(?:c|m)?ts)|\.coffee\.md)$)/;

/**
* Returns the extension of the given file name path.
Expand Down

0 comments on commit 712c5be

Please sign in to comment.