Skip to content

Commit

Permalink
perf: don't return rules when outside of commands dir
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 5, 2022
1 parent 216b8cf commit fbc44d1
Show file tree
Hide file tree
Showing 24 changed files with 692 additions and 657 deletions.
26 changes: 14 additions & 12 deletions src/rules/commandExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ export const commandExamples = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (isInCommandDirectory(context) && extendsSfCommand(node)) {
if (!node.body.body.some((member) => getClassPropertyIdentifierName(member) === 'examples')) {
context.report({
node,
messageId: 'example',
});
}
return isInCommandDirectory(context)
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (extendsSfCommand(node)) {
if (!node.body.body.some((member) => getClassPropertyIdentifierName(member) === 'examples')) {
context.report({
node: node.id,
messageId: 'example',
});
}
}
},
}
},
};
: {};
},
});
26 changes: 14 additions & 12 deletions src/rules/commandSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ export const commandSummary = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (isInCommandDirectory(context) && extendsSfCommand(node)) {
if (!node.body.body.some((member) => getClassPropertyIdentifierName(member) === 'summary')) {
context.report({
node: node.id,
messageId: 'summary',
});
}
return isInCommandDirectory(context)
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (extendsSfCommand(node)) {
if (!node.body.body.some((member) => getClassPropertyIdentifierName(member) === 'summary')) {
context.report({
node: node.id,
messageId: 'summary',
});
}
}
},
}
},
};
: {};
},
});
48 changes: 26 additions & 22 deletions src/rules/dash-h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@ export const dashH = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
Property(node): void {
// is a flag
if (isInCommandDirectory(context) && isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (
node.value?.type === 'CallExpression' &&
node.value.arguments?.[0]?.type === 'ObjectExpression' &&
node.value.arguments[0].properties.find(
(property) =>
property.type === 'Property' &&
flagPropertyIsNamed(property, 'char') &&
property.value.type === AST_NODE_TYPES.Literal &&
property.value.value === 'h'
)
) {
context.report({
node,
messageId: 'message',
});
}
return isInCommandDirectory(context)
? {
Property(node): void {
// is a flag
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression
) {
const hChar = node.value.arguments[0].properties.find(
(property) =>
property.type === AST_NODE_TYPES.Property &&
flagPropertyIsNamed(property, 'char') &&
property.value.type === AST_NODE_TYPES.Literal &&
property.value.value === 'h'
);
if (hChar) {
context.report({
node: hChar,
messageId: 'message',
});
}
}
},
}
},
};
: {};
},
});
34 changes: 18 additions & 16 deletions src/rules/extractMessageCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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
*/
import { ESLintUtils } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { extendsSfCommand, getClassPropertyIdentifierName, isInCommandDirectory } from '../shared/commands';

const propertiesYouShouldntHardCode = ['description', 'summary'];
Expand All @@ -24,22 +24,24 @@ export const extractMessageCommand = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (isInCommandDirectory(context) && extendsSfCommand(node)) {
node.body.body
.filter((prop) => propertiesYouShouldntHardCode.includes(getClassPropertyIdentifierName(prop)))
.forEach((prop) => {
if (prop.type === 'PropertyDefinition' && prop.value.type === 'Literal') {
context.report({
node,
messageId: 'message',
return isInCommandDirectory(context)
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (extendsSfCommand(node)) {
node.body.body
.filter((prop) => propertiesYouShouldntHardCode.includes(getClassPropertyIdentifierName(prop)))
.forEach((prop) => {
if (prop.type === AST_NODE_TYPES.PropertyDefinition && prop.value.type === AST_NODE_TYPES.Literal) {
context.report({
node: prop,
messageId: 'message',
});
}
});
}
});
}
},
}
},
};
: {};
},
});
39 changes: 19 additions & 20 deletions src/rules/extractMessageFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@ export const extractMessageFlags = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
Property(node): void {
if (!isInCommandDirectory(context)) {
return;
return isInCommandDirectory(context)
? {
Property(node): void {
const ancestors = context.getAncestors();
if (
node.key.type === AST_NODE_TYPES.Identifier &&
(node.key.name === 'summary' || node.key.name === 'description') &&
ancestors.some((a) => isFlag(a)) &&
ancestorsContainsSfCommand(ancestors)
) {
if (node.value.type === AST_NODE_TYPES.Literal) {
context.report({
node,
messageId: 'message',
});
}
}
},
}
const ancestors = context.getAncestors();
if (
node.key.type === AST_NODE_TYPES.Identifier &&
(node.key.name === 'summary' || node.key.name === 'description') &&
ancestors.some((a) => isFlag(a)) &&
ancestorsContainsSfCommand(ancestors)
) {
if (node.value.type === 'Literal') {
context.report({
node,
messageId: 'message',
});
}
}
},
};
: {};
},
});
34 changes: 18 additions & 16 deletions src/rules/flagCasing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ export const flagCasing = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
Property(node): void {
if (isInCommandDirectory(context) && isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
const flagName = getFlagName(node);
if (toLowerKebabCase(flagName) !== flagName) {
context.report({
node,
messageId: 'message',
data: { flagName },
fix: (fixer) => {
return fixer.replaceText(node.key, `'${toLowerKebabCase(flagName)}'`);
},
});
}
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
const flagName = getFlagName(node);
if (toLowerKebabCase(flagName) !== flagName) {
context.report({
node,
messageId: 'message',
data: { flagName },
fix: (fixer) => {
return fixer.replaceText(node.key, `'${toLowerKebabCase(flagName)}'`);
},
});
}
}
},
}
},
};
: {};
},
});
89 changes: 44 additions & 45 deletions src/rules/flagCrossReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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
*/
import { ESLintUtils } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { ancestorsContainsSfCommand, isInCommandDirectory } from '../shared/commands';
import { isFlag, isFlagsStaticProperty } from '../shared/flags';

