UNPKG

eslint-plugin-sonarjs

Version:
115 lines (114 loc) • 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ROOT_PATH = exports.normalizeToAbsolutePath = exports.normalizePath = exports.isAbsolutePath = void 0; exports.stripBOM = stripBOM; exports.isRoot = isRoot; exports.assertNestedPath = assertNestedPath; exports.getPathRoot = getPathRoot; exports.dirnamePath = dirnamePath; exports.relativeToAncestorPath = relativeToAncestorPath; exports.joinPaths = joinPaths; exports.basenamePath = basenamePath; /* * SonarQube JavaScript Plugin * Copyright (C) SonarSource Sàrl * mailto:info AT sonarsource DOT com * * You can redistribute and/or modify this program under the terms of * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ const posix_1 = require("node:path/posix"); const win32_1 = require("node:path/win32"); const path_normalization_js_1 = require("./path-normalization.js"); var path_normalization_js_2 = require("./path-normalization.js"); Object.defineProperty(exports, "isAbsolutePath", { enumerable: true, get: function () { return path_normalization_js_2.isAbsolutePath; } }); Object.defineProperty(exports, "normalizePath", { enumerable: true, get: function () { return path_normalization_js_2.normalizePath; } }); Object.defineProperty(exports, "normalizeToAbsolutePath", { enumerable: true, get: function () { return path_normalization_js_2.normalizeToAbsolutePath; } }); Object.defineProperty(exports, "ROOT_PATH", { enumerable: true, get: function () { return path_normalization_js_2.ROOT_PATH; } }); /** * Byte Order Marker */ const BOM_BYTE = 0xfeff; /** * Removes any Byte Order Marker (BOM) from a string's head * * A string's head is nothing else but its first character. * * @param str the input string * @returns the stripped string */ function stripBOM(str) { if (str.codePointAt(0) === BOM_BYTE) { return str.slice(1); } return str; } function isParseResultRoot(result) { // A path is a root if it has a non-empty root and no base (filename) // and dir is either empty or equals the root itself return (result.root !== '' && result.base === '' && (result.dir === '' || result.dir === result.root)); } function isRoot(file) { return isParseResultRoot((0, win32_1.parse)(file)) || isParseResultRoot((0, posix_1.parse)(file)); } function assertNestedPath(from, topDir) { const fromSanitized = from.endsWith('/') ? from : from + '/'; const topDirSanitized = topDir.endsWith('/') ? topDir : topDir + '/'; if (!fromSanitized.startsWith(topDirSanitized)) { throw new Error(`"${from}" is not nested under topDir "${topDir}"`); } } /** * Returns the filesystem root that contains the given absolute path. * Examples: '/a/b' -> '/', 'C:/a/b' -> 'C:/', 'D:/foo' -> 'D:/'. * Used as a cross-platform "no upper bound" sentinel for find-up callers. */ function getPathRoot(filePath) { const winRoot = (0, win32_1.parse)(filePath).root; if (winRoot) { return (0, path_normalization_js_1.toUnixPath)(winRoot); } return path_normalization_js_1.ROOT_PATH; } /** * Type-safe dirname that preserves the NormalizedAbsolutePath brand. * The dirname of an absolute path is always absolute. * @param filePath the absolute path to get the directory of * @returns the parent directory as a branded NormalizedAbsolutePath */ function dirnamePath(filePath) { return (0, posix_1.dirname)(filePath); } function relativeToAncestorPath(filePath, topDir) { const topDirPrefix = topDir.endsWith('/') ? topDir : `${topDir}/`; if (filePath === topDir) { return ''; } return filePath.startsWith(topDirPrefix) ? filePath.slice(topDirPrefix.length) : undefined; } /** * Type-safe path join that preserves the NormalizedAbsolutePath brand. * Joins path segments using posix separators. * @param base the base absolute path * @param segments additional path segments to join * @returns the joined path as a branded NormalizedAbsolutePath */ function joinPaths(base, ...segments) { return (0, posix_1.join)(base, ...segments); } /** * Type-safe basename that extracts the filename from a path. * @param filePath the path to extract the basename from * @returns the filename (last segment of the path) */ function basenamePath(filePath) { return (0, posix_1.basename)(filePath); }