{"size": 12206, "ext": "js", "max_stars_repo_path": ".config/coc/extensions/node_modules/coc-scssmodules/dist/DefinitionProvider.js", "max_stars_repo_name": "xArthasx/dotfiles", "max_stars_repo_stars_event_min_datetime": "2016-02-25T22:43:34.000Z", "max_stars_repo_stars_event_max_datetime": "2016-04-05T04:15:37.000Z", "max_issues_repo_path": ".config/coc/extensions/node_modules/coc-scssmodules/dist/DefinitionProvider.js", "max_issues_repo_name": "xArthasx/dotfiles", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": ".config/coc/extensions/node_modules/coc-scssmodules/dist/DefinitionProvider.js", "max_forks_repo_name": "xArthasx/dotfiles", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 46.0603773585, "max_line_length": 196, "alphanum_fraction": 0.5284286417}
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var coc_nvim_1 = require("coc.nvim");
var fs_1 = require("fs");
var os_1 = __importDefault(require("os"));
var path_1 = require("path");
var vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
var utils_1 = require("./utils");
var CSSModulesDefinitionProvider = /** @class */ (function () {
    function CSSModulesDefinitionProvider(camelCase, logger) {
        this.camelCase = camelCase;
        this.logger = logger;
    }
    /**
     * Check if the current character is on the import style object or
     * a class name field on the style object.
     *
     * Good examples:
     * import styles from "./Something.module.scss"
     *        ^
     * import styles from "./Something.module.scss"
     *           ^
     *
     * Bad examples:
     * import styles from "./Something.module.scss"
     *   ^
     * import styles from "./Something.module.scss"
     *               ^
     */
    CSSModulesDefinitionProvider.prototype.isImportLine = function (line, character, importName, fileName) {
        if (!importName || !fileName) {
            return false;
        }
        var importNameIndex = line.indexOf(importName);
        var fileNameIndex = line.indexOf(fileName);
        return ((character >= importNameIndex &&
            character <= importNameIndex + importName.length) ||
            (character >= fileNameIndex &&
                character <= fileNameIndex + fileName.length));
    };
    /**
     * Try to find the styles import object and the className from
     * the current line based on the current character position.
     *
     * Good examples:
     *   return <div className={styles.container} />
     *                          ^
     *   return <div className={styles.container} />
     *                                 ^
     *   return <div className={styles.container} />
     *                                     ^
     *
     * Bad examples:
     *   return <div className={styles.container} />
     *   ^
     *   return <div className={styles.container} />
     *                                ^
     *   return <div className={styles.container} />
     *                   ^
     */
    CSSModulesDefinitionProvider.prototype.getParts = function (line, position) {
        var text = line.slice(0, position.character);
        var index = text.search(/[a-z0-9\._]*$/i);
        if (index === -1 || !text.includes(".")) {
            return ["", ""];
        }
        var _a = /^([a-z0-9\._]*)/i.exec(line.slice(index)) || [], _b = _a[1], remaining = _b === void 0 ? "" : _b;
        var _c = remaining.split("."), _d = _c[0], importName = _d === void 0 ? "" : _d, _e = _c[1], className = _e === void 0 ? "" : _e;
        return [importName, className];
    };
    /**
     * Creates a regexp string that makes all the parent selectors optional. This
     * should be used with the camelCase option.
     *
     * Example:
     *
     * // so this should match:
     * // .container-child, &-child, &_child, &--child, &__child
     * "containerChild" -> "(container|&)(-|_){1,2}Child"
     *
     * // so this should match:
     * // .container-child-element,
     * // &-child-element
     * // &_child-element
     * // &--child-element
     * // &__child-element
     * // &-element
     * // &_element
     * // &--element
     * // &__element
     * "containerChildElement" -> "((container|&)(-|_){1,2})?(Child|&)(-|_){1,2}Element"
     *
     * but... it's best practice to not really do this much nesting so don't
     * really even know if this works 100%
     */
    CSSModulesDefinitionProvider.prototype.optionalSelectorPrefix = function (_a, currentRegex) {
        var className = _a[0], remaining = _a.slice(1);
        if (!remaining.length) {
            return "" + currentRegex + className;
        }
        var prefix = currentRegex;
        if (currentRegex) {
            prefix = "(" + currentRegex + ")?";
        }
        return this.optionalSelectorPrefix(remaining, prefix + "(" + className + "|&)(-|_){1,2}");
    };
    /**
     * This is really only used for hyphenated bem camelCase configuration. This
     * ensures that if there is a child selector with the same name as a "root"
     * selector later in the file, the "root" selector will be chosen instead.
     *
     * Example:
     *
     * ```scss
     * .container {
     *   &--clear {
     *     background-color: transparent;
     *   }
     *
     *   &--red {
     *     background-color: red;
     *   }
     * }
     *
     * .clear {
     *   clear: both;
     * }
     *
     * .red, .red-fg, .red-thing {
     *   color: red;
     * }
     * ```
     *
     * ```tsx
     * // want this to match .clear instead of &--clear
     * styles.clear
     *
     * // want this to match .red instead of &--red
     * styles.red
     * ```
     */
    CSSModulesDefinitionProvider.prototype.isNotBestSelectorMatch = function (remainingLines, className) {
        if (!this.camelCase) {
            return false;
        }
        var lineStartsWithClassName = new RegExp("^." + className + "(,\\s*.[A-z_-]+)*\\s*({|,)\\s*$");
        return !!remainingLines.find(function (line) { return line.match(lineStartsWithClassName); });
    };
    /**
     * Find the position of the classname within the css/scss module file by
     * expanding camel case for parent selectors (if camelCase is enabled).
     */
    CSSModulesDefinitionProvider.prototype.getPosition = function (importPath, className) {
        var _a;
        var contents = fs_1.readFileSync(importPath, "utf8");
        var lines = contents.split(os_1.default.EOL);
        var nameOrRegexpString = className;
        if (this.camelCase && /^[a-z]+[A-Z]/.test(className)) {
            var parts = className.split(/(?=[A-Z][a-z])/);
            nameOrRegexpString = this.optionalSelectorPrefix(parts, "");
        }
        var keyword = new RegExp(nameOrRegexpString, "i");
        for (var lineNumber = 0; lineNumber < lines.length; lineNumber++) {
            var line = lines[lineNumber];
            // match either:
            // .className
            // .class-name
            // .className-oh_why_wouldYoudoThis
            //   &-class
            //   &--modifier
            //   &__child
            var matches = line.match(/^(\.[A-z])|(\s*&[-_]+)[A-z0-9_-]+\s*(,|{)\s*$/);
            if (!matches || !matches.length) {
                continue;
            }
            var lineMatches = line.match(keyword);
            if (!lineMatches ||
                this.isNotBestSelectorMatch(lines.slice(lineNumber + 1), className)) {
                continue;
            }
            var character = (_a = lineMatches.index) !== null && _a !== void 0 ? _a : line.indexOf(lineMatches[0]);
            if (character !== -1) {
                return vscode_languageserver_protocol_1.Position.create(lineNumber, character);
            }
        }
        return null;
    };
    CSSModulesDefinitionProvider.prototype.provideDefinition = function (document, position, _token) {
        return __awaiter(this, void 0, void 0, function () {
            var nvim, line, _a, _b, importName, _c, fileName, filePath_1, range_1, _d, stylesObject, className, importPath, classNamePosition, filePath, range;
            return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        nvim = coc_nvim_1.workspace.nvim;
                        return [4 /*yield*/, nvim.eval('getline(".")')];
                    case 1:
                        line = _e.sent();
                        if (typeof line !== "string") {
                            return [2 /*return*/, null];
                        }
                        _a = utils_1.getImportRegExp("(\\S+)").exec(line) || [], _b = _a[1], importName = _b === void 0 ? "" : _b, _c = _a[2], fileName = _c === void 0 ? "" : _c;
                        if (this.isImportLine(line, position.character, importName, fileName)) {
                            filePath_1 = coc_nvim_1.Uri.file(path_1.resolve(utils_1.getDirname(document), fileName)).toString();
                            range_1 = vscode_languageserver_protocol_1.Range.create(vscode_languageserver_protocol_1.Position.create(0, 0), vscode_languageserver_protocol_1.Position.create(0, 0));
                            return [2 /*return*/, vscode_languageserver_protocol_1.Location.create(filePath_1, range_1)];
                        }
                        _d = this.getParts(line, position), stylesObject = _d[0], className = _d[1];
                        importPath = utils_1.getImportPath(document, stylesObject);
                        if (!importPath || !fs_1.existsSync(importPath)) {
                            return [2 /*return*/, null];
                        }
                        classNamePosition = this.getPosition(importPath, className);
                        if (!classNamePosition) {
                            return [2 /*return*/, null];
                        }
                        filePath = coc_nvim_1.Uri.file(importPath).toString();
                        range = vscode_languageserver_protocol_1.Range.create(classNamePosition, classNamePosition);
                        return [2 /*return*/, vscode_languageserver_protocol_1.Location.create(filePath, range)];
                }
            });
        });
    };
    return CSSModulesDefinitionProvider;
}());
exports.default = CSSModulesDefinitionProvider;
//# sourceMappingURL=DefinitionProvider.js.map