Skip to content

Commit 115d1d0

Browse files
authored
fix(ruleset-migrator): fix ruleset migrator output when a rule name contains '/' (#2859)
1 parent 724c7f8 commit 115d1d0

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

packages/ruleset-migrator/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseWithPointers as parseJsonWithPointers, safeStringify } from '@stoplight/json';
1+
import { parseWithPointers as parseJsonWithPointers, pathToPointer, safeStringify } from '@stoplight/json';
22
import { parseWithPointers as parseYamlWithPointers } from '@stoplight/yaml';
33
import { fetch as defaultFetch } from '@stoplight/spectral-runtime';
44
import { dirname, extname, isURL } from '@stoplight/path';
@@ -59,9 +59,13 @@ export async function migrateRuleset(filepath: string, opts: MigrationOptions):
5959
return tree.toString();
6060
}
6161

62-
async function _process(input: unknown, ctx: TransformerCtx, path: string): Promise<ExpressionKind | null> {
62+
async function _process(input: unknown, ctx: TransformerCtx, path: string[]): Promise<ExpressionKind | null> {
63+
// Convert the path to a /a/b/c format, using the JSON Pointer tools to escape '/' properly in keys.
64+
// This is needed because rule names can contain '/' characters.
65+
const pathStr = pathToPointer(path).slice(1);
66+
6367
for (const [pattern, fn] of ctx.hooks) {
64-
if (pattern.test(path)) {
68+
if (pattern.test(pathStr)) {
6569
const output = await fn(input, ctx);
6670

6771
if (output !== void 0) {
@@ -72,7 +76,7 @@ async function _process(input: unknown, ctx: TransformerCtx, path: string): Prom
7276

7377
if (Array.isArray(input)) {
7478
return b.arrayExpression(
75-
(await Promise.all(input.map(async (item, i) => await _process(item, ctx, `${path}/${String(i)}`)))).filter(
79+
(await Promise.all(input.map(async (item, i) => await _process(item, ctx, [...path, String(i)])))).filter(
7680
Boolean,
7781
),
7882
);
@@ -90,7 +94,7 @@ async function _process(input: unknown, ctx: TransformerCtx, path: string): Prom
9094
(
9195
await Promise.all(
9296
Object.entries(input).map(async ([key, value]) => {
93-
const propertyValue = await _process(value, ctx, `${path}/${key}`);
97+
const propertyValue = await _process(value, ctx, [...path, key]);
9498

9599
if (propertyValue !== null) {
96100
return b.property('init', b.identifier(JSON.stringify(key)), propertyValue);
@@ -104,5 +108,5 @@ async function _process(input: unknown, ctx: TransformerCtx, path: string): Prom
104108
}
105109

106110
export async function process(input: Ruleset, ctx: TransformerCtx): Promise<namedTypes.ObjectExpression> {
107-
return (await _process(input, ctx, '')) as namedTypes.ObjectExpression;
111+
return (await _process(input, ctx, [])) as namedTypes.ObjectExpression;
108112
}

0 commit comments

Comments
 (0)