Skip to content

Commit

Permalink
fix other sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zglicz committed Mar 20, 2024
1 parent 1135aec commit 0d87f33
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
28 changes: 16 additions & 12 deletions packages/jsts/src/rules/helpers/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,18 @@ export function getPropertyWithValue(
return undefined;
}

function getPropertyFromSpreadElement(
spreadElement: estree.SpreadElement,
propertyName: string,
ctx: Rule.RuleContext,
): estree.Property | null | undefined {
const props = getValueOfExpression(ctx, spreadElement.argument, 'ObjectExpression');
if (props === undefined) {
return undefined;
}
return getProperty(props, propertyName, ctx);
}

/**
* Retrieves the property with the specified key from the given node.
* @returns The property if found, or null if not found, or undefined if property not found and one of the properties
Expand All @@ -448,19 +460,11 @@ export function getProperty(
return property;
}
if (property.type === 'SpreadElement') {
const props = getValueOfExpression(ctx, property.argument, 'ObjectExpression');
const recursiveDefinition = findFirstMatchingAncestor(
property.argument as TSESTree.Node,
node => node === props,
);
if (recursiveDefinition || props === undefined) {
const prop = getPropertyFromSpreadElement(property, key, ctx);
if (prop === undefined) {
unresolvedSpreadElement = true;
} else {
const prop = getProperty(props, key, ctx);

if (prop !== null) {
return prop;
}
} else if (prop !== null) {
return prop;
}
}
}
Expand Down
45 changes: 21 additions & 24 deletions packages/jsts/tests/rules/helpers/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,28 @@ describe('getProperty', () => {
'baz',
property => expect(property).toBeUndefined(),
],
])(
'it %s',
async (_test_name: string, fixtureFile: string, key: string, verifier: (property) => void) => {
const baseDir = path.join(__dirname, 'fixtures');
])('it %s', async (_: string, fixtureFile: string, key: string, verifier: (property) => void) => {
const baseDir = path.join(__dirname, 'fixtures');

const linter = new Linter();
linter.defineRule('custom-rule-file', {
create(context: Rule.RuleContext) {
return {
'ExpressionStatement ObjectExpression': node => {
const property = getProperty(node, key, context);
verifier(property);
},
};
},
} as Rule.RuleModule);
const linter = new Linter();
linter.defineRule('custom-rule-file', {
create(context: Rule.RuleContext) {
return {
'ExpressionStatement ObjectExpression': node => {
const property = getProperty(node, key, context);
verifier(property);
},
};
},
} as Rule.RuleModule);

const filePath = path.join(baseDir, fixtureFile);
const sourceCode = await parseJavaScriptSourceFile(filePath);
const filePath = path.join(baseDir, fixtureFile);
const sourceCode = await parseJavaScriptSourceFile(filePath);

linter.verify(
sourceCode,
{ rules: { 'custom-rule-file': 'error' } },
{ filename: filePath, allowInlineConfig: false },
);
},
);
linter.verify(
sourceCode,
{ rules: { 'custom-rule-file': 'error' } },
{ filename: filePath, allowInlineConfig: false },
);
});
});

0 comments on commit 0d87f33

Please sign in to comment.