Skip to content

Commit

Permalink
fix: handle import aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiand391 committed Jul 23, 2024
1 parent 127a366 commit 17e392b
Show file tree
Hide file tree
Showing 77 changed files with 344 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/rules/command-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const commandExamples = createRule({
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (extendsSfCommand(node) && node.id) {
if (extendsSfCommand(node,context) && node.id) {
if (!node.body.body.some((member) => getClassPropertyIdentifierName(member) === 'examples')) {
context.report({
node: node.id,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/command-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const commandSummary = RuleCreator.withoutDocs({
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (extendsSfCommand(node) && node.id) {
if (extendsSfCommand(node, context) && node.id) {
if (!node.body.body.some((member) => getClassPropertyIdentifierName(member) === 'summary')) {
const descriptionNode = node.body.body.find(
(member) => getClassPropertyIdentifierName(member) === 'description'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/dash-h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const dashH = RuleCreator.withoutDocs({
// is a flag
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
ancestorsContainsSfCommand(context) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression
) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/dash-o.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const dashO = RuleCreator.withoutDocs({
// is a flag
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
ancestorsContainsSfCommand(context) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value.callee.property.type === AST_NODE_TYPES.Identifier &&
Expand Down
2 changes: 1 addition & 1 deletion src/rules/extract-message-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const extractMessageCommand = RuleCreator.withoutDocs({
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (extendsSfCommand(node)) {
if (extendsSfCommand(node, context)) {
node.body.body
.filter((prop) =>
// this could be `undefined` but that works okay with `.includes`
Expand Down
2 changes: 1 addition & 1 deletion src/rules/extract-message-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const extractMessageFlags = RuleCreator.withoutDocs({
node.key.type === AST_NODE_TYPES.Identifier &&
(node.key.name === 'summary' || node.key.name === 'description') &&
ancestors.some((a) => isFlag(a)) &&
ancestorsContainsSfCommand(ancestors)
ancestorsContainsSfCommand(context)
) {
if (node.value.type === AST_NODE_TYPES.Literal) {
context.report({
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flag-casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const flagCasing = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
const flagName = resolveFlagName(node);
if (flagName && toLowerKebabCase(flagName) !== flagName) {
context.report({
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flag-cross-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const flagCrossReferences = RuleCreator.withoutDocs({
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) &&
ancestorsContainsSfCommand(context) &&
ancestors.some((a) => isFlag(a))
) {
const flagsNode = ancestors
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flag-min-max-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const flagMinMaxDefault = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
if (
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression &&
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flag-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const flagSummary = RuleCreator.withoutDocs({
Property(node): void {
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
ancestorsContainsSfCommand(context) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression
) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/get-connections-with-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getConnectionWithVersion = RuleCreator.withoutDocs({
node.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
node.callee.property?.name === 'getConnection' &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
context.report({
node: node.callee.property,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/id-flag-suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const idFlagSuggestions = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
if (
(node.key.type === AST_NODE_TYPES.Identifier || node.key.type === AST_NODE_TYPES.Literal) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
Expand Down
4 changes: 2 additions & 2 deletions src/rules/migration/encourage-alias-deprecation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const encourageAliasDeprecation = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
PropertyDefinition(node): void {
if (ancestorsContainsSfCommand(context.getAncestors())) {
if (ancestorsContainsSfCommand(context)) {
if (node.key.type === AST_NODE_TYPES.Identifier && node.key.name === 'aliases') {
// but you don't have deprecateAliases = true then add id
if (
Expand Down Expand Up @@ -63,7 +63,7 @@ export const encourageAliasDeprecation = RuleCreator.withoutDocs({
isFlag(node) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
const argProps = node.value.arguments[0].properties.filter(
ASTUtils.isNodeOfType(AST_NODE_TYPES.Property)
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-builtin-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const noBuiltinFlags = RuleCreator.withoutDocs({
isFlag(node) &&
node.key.type === AST_NODE_TYPES.Identifier &&
builtInFlagTypes.includes(node.key.name) &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
const toReplace = getCalleePropertyByName(node, 'builtin');
if (toReplace) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-deprecated-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const noDeprecatedProperties = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
PropertyDefinition(node): void {
if (ancestorsContainsSfCommand(context.getAncestors())) {
if (ancestorsContainsSfCommand(context)) {
if (
node.key.type === AST_NODE_TYPES.Identifier &&
['supportsDevhubUsername', 'varargs'].includes(node.key.name)
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-filepath-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const noFilepathFlags = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
const toReplace = getCalleePropertyByName(node, 'filepath');
if (toReplace) {
context.report({
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-id-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const noIdFlags = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
const toReplace = getCalleePropertyByName(node, 'id');
if (toReplace) {
context.report({
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-number-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const noNumberFlags = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
const toReplace = getCalleePropertyByName(node, 'number');
if (toReplace) {
context.report({
Expand Down
4 changes: 2 additions & 2 deletions src/rules/migration/no-this-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export const noThisFlags = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
MemberExpression(node): void {
if (MemberExpressionIsThisDotFoo(node, 'flags') && ancestorsContainsSfCommand(context.getAncestors())) {
if (MemberExpressionIsThisDotFoo(node, 'flags') && ancestorsContainsSfCommand(context)) {
// it's ok if there's a this.flags on the class...
const classAbove = getSfCommand(context.getAncestors());
const classAbove = getSfCommand(context);
if (!classAbove) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/migration/no-this-org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const noThisOrg = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
MemberExpression(node): void {
if (MemberExpressionIsThisDotFoo(node, 'org') && ancestorsContainsSfCommand(context.getAncestors())) {
if (MemberExpressionIsThisDotFoo(node, 'org') && ancestorsContainsSfCommand(context)) {
// it's ok if there's a this.org on the class...
const classAbove = getSfCommand(context.getAncestors());
const classAbove = getSfCommand(context);
if (!classAbove) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-this-ux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const noThisUx = RuleCreator.withoutDocs({
MemberExpression(node): void {
if (
MemberExpressionContainsMemberExpressionThisDotFoo(node, 'ux') &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
// spinner cases
if (node.property.type === AST_NODE_TYPES.Identifier && spinnerMigration.has(node.property.name)) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/no-time-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const noTimeFlags = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
if (
(node.key.type === AST_NODE_TYPES.Identifier || node.key.type === AST_NODE_TYPES.Literal) &&
node.value.type === AST_NODE_TYPES.CallExpression &&
Expand Down
4 changes: 2 additions & 2 deletions src/rules/migration/no-username-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const noUsernameProperties = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
PropertyDefinition(node): void {
if (ancestorsContainsSfCommand(context.getAncestors())) {
if (ancestorsContainsSfCommand(context)) {
if (node.key.type === AST_NODE_TYPES.Identifier && propertyMap.has(node.key.name)) {
const mappedMetadata = propertyMap.get(node.key.name);
if (!mappedMetadata) {
Expand All @@ -85,7 +85,7 @@ export const noUsernameProperties = RuleCreator.withoutDocs({

// add the flag if not already present

const outerClass = getSfCommand(ancestors);
const outerClass = getSfCommand(context);
if (!outerClass) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/sfdx-flags-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const sfdxFlagsProperty = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
PropertyDefinition(node): void {
if (ancestorsContainsSfCommand(context.getAncestors())) {
if (ancestorsContainsSfCommand(context)) {
if (node.key.type === AST_NODE_TYPES.Identifier && node.key.name === 'flagsConfig') {
context.report({
node,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/should-parse-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const shouldParseFlags = RuleCreator.withoutDocs({
if (isRunMethod(node) && node.value?.body?.body) {
// OK, looks like a run method has a type annotation
const ancestors = context.getAncestors();
const classDeclaration = getSfCommand(ancestors);
const classDeclaration = getSfCommand(context);
if (
// and it has flags to be parsed
classDeclaration?.body?.body?.some((prop) => isFlagsStaticProperty(prop))
Expand Down
2 changes: 1 addition & 1 deletion src/rules/migration/use-sf-command-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const useSfCommandFlags = RuleCreator.withoutDocs({
node.value?.callee?.type === AST_NODE_TYPES.MemberExpression &&
node.value?.callee?.object?.type === AST_NODE_TYPES.Identifier &&
node.value?.callee?.object?.name === 'flags' &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
const range = node.value.callee.object.range;
context.report({
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-args-parse-without-strict-false.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export const noArgsParseWithoutStrictFalse = RuleCreator.withoutDocs({
)
) {
// Verify that the class has strict = false
const ancestors = context.getAncestors();
const sfCommand = getSfCommand(ancestors);
const sfCommand = getSfCommand(context);
if (!sfCommand) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-default-depends-on-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const noDefaultDependsOnFlags = RuleCreator.withoutDocs({
// is a flag
if (
isFlag(node) &&
ancestorsContainsSfCommand(context.getAncestors()) &&
ancestorsContainsSfCommand(context) &&
node.value?.type === AST_NODE_TYPES.CallExpression &&
node.value.arguments?.[0]?.type === AST_NODE_TYPES.ObjectExpression
) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-duplicate-short-characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const noDuplicateShortCharacters = RuleCreator.withoutDocs({
PropertyDefinition(node): void {
// is "public static flags" property
if (
ancestorsContainsSfCommand(context.getAncestors()) &&
ancestorsContainsSfCommand(context) &&
node.value?.type === AST_NODE_TYPES.ObjectExpression &&
isFlagsStaticProperty(node)
) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-hyphens-aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const noHyphenAliases = RuleCreator.withoutDocs({
node.parent.parent.key.name === 'aliases' &&
node.parent.parent.parent?.parent?.parent &&
isFlag(node.parent.parent.parent?.parent?.parent) &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
context.report({
node,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-json-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const jsonFlag = RuleCreator.withoutDocs({
return isInCommandDirectory(context)
? {
Property(node): void {
if (isFlag(node) && ancestorsContainsSfCommand(context.getAncestors())) {
if (isFlag(node) && ancestorsContainsSfCommand(context)) {
if (node.key.type === AST_NODE_TYPES.Identifier && node.key.name === 'json') {
context.report({
node,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-split-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const noSplitExamples = RuleCreator.withoutDocs({
node.value.callee.object.callee.property.name === 'getMessage' &&
node.value.callee.property.type === AST_NODE_TYPES.Identifier &&
node.value.callee.property.name === 'split' &&
extendsSfCommand(node.parent.parent)
extendsSfCommand(node.parent.parent, context)
) {
const target = node.value;
const fixedText = context
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-unnecessary-aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const noUnnecessaryAliases = RuleCreator.withoutDocs({
node.parent.parent.key.type === AST_NODE_TYPES.Identifier &&
node.parent.parent.key.name === 'aliases' &&
context.getPhysicalFilename &&
ancestorsContainsSfCommand(context.getAncestors())
ancestorsContainsSfCommand(context)
) {
const parentLength = node.parent.elements.length;
const cmdParts = getCommandNameParts(context.getPhysicalFilename());
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-unnecessary-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const noUnnecessaryProperties = RuleCreator.withoutDocs({
node.parent?.type === AST_NODE_TYPES.ClassBody &&
node.parent.parent?.type === AST_NODE_TYPES.ClassDeclaration &&
node.value &&
extendsSfCommand(node.parent.parent)
extendsSfCommand(node.parent.parent, context)
) {
// properties that default to false
if (
Expand Down
24 changes: 23 additions & 1 deletion src/rules/only-extend-sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,33 @@ export const onlyExtendSfCommand = RuleCreator.withoutDocs({
},
defaultOptions: [],
create(context) {
// function extendsSfCommand(node: TSESTree.ClassDeclaration, context: RuleContext<any, any>): boolean {
// // Track imported classes and their aliases
// const importedClasses = new Map();
//
// for (const node of (context.sourceCode).ast.body) {
// if (node.type === 'ImportDeclaration') {
// node.specifiers.forEach(specifier => {
// if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'SfCommand') {
// importedClasses.set(specifier.local.name, 'SfCommand');
// }
// // Handle import aliases
// else if (specifier.type === 'ImportSpecifier' && specifier.local.name !== specifier.imported.name) {
// importedClasses.set(specifier.local.name, specifier.imported.name);
// }
// })
// }
// }
//
// return node.superClass?.type === AST_NODE_TYPES.Identifier && (importedClasses.get(node.superClass.name) == 'SfCommand');
// }

return isInCommandDirectory(context)
? {
ClassDeclaration(node): void {
// verify it extends SfCommand
if (!extendsSfCommand(node) && node.id) {
// importedClasses.has()
if (!extendsSfCommand(node, context) && node.id) {
context.report({
node: node.id,
messageId: 'message',
Expand Down
2 changes: 1 addition & 1 deletion src/rules/read-only-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const readOnlyProperties = RuleCreator.withoutDocs({
props.includes(node.key.name) &&
node.parent?.type === AST_NODE_TYPES.ClassBody &&
node.parent.parent?.type === AST_NODE_TYPES.ClassDeclaration &&
extendsSfCommand(node.parent.parent)
extendsSfCommand(node.parent.parent,context)
) {
if (!node.readonly) {
context.report({
Expand Down
2 changes: 1 addition & 1 deletion src/rules/run-matches-class-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const runMatchesClassType = RuleCreator.withoutDocs({
if (isRunMethod(node) && node.value.returnType?.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference) {
// OK, run method has a type annotation. Now we need to check if the class extends SfCommand and get the <type parameter>
const ancestors = context.getAncestors();
const classDeclaration = getSfCommand(ancestors);
const classDeclaration = getSfCommand(context);

if (classDeclaration) {
// get the text for the two nodes
Expand Down
Loading

0 comments on commit 17e392b

Please sign in to comment.