Skip to content

Commit

Permalink
Merge pull request #4 from NangoHQ/fix-double-type
Browse files Browse the repository at this point in the history
remove double type produced from the enforce-proxy-configuration rule
  • Loading branch information
khaliqgant authored Sep 23, 2024
2 parents 8181502 + 4ec1318 commit c99bd4b
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions src/rules/enforce-proxy-configuration-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,70 @@ const enforceProxyConfigurationType: Rule.RuleModule = {
if (node.source.value === '../../models') {
importNode = node;
hasProxyConfigurationImport = node.specifiers.some(
(specifier) => specifier.type === 'ImportSpecifier' &&
'imported' in specifier &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name === 'ProxyConfiguration'
(specifier) =>
specifier.type === 'ImportSpecifier' &&
'imported' in specifier &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name === 'ProxyConfiguration'
);
}
},
VariableDeclaration(node: VariableDeclaration) {
const declarator = node.declarations[0] as VariableDeclarator;
if (declarator && declarator.type === 'VariableDeclarator' &&
declarator.id.type === 'Identifier' && declarator.init &&
declarator.init.type === 'ObjectExpression') {
if (
declarator &&
declarator.type === 'VariableDeclarator' &&
declarator.id.type === 'Identifier' &&
declarator.init &&
declarator.init.type === 'ObjectExpression'
) {
const properties = declarator.init.properties;
if (properties.some((prop): prop is Property =>
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'endpoint')) {
if (
properties.some(
(prop): prop is Property =>
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'endpoint'
)
) {
configVariableName = declarator.id.name;
configNode = node;
}
}
},
'Program:exit'() {
if (configVariableName && !hasProxyConfigurationImport && importNode && configNode) {
context.report({
node: context.getSourceCode().ast,
message: 'ProxyConfiguration type should be imported and used for Nango API call configurations',
fix(fixer) {
const fixes = [];
const declarator = configNode.declarations[0] as VariableDeclarator;

if (importNode && importNode.specifiers.length > 0) {
fixes.push(fixer.insertTextAfter(
importNode.specifiers[importNode.specifiers.length - 1],
', ProxyConfiguration'
));
}
const hasTypeAnnotation =
'typeAnnotation' in declarator.id && !!(declarator.id as any).typeAnnotation;

if (configNode && configNode.declarations.length > 0) {
const configDeclarator = configNode.declarations[0] as VariableDeclarator;
if (configDeclarator && configDeclarator.id.type === 'Identifier') {
fixes.push(fixer.insertTextAfter(
configDeclarator.id,
': ProxyConfiguration'
));
if (declarator.id.type === 'Identifier' && !hasTypeAnnotation) {
context.report({
node: context.getSourceCode().ast,
message: 'ProxyConfiguration type should be imported and used for Nango API call configurations',
fix(fixer) {
const fixes = [];

if (importNode && importNode.specifiers.length > 0) {
fixes.push(
fixer.insertTextAfter(
importNode.specifiers[importNode.specifiers.length - 1],
', ProxyConfiguration'
)
);
}

if (declarator && declarator.id.type === 'Identifier') {
fixes.push(
fixer.insertTextAfter(declarator.id, ': ProxyConfiguration')
);
}
}

return fixes;
},
});
return fixes;
},
});
}
}
},
};
Expand Down

0 comments on commit c99bd4b

Please sign in to comment.