Expand All @@ -25,55 +25,54 @@ export const flagCrossReferences = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
return {
Property(node): void {
if (!isInCommandDirectory(context)) {
return;
}
const ancestors = context.getAncestors();
if (
node.key.type === 'Identifier' &&
node.value.type === 'ArrayExpression' &&
node.value.elements.every((e) => e.type === 'Literal' && e.raw) &&
propertyNames.includes(node.key.name) &&
ancestorsContainsSfCommand(ancestors) &&
ancestors.some((a) => isFlag(a))
) {
const flagsNode = ancestors.find((a) => isFlagsStaticProperty(a));
return isInCommandDirectory(context)
? {
Property(node): void {
const ancestors = context.getAncestors();
if (
node.key.type === AST_NODE_TYPES.Identifier &&
node.value.type === AST_NODE_TYPES.ArrayExpression &&
node.value.elements.every((e) => e.type === AST_NODE_TYPES.Literal && e.raw) &&
propertyNames.includes(node.key.name) &&
ancestorsContainsSfCommand(ancestors) &&
ancestors.some((a) => isFlag(a))
) {
const flagsNode = ancestors.find((a) => isFlagsStaticProperty(a));

const arrayValues = node.value.elements
.map((e) => (e.type === 'Literal' ? e.value : undefined))
.filter(Boolean);
const arrayValues = node.value.elements
.map((e) => (e.type === AST_NODE_TYPES.Literal ? e.value : undefined))
.filter(Boolean);

if (
flagsNode?.type === 'PropertyDefinition' &&
flagsNode.key.type === 'Identifier' &&
flagsNode.value.type === 'ObjectExpression'
) {
// get the names of all the flags as an array
const flagNames = flagsNode.value.properties.map((flagProp) => {
if (flagProp.type === 'Property') {
if (flagProp.key.type === 'Identifier') {
return flagProp.key.name;
} else if (flagProp.key.type === 'Literal') {
return flagProp.key.value;
}
}
});
if (
flagsNode?.type === AST_NODE_TYPES.PropertyDefinition &&
flagsNode.key.type === AST_NODE_TYPES.Identifier &&
flagsNode.value.type === AST_NODE_TYPES.ObjectExpression
) {
// get the names of all the flags as an array
const flagNames = flagsNode.value.properties.map((flagProp) => {
if (flagProp.type === AST_NODE_TYPES.Property) {
if (flagProp.key.type === AST_NODE_TYPES.Identifier) {
return flagProp.key.name;
} else if (flagProp.key.type === AST_NODE_TYPES.Literal) {
return flagProp.key.value;
}
}
});

// for each of the _Literal_ values in the dependOn/exactlyOne/exclusive array
arrayValues.forEach((value) => {
if (!flagNames.includes(value)) {
context.report({
node,
messageId: 'missingFlag',
data: { flagName: value },
// for each of the _Literal_ values in the dependOn/exactlyOne/exclusive array
arrayValues.forEach((value) => {
if (!flagNames.includes(value)) {
context.report({
node,
messageId: 'missingFlag',
data: { flagName: value },
});
}
});
}
});
}
}
},
}
},
};
: {};
},
});
Loading

0 comments on commit fbc44d1

Please sign in to comment.