@stedi/prettier-plugin-jsonata
Version:
Prettier plugin for JSONata language
110 lines (109 loc) • 5.35 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatJsonata = formatJsonata;
exports.formatJsonataSync = formatJsonataSync;
exports.serializeJsonataSync = serializeJsonataSync;
exports.serializeJsonata = serializeJsonata;
const prettierPlugin = __importStar(require("../plugin"));
const standalone_1 = require("prettier/standalone");
const parser_1 = require("../plugin/parser");
const printer_1 = require("../plugin/printer");
/**
* Re-formats JSONata expression string in an opinionated way on where to put line breaks and whitespace.
*/
function formatJsonata(expression, options) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, standalone_1.format)(expression, Object.assign({ parser: prettierPlugin.AST_PARSER_NAME, plugins: [prettierPlugin], printWidth: 150, tabWidth: 2, useTabs: false }, options));
});
}
function formatJsonataSync(expression, options) {
const doc = (0, standalone_1.printToDocSync)(expression, {
parser: prettierPlugin.AST_PARSER_NAME,
plugins: [prettierPlugin],
printer: {
print: printer_1.print,
},
});
const { formatted } = (0, standalone_1.printDocToStringSync)(doc, Object.assign({ parser: prettierPlugin.AST_PARSER_NAME, plugins: [prettierPlugin], printWidth: 150, tabWidth: 2, useTabs: false }, options));
return formatted;
}
function serializeJsonataSync(jsonataAST, options) {
const pluginBoundToAST = buildPluginBoundToAST(jsonataAST);
const doc = (0, standalone_1.printAstToDocSync)(jsonataAST, {
parser: prettierPlugin.AST_PARSER_NAME,
plugins: [pluginBoundToAST],
printer: {
print: printer_1.print,
},
});
const { formatted } = (0, standalone_1.printDocToStringSync)(doc, Object.assign({ parser: prettierPlugin.AST_PARSER_NAME, plugins: [pluginBoundToAST], printWidth: 150, tabWidth: 2, useTabs: false }, options));
return formatted;
}
/**
* Serializes JSONata AST to a formatted string representing JSONata expression.
*/
function serializeJsonata(jsonataAST, options) {
return __awaiter(this, void 0, void 0, function* () {
const pluginBoundToAST = buildPluginBoundToAST(jsonataAST);
return yield (0, standalone_1.format)("ignore text input", Object.assign({ parser: prettierPlugin.AST_PARSER_NAME, plugins: [pluginBoundToAST], printWidth: 150, tabWidth: 2, useTabs: false }, options));
});
}
/**
* Prettier does not allow to skip the "parse" step completely, and jump straight to converting AST to a formatted string,
* but there are use-cases when we need to format only a part of the full AST tree without access to the original string representation of it.
*
* To work around it, we are adding this dynamic plugin generator, which returns its AST argument as the result of `parse` callback
* without any parsing, ignoring text input.
*/
const buildPluginBoundToAST = (jsonataAST) => {
return Object.assign(Object.assign({}, prettierPlugin), { parsers: {
[prettierPlugin.AST_PARSER_NAME]: {
astFormat: prettierPlugin.AST_FORMAT_NAME,
parse: () => jsonataAST,
locStart: parser_1.locStart,
locEnd: parser_1.locEnd,
},
} });
};
;