{"size": 1556, "ext": "js", "max_stars_repo_path": "modules/commands/toggle-property-declaration/toggle-property-declaration-action.js", "max_stars_repo_name": "cmstead/js-codeformer", "max_stars_repo_stars_event_min_datetime": "2021-08-01T02:13:21.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-07T21:31:52.000Z", "max_issues_repo_path": "modules/commands/toggle-property-declaration/toggle-property-declaration-action.js", "max_issues_repo_name": "cmstead/js-codeformer", "max_issues_repo_issues_event_min_datetime": "2021-08-12T13:43:38.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-17T03:59:05.000Z", "max_forks_repo_path": "modules/commands/toggle-property-declaration/toggle-property-declaration-action.js", "max_forks_repo_name": "cmstead/js-codeformer", "max_forks_repo_forks_event_min_datetime": "2021-09-18T17:37:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-18T17:37:50.000Z"}
{"max_stars_count": 16, "max_issues_count": 26, "max_forks_count": 1, "avg_line_length": 43.2222222222, "max_line_length": 110, "alphanum_fraction": 0.6690231362}
const { asyncPrepareActionSetup } = require("../../action-setup");
const { PROPERTY } = require("../../constants/ast-node-types");
const { findNodeInPath } = require("../../edit-utils/node-path-utils");
const { getNewSourceEdit } = require("../../edit-utils/SourceEdit");
const { transformLocationToRange } = require("../../edit-utils/textEditTransforms");
const { buildInfoMessage, parseAndShowMessage } = require("../../ui-services/messageService");
const { validateUserInput } = require("../../validatorService");
const { getPropertyDeclarationString, isConvertablePropertyNode } = require("./toggle-property-declaration");

function togglePropertyDeclaration() {
    return asyncPrepareActionSetup()

        .then((actionSetup) => findNodeInPath(actionSetup.selectionPath, PROPERTY))
        .then((node) => validateUserInput({
            value: node,
            validator: (node) => node !== null && isConvertablePropertyNode(node),
            message: buildInfoMessage('Unable to find acceptable property node; canceling toggle declaration')
        }))

        .then((propertyNode) => {
            const replacementRange = transformLocationToRange(propertyNode.loc);
            const replacementString = getPropertyDeclarationString(propertyNode);

            return getNewSourceEdit()
                .addReplacementEdit(replacementRange, replacementString)
                .applyEdit();
        })

        .catch(function (error) {
            parseAndShowMessage(error);
        });
}

module.exports = {
    togglePropertyDeclaration
};