Skip to content

Commit 3d47ec4

Browse files
authored
fix(core): reset path in fn context (#2389)
1 parent 6bbb6cb commit 3d47ec4

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

packages/core/src/__tests__/linter.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,4 +1682,71 @@ responses:: !!foo
16821682
}),
16831683
]);
16841684
});
1685+
1686+
test.concurrent('should reset path provided in fn context', async () => {
1687+
const spectral = new Spectral();
1688+
const fn = jest.fn();
1689+
1690+
spectral.setRuleset({
1691+
rules: {
1692+
'valid-info': {
1693+
given: '$.info',
1694+
then: [
1695+
{
1696+
field: 'title',
1697+
function: truthy,
1698+
},
1699+
{
1700+
function: fn,
1701+
},
1702+
{
1703+
field: 'description',
1704+
function: truthy,
1705+
},
1706+
{
1707+
function: fn,
1708+
},
1709+
],
1710+
},
1711+
},
1712+
});
1713+
1714+
const documentUri = path.join(__dirname, './__fixtures__/test.json');
1715+
const document = new Document(
1716+
JSON.stringify({
1717+
info: {
1718+
title: 'test',
1719+
description: 'some description',
1720+
},
1721+
}),
1722+
Parsers.Json,
1723+
documentUri,
1724+
);
1725+
1726+
await expect(spectral.run(document)).resolves.toEqual([]);
1727+
1728+
expect(fn).nthCalledWith(
1729+
1,
1730+
{
1731+
title: 'test',
1732+
description: 'some description',
1733+
},
1734+
null,
1735+
expect.objectContaining({
1736+
path: ['info'],
1737+
}),
1738+
);
1739+
1740+
expect(fn).nthCalledWith(
1741+
2,
1742+
{
1743+
title: 'test',
1744+
description: 'some description',
1745+
},
1746+
null,
1747+
expect.objectContaining({
1748+
path: ['info'],
1749+
}),
1750+
);
1751+
});
16851752
});

packages/core/src/runner/lintNode.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export const lintNode = (context: IRunnerInternalContext, node: IGivenNode, rule
2424
for (const target of targets) {
2525
if (target.path.length > 0) {
2626
fnContext.path = [...givenPath, ...target.path];
27+
} else {
28+
fnContext.path = givenPath;
2729
}
2830

2931
let targetResults;

0 commit comments

Comments
 (0)