CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
SecretLintSourceCodeImpl.ts117 linesDownload Raw Back to src
1import { StructuredSource } from "structured-source";2import {3    SecretLintSourceCode,4    SecretLintSourceNodeLocation,5    SecretLintSourceNodePosition,6    SecretLintSourceNodeRange,7} from "@secretlint/types";8import { invariant } from "./helper/invariant.js";9 10/**11 * This class represent of source code.12 */13export class SecretLintSourceCodeImpl implements SecretLintSourceCode {14    readonly hasBOM: boolean;15    readonly content: string;16    readonly filePath: string | undefined;17    readonly physicalFilePath: string | undefined;18    readonly contentType: "binary" | "text" | "unknown";19    readonly ext: string;20    private structuredSource: StructuredSource;21 22    constructor({23        content = "",24        ext,25        filePath,26        physicalFilePath,27        contentType,28    }: {29        content: string;30        ext: string;31        filePath: string;32        physicalFilePath: string | undefined;33        contentType: "binary" | "text" | "unknown";34    }) {35        invariant(ext || filePath, "should be set either of fileExt or filePath.");36        this.hasBOM = content.charCodeAt(0) === 0xfeff;37        this.content = this.hasBOM ? content.slice(1) : content;38        this.structuredSource = new StructuredSource(this.content);39        this.filePath = filePath;40        this.physicalFilePath = physicalFilePath;41        this.contentType = contentType;42        this.ext = ext;43    }44 45    /**46     * get file path47     * This return `undefined` if the source code is created without file path.48     * For example, use core API to create a source code directly.49     *50     * You can use this path to detect file type.51     * However, if you want to read the file content, use `getPhysicalFilePath` instead.52     * @returns {string|undefined}53     */54    getFilePath(): string | undefined {55        return this.filePath;56    }57 58    /**59     * get physical file path60     * This return `undefined` if the source code is not related to file.61     * For example, the source code is given from the stdin.62     *63     * You can use this path to read the file content.64     */65    getPhysicalFilePath(): string | undefined {66        return this.physicalFilePath;67    }68 69    /**70     * @param {SecretLintSourceNodeLocation} loc - location indicator.71     * @return {[ number, number ]} range.72     */73    locationToRange(loc: SecretLintSourceNodeLocation): SecretLintSourceNodeRange {74        return this.structuredSource.locationToRange(loc);75    }76 77    /**78     * @param {[ number, number ]} range - pair of indice.79     * @return {SecretLintSourceNodeLocation} location.80     */81    rangeToLocation(range: SecretLintSourceNodeRange): SecretLintSourceNodeLocation {82        const location = this.structuredSource.rangeToLocation(range as [number, number]);83        // Note: unwrap class instance to get plain object84        return {85            start: {86                line: location.start.line,87                column: location.start.column,88            },89            end: {90                line: location.end.line,91                column: location.end.column,92            },93        };94    }95 96    /**97     * @param {Position} pos - position indicator.98     * @return {number} index.99     */100    positionToIndex(pos: SecretLintSourceNodePosition): number {101        return this.structuredSource.positionToIndex(pos);102    }103 104    /**105     * @param {number} index - index to the source code.106     * @return {Position} position.107     */108    indexToPosition(index: number): SecretLintSourceNodePosition {109        const position = this.structuredSource.indexToPosition(index);110        // Note: unwrap class instance to get plain object111        return {112            line: position.line,113            column: position.column,114        };115    }116}117 
basant307/AI_Governance_Project · CoolFace