UNPKG

@swarmion/eslint-plugin

Version:

An eslint plugin with rules to enforces proper usage of contracts.

210 lines (202 loc) 7.16 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // ../../node_modules/.pnpm/find-up@7.0.0/node_modules/find-up/index.js var import_node_path2 = __toESM(require("path"), 1); // ../../node_modules/.pnpm/locate-path@7.2.0/node_modules/locate-path/index.js var import_node_process = __toESM(require("process"), 1); var import_node_path = __toESM(require("path"), 1); var import_node_fs = __toESM(require("fs"), 1); var import_node_url = require("url"); var typeMappings = { directory: "isDirectory", file: "isFile" }; function checkType(type) { if (Object.hasOwnProperty.call(typeMappings, type)) { return; } throw new Error(`Invalid type specified: ${type}`); } var matchType = (type, stat) => stat[typeMappings[type]](); var toPath = (urlOrPath) => urlOrPath instanceof URL ? (0, import_node_url.fileURLToPath)(urlOrPath) : urlOrPath; function locatePathSync(paths, { cwd = import_node_process.default.cwd(), type = "file", allowSymlinks = true } = {}) { checkType(type); cwd = toPath(cwd); const statFunction = allowSymlinks ? import_node_fs.default.statSync : import_node_fs.default.lstatSync; for (const path_ of paths) { try { const stat = statFunction(import_node_path.default.resolve(cwd, path_), { throwIfNoEntry: false }); if (!stat) { continue; } if (matchType(type, stat)) { return path_; } } catch { } } } // ../../node_modules/.pnpm/unicorn-magic@0.1.0/node_modules/unicorn-magic/node.js var import_node_url2 = require("url"); function toPath2(urlOrPath) { return urlOrPath instanceof URL ? (0, import_node_url2.fileURLToPath)(urlOrPath) : urlOrPath; } // ../../node_modules/.pnpm/path-exists@5.0.0/node_modules/path-exists/index.js var import_node_fs2 = __toESM(require("fs"), 1); function pathExistsSync(path4) { try { import_node_fs2.default.accessSync(path4); return true; } catch { return false; } } // ../../node_modules/.pnpm/find-up@7.0.0/node_modules/find-up/index.js var findUpStop = Symbol("findUpStop"); function findUpMultipleSync(name, options = {}) { let directory = import_node_path2.default.resolve(toPath2(options.cwd) ?? ""); const { root } = import_node_path2.default.parse(directory); const stopAt = import_node_path2.default.resolve(directory, toPath2(options.stopAt) ?? root); const limit = options.limit ?? Number.POSITIVE_INFINITY; const paths = [name].flat(); const runMatcher = (locateOptions) => { if (typeof name !== "function") { return locatePathSync(paths, locateOptions); } const foundPath = name(locateOptions.cwd); if (typeof foundPath === "string") { return locatePathSync([foundPath], locateOptions); } return foundPath; }; const matches = []; while (true) { const foundPath = runMatcher({ ...options, cwd: directory }); if (foundPath === findUpStop) { break; } if (foundPath) { matches.push(import_node_path2.default.resolve(directory, foundPath)); } if (directory === stopAt || matches.length >= limit) { break; } directory = import_node_path2.default.dirname(directory); } return matches; } function findUpSync(name, options = {}) { const matches = findUpMultipleSync(name, { ...options, limit: 1 }); return matches[0]; } // src/rules/noUndeclaredContracts.ts var import_fs = require("fs"); var import_path = __toESM(require("path")); var create = (context) => { return { ImportDeclaration: (node) => { const importedModule = node.source.value; if (typeof importedModule !== "string") { return; } const importedModulePackageJson = `node_modules/${importedModule}/package.json`; const targetDirectory = findUpSync( (directory) => { const packageJsonFound = pathExistsSync( import_path.default.join(directory, importedModulePackageJson) ); if (!packageJsonFound) { return void 0; } return directory; }, { type: "directory" } ); if (targetDirectory === void 0) { return; } const importeModulePackageJson = import_path.default.join( targetDirectory, importedModulePackageJson ); const packageJsonFile = JSON.parse( (0, import_fs.readFileSync)(importeModulePackageJson, "utf8") ); if (packageJsonFile.contracts !== true) { return; } const currentDirectoryServerlessConf = import_path.default.join( context.cwd, "serverless.ts" ); const closestServerlessTsPath = pathExistsSync( currentDirectoryServerlessConf ) ? currentDirectoryServerlessConf : findUpSync("serverless.ts"); if (closestServerlessTsPath === void 0) { return; } const serverlessConfFile = (0, import_fs.readFileSync)(closestServerlessTsPath, "utf-8"); const contracts = node.specifiers.map((specifier) => specifier.local.name); const invalidContracts = contracts.filter((contract) => { const contractRegex = new RegExp( `contracts: {( |.)*(provides|consumes): {( |.)*${contract}` ); return !contractRegex.test(serverlessConfFile); }); if (invalidContracts.length === 0) { return; } context.report({ node, message: `Contract${invalidContracts.length > 1 ? "s" : ""} ${invalidContracts.map((invalidContract) => `\`${invalidContract}\``).join( ", " )} from package '${importedModule}' must be declared in the \`contracts.consumes\` or \`contracts.provides\` property of the 'serverless.ts' service file` }); } }; }; var module2 = { create }; var noUndeclaredContracts_default = module2; // src/index.ts module.exports = { rules: { "no-undeclared-contracts": noUndeclaredContracts_default }, configs: { recommended: { plugins: ["@swarmion"], rules: { "@swarmion/no-undeclared-contracts": "error" } } } };