UNPKG

@redocly/openapi-core

Version:

See https://github.com/Redocly/redocly-cli

82 lines 2.66 kB
export const NoUnusedComponents = () => { const components = new Map(); function registerComponent(location, name) { components.set(location.absolutePointer, { used: components.get(location.absolutePointer)?.used || false, location, name, }); } return { ref(ref, { type, resolve, key, location }) { if ([ 'Schema', 'ContentDescriptor', 'Example', 'Link', 'ErrorObject', 'ExamplePairing', 'Tag', ].includes(type.name)) { const resolvedRef = resolve(ref); if (!resolvedRef.location) return; components.set(resolvedRef.location.absolutePointer, { used: true, name: key.toString(), location, }); } }, Root: { leave(_, { report }) { components.forEach((usageInfo) => { if (!usageInfo.used) { report({ message: `Component: "${usageInfo.name}" is never used.`, location: usageInfo.location.key(), }); } }); }, }, NamedSchemas: { Schema(schema, { location, key }) { if (!schema.allOf) { registerComponent(location, key.toString()); } }, }, NamedContentDescriptors: { ContentDescriptor(_node, { location, key }) { registerComponent(location, key.toString()); }, }, NamedExamples: { Example(_node, { location, key }) { registerComponent(location, key.toString()); }, }, NamedLinks: { Link(_node, { location, key }) { registerComponent(location, key.toString()); }, }, NamedErrors: { ErrorObject(_node, { location, key }) { registerComponent(location, key.toString()); }, }, NamedExamplePairingObjects: { ExamplePairing(_node, { location, key }) { registerComponent(location, key.toString()); }, }, NamedTags: { Tag(_node, { location, key }) { registerComponent(location, key.toString()); }, }, }; }; //# sourceMappingURL=no-unused-components.js.map