eslint-plugin-sf-plugin
Version:
Helpful eslint rules for sf plugins.
59 lines (58 loc) • 2.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldParseFlags = void 0;
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const eslint_utils_1 = require("@typescript-eslint/utils/eslint-utils");
const commands_1 = require("../../shared/commands");
const flags_1 = require("../../shared/flags");
exports.shouldParseFlags = eslint_utils_1.RuleCreator.withoutDocs({
meta: {
docs: {
description: 'The run method should call this.parse when there are flags',
recommended: 'recommended',
},
messages: {
summary: 'The run method should call this.parse when there are flags',
},
type: 'problem',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
return (0, commands_1.isInCommandDirectory)(context)
? {
// eslint-disable-next-line complexity
MethodDefinition(node) {
var _a, _b, _c, _d, _e;
if ((0, commands_1.isRunMethod)(node) && ((_b = (_a = node.value) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.body)) {
// OK, looks like a run method has a type annotation
const ancestors = context.getAncestors();
const classDeclaration = (0, commands_1.getSfCommand)(context);
if (
// and it has flags to be parsed
(_d = (_c = classDeclaration === null || classDeclaration === void 0 ? void 0 : classDeclaration.body) === null || _c === void 0 ? void 0 : _c.body) === null || _d === void 0 ? void 0 : _d.some((prop) => (0, flags_1.isFlagsStaticProperty)(prop))) {
// get the text for the two nodes
const sourceCode = context.sourceCode;
const runBody = sourceCode.getText(node.value.body);
const className = (_e = classDeclaration.id) === null || _e === void 0 ? void 0 : _e.name;
if (!runBody.includes(`await this.parse(${className})`)) {
const target = node.value.body.body[0];
context.report({
node,
messageId: 'summary',
fix: (fixer) => fixer.insertTextBefore(target, `const {flags} = await this.parse(${className});`),
});
}
}
}
},
}
: {};
},
});
;