Skip to content

Commit

Permalink
Fix no shadow issues in config-utils.ts
Browse files Browse the repository at this point in the history
Rename throwaway variable "suite" to "found" when assigned from "find".

Rename local variable "path" to "newPath" as it is a modification of
the "originalPath" provided to `validateAndSanitisePath`.

Rename instances of "path" to more explicit varients "ignorePath" and
"includePath". Maybe "ignoredPath" and "includedPath" are better names?
  • Loading branch information
Eric Cornelissen committed Nov 19, 2020
1 parent 06e99f1 commit 2a2910e
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ async function addBuiltinSuiteQueries(
suiteName: string,
configFile?: string
) {
const suite = builtinSuites.find((suite) => suite === suiteName);
if (!suite) {
const found = builtinSuites.find((suite) => suite === suiteName);
if (!found) {
throw new Error(getQueryUsesInvalid(configFile, suiteName));
}

Expand Down Expand Up @@ -388,20 +388,20 @@ export function validateAndSanitisePath(
logger: Logger
): string {
// Take a copy so we don't modify the original path, so we can still construct error messages
let path = originalPath;
let newPath = originalPath;

// All paths are relative to the src root, so strip off leading slashes.
while (path.charAt(0) === "/") {
path = path.substring(1);
while (newPath.charAt(0) === "/") {
newPath = newPath.substring(1);
}

// Trailing ** are redundant, so strip them off
if (path.endsWith("/**")) {
path = path.substring(0, path.length - 2);
if (newPath.endsWith("/**")) {
newPath = newPath.substring(0, newPath.length - 2);
}

// An empty path is not allowed as it's meaningless
if (path === "") {
if (newPath === "") {
throw new Error(
getConfigFilePropertyError(
configFile,
Expand All @@ -413,7 +413,7 @@ export function validateAndSanitisePath(
}

// Check for illegal uses of **
if (path.match(pathStarsRegex)) {
if (newPath.match(pathStarsRegex)) {
throw new Error(
getConfigFilePropertyError(
configFile,
Expand All @@ -426,7 +426,7 @@ export function validateAndSanitisePath(

// Check for other regex characters that we don't support.
// Output a warning so the user knows, but otherwise continue normally.
if (path.match(filterPatternCharactersRegex)) {
if (newPath.match(filterPatternCharactersRegex)) {
logger.warning(
getConfigFilePropertyError(
configFile,
Expand All @@ -440,7 +440,7 @@ export function validateAndSanitisePath(
// Ban any uses of backslash for now.
// This may not play nicely with project layouts.
// This restriction can be lifted later if we determine they are ok.
if (path.indexOf("\\") !== -1) {
if (newPath.indexOf("\\") !== -1) {
throw new Error(
getConfigFilePropertyError(
configFile,
Expand All @@ -451,7 +451,7 @@ export function validateAndSanitisePath(
);
}

return path;
return newPath;
}

// An undefined configFile in some of these functions indicates that
Expand Down Expand Up @@ -890,12 +890,17 @@ async function loadConfig(
if (!(parsedYAML[PATHS_IGNORE_PROPERTY] instanceof Array)) {
throw new Error(getPathsIgnoreInvalid(configFile));
}
for (const path of parsedYAML[PATHS_IGNORE_PROPERTY]!) {
if (typeof path !== "string" || path === "") {
for (const ignorePath of parsedYAML[PATHS_IGNORE_PROPERTY]!) {
if (typeof ignorePath !== "string" || ignorePath === "") {
throw new Error(getPathsIgnoreInvalid(configFile));
}
pathsIgnore.push(
validateAndSanitisePath(path, PATHS_IGNORE_PROPERTY, configFile, logger)
validateAndSanitisePath(
ignorePath,
PATHS_IGNORE_PROPERTY,
configFile,
logger
)
);
}
}
Expand All @@ -904,12 +909,12 @@ async function loadConfig(
if (!(parsedYAML[PATHS_PROPERTY] instanceof Array)) {
throw new Error(getPathsInvalid(configFile));
}
for (const path of parsedYAML[PATHS_PROPERTY]!) {
if (typeof path !== "string" || path === "") {
for (const includePath of parsedYAML[PATHS_PROPERTY]!) {
if (typeof includePath !== "string" || includePath === "") {
throw new Error(getPathsInvalid(configFile));
}
paths.push(
validateAndSanitisePath(path, PATHS_PROPERTY, configFile, logger)
validateAndSanitisePath(includePath, PATHS_PROPERTY, configFile, logger)
);
}
}
Expand Down

0 comments on commit 2a2910e

Please sign in to comment.