@redocly/openapi-core 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/__tests__/utils.ts +88 -0
  3. package/lib/config/all.js +0 -1
  4. package/lib/config/minimal.js +0 -1
  5. package/lib/config/recommended.js +0 -1
  6. package/package.json +1 -1
  7. package/src/__tests__/__snapshots__/bundle.test.ts.snap +437 -0
  8. package/src/__tests__/bundle.test.ts +236 -0
  9. package/src/__tests__/codeframes.test.ts +530 -0
  10. package/src/__tests__/fixtures/.redocly.lint-ignore.yaml +5 -0
  11. package/src/__tests__/fixtures/extension.js +24 -0
  12. package/src/__tests__/fixtures/refs/definitions.yaml +3 -0
  13. package/src/__tests__/fixtures/refs/examples.yaml +8 -0
  14. package/src/__tests__/fixtures/refs/external-request-body.yaml +13 -0
  15. package/src/__tests__/fixtures/refs/externalref.yaml +35 -0
  16. package/src/__tests__/fixtures/refs/hosted.yaml +35 -0
  17. package/src/__tests__/fixtures/refs/openapi-with-external-refs-conflicting-names.yaml +21 -0
  18. package/src/__tests__/fixtures/refs/openapi-with-external-refs.yaml +33 -0
  19. package/src/__tests__/fixtures/refs/openapi-with-url-refs.yaml +18 -0
  20. package/src/__tests__/fixtures/refs/param-b.yaml +1 -0
  21. package/src/__tests__/fixtures/refs/param-c.yaml +1 -0
  22. package/src/__tests__/fixtures/refs/rename.yaml +1 -0
  23. package/src/__tests__/fixtures/refs/requestBody.yaml +9 -0
  24. package/src/__tests__/fixtures/refs/schema-a.yaml +1 -0
  25. package/src/__tests__/fixtures/refs/simple.yaml +1 -0
  26. package/src/__tests__/fixtures/refs/vendor.schema.yaml +20 -0
  27. package/src/__tests__/fixtures/resolve/External.yaml +10 -0
  28. package/src/__tests__/fixtures/resolve/External2.yaml +4 -0
  29. package/src/__tests__/fixtures/resolve/description.md +3 -0
  30. package/src/__tests__/fixtures/resolve/externalInfo.yaml +4 -0
  31. package/src/__tests__/fixtures/resolve/externalLicense.yaml +1 -0
  32. package/src/__tests__/fixtures/resolve/openapi-with-back.yaml +13 -0
  33. package/src/__tests__/fixtures/resolve/openapi-with-md-description.yaml +5 -0
  34. package/src/__tests__/fixtures/resolve/openapi.yaml +28 -0
  35. package/src/__tests__/fixtures/resolve/schemas/type-a.yaml +10 -0
  36. package/src/__tests__/fixtures/resolve/schemas/type-b.yaml +6 -0
  37. package/src/__tests__/fixtures/resolve/transitive/a.yaml +1 -0
  38. package/src/__tests__/fixtures/resolve/transitive/components.yaml +5 -0
  39. package/src/__tests__/fixtures/resolve/transitive/schemas.yaml +3 -0
  40. package/src/__tests__/format.test.ts +76 -0
  41. package/src/__tests__/js-yaml.test.ts +73 -0
  42. package/src/__tests__/lint.test.ts +392 -0
  43. package/src/__tests__/logger-browser.test.ts +53 -0
  44. package/src/__tests__/logger.test.ts +47 -0
  45. package/src/__tests__/login.test.ts +17 -0
  46. package/src/__tests__/normalizeVisitors.test.ts +151 -0
  47. package/src/__tests__/output-browser.test.ts +18 -0
  48. package/src/__tests__/output.test.ts +15 -0
  49. package/src/__tests__/ref-utils.test.ts +120 -0
  50. package/src/__tests__/resolve-http.test.ts +77 -0
  51. package/src/__tests__/resolve.test.ts +431 -0
  52. package/src/__tests__/utils-browser.test.ts +11 -0
  53. package/src/__tests__/utils.test.ts +144 -0
  54. package/src/__tests__/walk.test.ts +1545 -0
  55. package/src/benchmark/benches/lint-with-many-rules.bench.ts +35 -0
  56. package/src/benchmark/benches/lint-with-nested-rule.bench.ts +39 -0
  57. package/src/benchmark/benches/lint-with-no-rules.bench.ts +20 -0
  58. package/src/benchmark/benches/lint-with-top-level-rule-report.bench.ts +35 -0
  59. package/src/benchmark/benches/lint-with-top-level-rule.bench.ts +32 -0
  60. package/src/benchmark/benches/rebilly.yaml +32275 -0
  61. package/src/benchmark/benches/recommended-oas3.bench.ts +22 -0
  62. package/src/benchmark/benches/resolve-with-no-external.bench.ts +23 -0
  63. package/src/benchmark/benchmark.js +311 -0
  64. package/src/benchmark/colors.js +29 -0
  65. package/src/benchmark/fork.js +83 -0
  66. package/src/benchmark/utils.ts +36 -0
  67. package/src/bundle.ts +417 -0
  68. package/src/config/__tests__/__snapshots__/config-resolvers.test.ts.snap +164 -0
  69. package/src/config/__tests__/__snapshots__/config.test.ts.snap +144 -0
  70. package/src/config/__tests__/config-resolvers.test.ts +491 -0
  71. package/src/config/__tests__/config.test.ts +312 -0
  72. package/src/config/__tests__/fixtures/ingore-file.ts +8 -0
  73. package/src/config/__tests__/fixtures/load-redocly.yaml +2 -0
  74. package/src/config/__tests__/fixtures/plugin-config.yaml +2 -0
  75. package/src/config/__tests__/fixtures/plugin.js +56 -0
  76. package/src/config/__tests__/fixtures/resolve-config/api/nested-config.yaml +11 -0
  77. package/src/config/__tests__/fixtures/resolve-config/api/plugin.js +69 -0
  78. package/src/config/__tests__/fixtures/resolve-config/local-config-with-circular.yaml +7 -0
  79. package/src/config/__tests__/fixtures/resolve-config/local-config-with-custom-function.yaml +17 -0
  80. package/src/config/__tests__/fixtures/resolve-config/local-config-with-file.yaml +18 -0
  81. package/src/config/__tests__/fixtures/resolve-config/local-config-with-wrong-custom-function.yaml +15 -0
  82. package/src/config/__tests__/fixtures/resolve-config/local-config.yaml +9 -0
  83. package/src/config/__tests__/fixtures/resolve-config/plugin.js +80 -0
  84. package/src/config/__tests__/fixtures/resolve-remote-configs/nested-remote-config.yaml +3 -0
  85. package/src/config/__tests__/fixtures/resolve-remote-configs/remote-config.yaml +4 -0
  86. package/src/config/__tests__/load.test.ts +167 -0
  87. package/src/config/__tests__/resolve-plugins.test.ts +27 -0
  88. package/src/config/__tests__/utils.test.ts +204 -0
  89. package/src/config/all.ts +74 -0
  90. package/src/config/builtIn.ts +37 -0
  91. package/src/config/config-resolvers.ts +474 -0
  92. package/src/config/config.ts +332 -0
  93. package/src/config/index.ts +7 -0
  94. package/src/config/load.ts +144 -0
  95. package/src/config/minimal.ts +61 -0
  96. package/src/config/recommended.ts +61 -0
  97. package/src/config/rules.ts +54 -0
  98. package/src/config/types.ts +231 -0
  99. package/src/config/utils.ts +349 -0
  100. package/src/decorators/__tests__/filter-in.test.ts +310 -0
  101. package/src/decorators/__tests__/filter-out.test.ts +335 -0
  102. package/src/decorators/__tests__/media-type-examples-override.test.ts +665 -0
  103. package/src/decorators/__tests__/remove-x-internal.test.ts +316 -0
  104. package/src/decorators/__tests__/resources/request.yaml +3 -0
  105. package/src/decorators/__tests__/resources/response.yaml +3 -0
  106. package/src/decorators/common/filters/filter-helper.ts +72 -0
  107. package/src/decorators/common/filters/filter-in.ts +18 -0
  108. package/src/decorators/common/filters/filter-out.ts +18 -0
  109. package/src/decorators/common/info-description-override.ts +24 -0
  110. package/src/decorators/common/info-override.ts +15 -0
  111. package/src/decorators/common/media-type-examples-override.ts +79 -0
  112. package/src/decorators/common/operation-description-override.ts +30 -0
  113. package/src/decorators/common/registry-dependencies.ts +25 -0
  114. package/src/decorators/common/remove-x-internal.ts +59 -0
  115. package/src/decorators/common/tag-description-override.ts +25 -0
  116. package/src/decorators/oas2/index.ts +20 -0
  117. package/src/decorators/oas3/index.ts +22 -0
  118. package/src/env.ts +5 -0
  119. package/src/format/codeframes.ts +216 -0
  120. package/src/format/format.ts +375 -0
  121. package/src/index.ts +71 -0
  122. package/src/js-yaml/index.ts +14 -0
  123. package/src/lint.ts +148 -0
  124. package/src/logger.ts +34 -0
  125. package/src/oas-types.ts +57 -0
  126. package/src/output.ts +7 -0
  127. package/src/redocly/__tests__/redocly-client.test.ts +146 -0
  128. package/src/redocly/index.ts +187 -0
  129. package/src/redocly/redocly-client-types.ts +10 -0
  130. package/src/redocly/registry-api-types.ts +32 -0
  131. package/src/redocly/registry-api.ts +150 -0
  132. package/src/ref-utils.ts +85 -0
  133. package/src/resolve.ts +417 -0
  134. package/src/rules/__tests__/fixtures/code-sample.php +9 -0
  135. package/src/rules/__tests__/fixtures/invalid-yaml.yaml +1 -0
  136. package/src/rules/__tests__/fixtures/ref.yaml +1 -0
  137. package/src/rules/__tests__/no-unresolved-refs.test.ts +257 -0
  138. package/src/rules/__tests__/utils.test.ts +160 -0
  139. package/src/rules/ajv.ts +102 -0
  140. package/src/rules/common/__tests__/info-license.test.ts +62 -0
  141. package/src/rules/common/__tests__/license-url.test.ts +63 -0
  142. package/src/rules/common/__tests__/no-ambiguous-paths.test.ts +96 -0
  143. package/src/rules/common/__tests__/no-enum-type-mismatch.test.ts +210 -0
  144. package/src/rules/common/__tests__/no-identical-paths.test.ts +58 -0
  145. package/src/rules/common/__tests__/no-path-trailing-slash.test.ts +85 -0
  146. package/src/rules/common/__tests__/operation-2xx-response.test.ts +192 -0
  147. package/src/rules/common/__tests__/operation-4xx-response.test.ts +231 -0
  148. package/src/rules/common/__tests__/operation-operationId-unique.test.ts +76 -0
  149. package/src/rules/common/__tests__/operation-operationId-url-safe.test.ts +45 -0
  150. package/src/rules/common/__tests__/operation-parameters-unique.test.ts +167 -0
  151. package/src/rules/common/__tests__/operation-singular-tag.test.ts +72 -0
  152. package/src/rules/common/__tests__/path-http-verbs-order.test.ts +95 -0
  153. package/src/rules/common/__tests__/path-not-include-query.test.ts +64 -0
  154. package/src/rules/common/__tests__/path-params-defined.test.ts +202 -0
  155. package/src/rules/common/__tests__/paths-kebab-case.test.ts +108 -0
  156. package/src/rules/common/__tests__/scalar-property-missing-example.test.ts +264 -0
  157. package/src/rules/common/__tests__/security-defined.test.ts +175 -0
  158. package/src/rules/common/__tests__/spec-strict-refs.test.ts +69 -0
  159. package/src/rules/common/__tests__/spec.test.ts +610 -0
  160. package/src/rules/common/__tests__/tag-description.test.ts +65 -0
  161. package/src/rules/common/__tests__/tags-alphabetical.test.ts +64 -0
  162. package/src/rules/common/assertions/__tests__/asserts.test.ts +869 -0
  163. package/src/rules/common/assertions/__tests__/index.test.ts +100 -0
  164. package/src/rules/common/assertions/__tests__/utils.test.ts +236 -0
  165. package/src/rules/common/assertions/asserts.ts +357 -0
  166. package/src/rules/common/assertions/index.ts +53 -0
  167. package/src/rules/common/assertions/utils.ts +331 -0
  168. package/src/rules/common/info-contact.ts +15 -0
  169. package/src/rules/common/info-license-url.ts +10 -0
  170. package/src/rules/common/info-license.ts +15 -0
  171. package/src/rules/common/no-ambiguous-paths.ts +50 -0
  172. package/src/rules/common/no-enum-type-mismatch.ts +52 -0
  173. package/src/rules/common/no-http-verbs-in-paths.ts +36 -0
  174. package/src/rules/common/no-identical-paths.ts +24 -0
  175. package/src/rules/common/no-invalid-parameter-examples.ts +36 -0
  176. package/src/rules/common/no-invalid-schema-examples.ts +27 -0
  177. package/src/rules/common/no-path-trailing-slash.ts +15 -0
  178. package/src/rules/common/operation-2xx-response.ts +24 -0
  179. package/src/rules/common/operation-4xx-response.ts +24 -0
  180. package/src/rules/common/operation-description.ts +13 -0
  181. package/src/rules/common/operation-operationId-unique.ts +21 -0
  182. package/src/rules/common/operation-operationId-url-safe.ts +19 -0
  183. package/src/rules/common/operation-operationId.ts +17 -0
  184. package/src/rules/common/operation-parameters-unique.ts +48 -0
  185. package/src/rules/common/operation-singular-tag.ts +17 -0
  186. package/src/rules/common/operation-summary.ts +13 -0
  187. package/src/rules/common/operation-tag-defined.ts +26 -0
  188. package/src/rules/common/parameter-description.ts +22 -0
  189. package/src/rules/common/path-declaration-must-exist.ts +15 -0
  190. package/src/rules/common/path-excludes-patterns.ts +23 -0
  191. package/src/rules/common/path-http-verbs-order.ts +30 -0
  192. package/src/rules/common/path-not-include-query.ts +17 -0
  193. package/src/rules/common/path-params-defined.ts +65 -0
  194. package/src/rules/common/path-segment-plural.ts +31 -0
  195. package/src/rules/common/paths-kebab-case.ts +19 -0
  196. package/src/rules/common/required-string-property-missing-min-length.ts +44 -0
  197. package/src/rules/common/response-contains-header.ts +35 -0
  198. package/src/rules/common/scalar-property-missing-example.ts +58 -0
  199. package/src/rules/common/security-defined.ts +65 -0
  200. package/src/rules/common/spec-strict-refs.ts +30 -0
  201. package/src/rules/common/spec.ts +175 -0
  202. package/src/rules/common/tag-description.ts +10 -0
  203. package/src/rules/common/tags-alphabetical.ts +20 -0
  204. package/src/rules/no-unresolved-refs.ts +51 -0
  205. package/src/rules/oas2/__tests__/boolean-parameter-prefixes.test.ts +110 -0
  206. package/src/rules/oas2/__tests__/response-contains-header.test.ts +174 -0
  207. package/src/rules/oas2/__tests__/response-contains-property.test.ts +155 -0
  208. package/src/rules/oas2/__tests__/spec/fixtures/description.md +1 -0
  209. package/src/rules/oas2/__tests__/spec/info.test.ts +355 -0
  210. package/src/rules/oas2/__tests__/spec/operation.test.ts +123 -0
  211. package/src/rules/oas2/__tests__/spec/paths.test.ts +245 -0
  212. package/src/rules/oas2/__tests__/spec/referenceableScalars.test.ts +35 -0
  213. package/src/rules/oas2/__tests__/spec/utils.ts +32 -0
  214. package/src/rules/oas2/boolean-parameter-prefixes.ts +26 -0
  215. package/src/rules/oas2/index.ts +91 -0
  216. package/src/rules/oas2/remove-unused-components.ts +81 -0
  217. package/src/rules/oas2/request-mime-type.ts +16 -0
  218. package/src/rules/oas2/response-contains-property.ts +36 -0
  219. package/src/rules/oas2/response-mime-type.ts +16 -0
  220. package/src/rules/oas3/__tests__/boolean-parameter-prefixes.test.ts +111 -0
  221. package/src/rules/oas3/__tests__/component-name-unique.test.ts +823 -0
  222. package/src/rules/oas3/__tests__/fixtures/common.yaml +11 -0
  223. package/src/rules/oas3/__tests__/no-empty-enum-servers.com.test.ts +205 -0
  224. package/src/rules/oas3/__tests__/no-example-value-and-externalValue.test.ts +65 -0
  225. package/src/rules/oas3/__tests__/no-invalid-media-type-examples.test.ts +473 -0
  226. package/src/rules/oas3/__tests__/no-server-example.com.test.ts +60 -0
  227. package/src/rules/oas3/__tests__/no-server-trailing-slash.test.ts +79 -0
  228. package/src/rules/oas3/__tests__/no-unused-components.test.ts +131 -0
  229. package/src/rules/oas3/__tests__/operation-4xx-problem-details-rfc7807.test.ts +145 -0
  230. package/src/rules/oas3/__tests__/response-contains-header.test.ts +389 -0
  231. package/src/rules/oas3/__tests__/response-contains-property.test.ts +403 -0
  232. package/src/rules/oas3/__tests__/spec/callbacks.test.ts +41 -0
  233. package/src/rules/oas3/__tests__/spec/fixtures/description.md +1 -0
  234. package/src/rules/oas3/__tests__/spec/info.test.ts +391 -0
  235. package/src/rules/oas3/__tests__/spec/operation.test.ts +253 -0
  236. package/src/rules/oas3/__tests__/spec/paths.test.ts +284 -0
  237. package/src/rules/oas3/__tests__/spec/referenceableScalars.test.ts +77 -0
  238. package/src/rules/oas3/__tests__/spec/servers.test.ts +505 -0
  239. package/src/rules/oas3/__tests__/spec/spec.test.ts +298 -0
  240. package/src/rules/oas3/__tests__/spec/utils.ts +32 -0
  241. package/src/rules/oas3/__tests__/spec-components-invalid-map-name.test.ts +276 -0
  242. package/src/rules/oas3/__tests__/utils/lint-document-for-test.ts +23 -0
  243. package/src/rules/oas3/boolean-parameter-prefixes.ts +28 -0
  244. package/src/rules/oas3/component-name-unique.ts +158 -0
  245. package/src/rules/oas3/index.ts +113 -0
  246. package/src/rules/oas3/no-empty-servers.ts +22 -0
  247. package/src/rules/oas3/no-example-value-and-externalValue.ts +14 -0
  248. package/src/rules/oas3/no-invalid-media-type-examples.ts +49 -0
  249. package/src/rules/oas3/no-server-example.com.ts +14 -0
  250. package/src/rules/oas3/no-server-trailing-slash.ts +15 -0
  251. package/src/rules/oas3/no-server-variables-empty-enum.ts +66 -0
  252. package/src/rules/oas3/no-undefined-server-variable.ts +30 -0
  253. package/src/rules/oas3/no-unused-components.ts +75 -0
  254. package/src/rules/oas3/operation-4xx-problem-details-rfc7807.ts +35 -0
  255. package/src/rules/oas3/remove-unused-components.ts +95 -0
  256. package/src/rules/oas3/request-mime-type.ts +30 -0
  257. package/src/rules/oas3/response-contains-property.ts +38 -0
  258. package/src/rules/oas3/response-mime-type.ts +30 -0
  259. package/src/rules/oas3/spec-components-invalid-map-name.ts +69 -0
  260. package/src/rules/other/stats.ts +73 -0
  261. package/src/rules/utils.ts +193 -0
  262. package/src/types/config-external-schemas.ts +917 -0
  263. package/src/types/index.ts +149 -0
  264. package/src/types/oas2.ts +478 -0
  265. package/src/types/oas3.ts +597 -0
  266. package/src/types/oas3_1.ts +258 -0
  267. package/src/types/redocly-yaml.ts +1040 -0
  268. package/src/typings/common.ts +17 -0
  269. package/src/typings/openapi.ts +298 -0
  270. package/src/typings/swagger.ts +236 -0
  271. package/src/utils.ts +276 -0
  272. package/src/visitors.ts +491 -0
  273. package/src/walk.ts +439 -0
  274. package/tsconfig.json +8 -0
  275. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,20 @@
1
+ import { Oas2Decorator } from '../../visitors';
2
+ import { RegistryDependencies } from '../common/registry-dependencies';
3
+ import { OperationDescriptionOverride } from '../common/operation-description-override';
4
+ import { TagDescriptionOverride } from '../common/tag-description-override';
5
+ import { InfoDescriptionOverride } from '../common/info-description-override';
6
+ import { InfoOverride } from '../common/info-override';
7
+ import { RemoveXInternal } from '../common/remove-x-internal';
8
+ import { FilterIn } from '../common/filters/filter-in';
9
+ import { FilterOut } from '../common/filters/filter-out';
10
+
11
+ export const decorators = {
12
+ 'registry-dependencies': RegistryDependencies as Oas2Decorator,
13
+ 'operation-description-override': OperationDescriptionOverride as Oas2Decorator,
14
+ 'tag-description-override': TagDescriptionOverride as Oas2Decorator,
15
+ 'info-description-override': InfoDescriptionOverride as Oas2Decorator,
16
+ 'info-override': InfoOverride as Oas2Decorator,
17
+ 'remove-x-internal': RemoveXInternal as Oas2Decorator,
18
+ 'filter-in': FilterIn as Oas2Decorator,
19
+ 'filter-out': FilterOut as Oas2Decorator,
20
+ };
@@ -0,0 +1,22 @@
1
+ import { Oas3Decorator } from '../../visitors';
2
+ import { RegistryDependencies } from '../common/registry-dependencies';
3
+ import { OperationDescriptionOverride } from '../common/operation-description-override';
4
+ import { TagDescriptionOverride } from '../common/tag-description-override';
5
+ import { InfoDescriptionOverride } from '../common/info-description-override';
6
+ import { InfoOverride } from '../common/info-override';
7
+ import { RemoveXInternal } from '../common/remove-x-internal';
8
+ import { FilterIn } from '../common/filters/filter-in';
9
+ import { FilterOut } from '../common/filters/filter-out';
10
+ import { MediaTypeExamplesOverride } from '../common/media-type-examples-override';
11
+
12
+ export const decorators = {
13
+ 'registry-dependencies': RegistryDependencies as Oas3Decorator,
14
+ 'operation-description-override': OperationDescriptionOverride as Oas3Decorator,
15
+ 'tag-description-override': TagDescriptionOverride as Oas3Decorator,
16
+ 'info-description-override': InfoDescriptionOverride as Oas3Decorator,
17
+ 'info-override': InfoOverride as Oas3Decorator,
18
+ 'remove-x-internal': RemoveXInternal as Oas3Decorator,
19
+ 'filter-in': FilterIn as Oas3Decorator,
20
+ 'filter-out': FilterOut as Oas3Decorator,
21
+ 'media-type-examples-override': MediaTypeExamplesOverride as Oas3Decorator,
22
+ };
package/src/env.ts ADDED
@@ -0,0 +1,5 @@
1
+ export const isBrowser =
2
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
+ // @ts-ignore
4
+ typeof window !== 'undefined' || typeof self !== 'undefined' || typeof process === 'undefined'; // main and worker thread
5
+ export const env = isBrowser ? {} : process.env || {};
@@ -0,0 +1,216 @@
1
+ import * as yamlAst from 'yaml-ast-parser';
2
+ import { unescapePointer } from '../ref-utils';
3
+ import { LineColLocationObject, Loc, LocationObject } from '../walk';
4
+ import { colorize, colorOptions } from '../logger';
5
+
6
+ type YAMLMapping = yamlAst.YAMLMapping & { kind: yamlAst.Kind.MAPPING };
7
+ type YAMLMap = yamlAst.YamlMap & { kind: yamlAst.Kind.MAP };
8
+ type YAMLAnchorReference = yamlAst.YAMLAnchorReference & { kind: yamlAst.Kind.ANCHOR_REF };
9
+ type YAMLSequence = yamlAst.YAMLSequence & { kind: yamlAst.Kind.SEQ };
10
+ type YAMLScalar = yamlAst.YAMLScalar & { kind: yamlAst.Kind.SCALAR };
11
+ type YAMLNode = YAMLMapping | YAMLMap | YAMLAnchorReference | YAMLSequence | YAMLScalar;
12
+
13
+ const MAX_LINE_LENGTH = 150;
14
+ const MAX_CODEFRAME_LINES = 3;
15
+
16
+ // TODO: temporary
17
+ function parsePointer(pointer: string) {
18
+ return pointer.substr(2).split('/').map(unescapePointer);
19
+ }
20
+
21
+ export function getCodeframe(location: LineColLocationObject, color: boolean) {
22
+ colorOptions.enabled = color;
23
+ const { start, end = { line: start.line, col: start.col + 1 }, source } = location;
24
+ const lines = source.getLines();
25
+ const startLineNum = start.line;
26
+ const endLineNum = Math.max(Math.min(end.line, lines.length), start.line);
27
+ let skipLines = Math.max(endLineNum - startLineNum - MAX_CODEFRAME_LINES + 1, 0);
28
+ if (skipLines < 2) skipLines = 0; // do not skip one line
29
+
30
+ // Lines specified like this: ["prefix", "string"],
31
+ const prefixedLines: [string, string][] = [];
32
+
33
+ let currentPad = 0;
34
+
35
+ for (let i = startLineNum; i <= endLineNum; i++) {
36
+ if (skipLines > 0 && i >= endLineNum - skipLines) break;
37
+ const line = lines[i - 1] || '';
38
+ if (line !== '') currentPad = padSize(line);
39
+ const startIdx = i === startLineNum ? start.col - 1 : currentPad;
40
+ const endIdx = i === endLineNum ? end.col - 1 : line.length;
41
+
42
+ prefixedLines.push([`${i}`, markLine(line, startIdx, endIdx, colorize.red)]);
43
+ if (!color) prefixedLines.push(['', underlineLine(line, startIdx, endIdx)]);
44
+ }
45
+
46
+ if (skipLines > 0) {
47
+ prefixedLines.push([
48
+ `…`,
49
+ `${whitespace(currentPad)}${colorize.gray(`< ${skipLines} more lines >`)}`,
50
+ ]);
51
+ // print last line
52
+ prefixedLines.push([
53
+ `${endLineNum}`,
54
+ markLine(lines[endLineNum - 1], -1, end.col - 1, colorize.red),
55
+ ]);
56
+
57
+ if (!color) prefixedLines.push(['', underlineLine(lines[endLineNum - 1], -1, end.col - 1)]);
58
+ }
59
+
60
+ return printPrefixedLines([
61
+ [`${startLineNum - 2}`, markLine(lines[startLineNum - 1 - 2])],
62
+ [`${startLineNum - 1}`, markLine(lines[startLineNum - 1 - 1])],
63
+ ...prefixedLines,
64
+ [`${endLineNum + 1}`, markLine(lines[endLineNum - 1 + 1])],
65
+ [`${endLineNum + 2}`, markLine(lines[endLineNum - 1 + 2])],
66
+ ]);
67
+
68
+ function markLine(
69
+ line: string,
70
+ startIdx: number = -1,
71
+ endIdx: number = +Infinity,
72
+ variant = colorize.gray
73
+ ) {
74
+ if (!color) return line;
75
+ if (!line) return line;
76
+
77
+ if (startIdx === -1) {
78
+ startIdx = padSize(line);
79
+ }
80
+
81
+ endIdx = Math.min(endIdx, line.length);
82
+ return (
83
+ line.substr(0, startIdx) + variant(line.substring(startIdx, endIdx)) + line.substr(endIdx)
84
+ );
85
+ }
86
+ }
87
+
88
+ function printPrefixedLines(lines: [string, string][]): string {
89
+ const existingLines = lines.filter(([_, line]) => line !== undefined);
90
+
91
+ const padLen = Math.max(...existingLines.map(([prefix]) => prefix.length));
92
+ const dedentLen = Math.min(
93
+ ...existingLines.map(([_, line]) => (line === '' ? Infinity : padSize(line)))
94
+ );
95
+
96
+ return existingLines
97
+ .map(
98
+ ([prefix, line]) =>
99
+ colorize.gray(leftPad(padLen, prefix) + ' |') +
100
+ (line ? ' ' + limitLineLength(line.substring(dedentLen)) : '')
101
+ )
102
+ .join('\n');
103
+ }
104
+
105
+ function limitLineLength(line: string, maxLen: number = MAX_LINE_LENGTH) {
106
+ const overflowLen = line.length - maxLen;
107
+ if (overflowLen > 0) {
108
+ const charsMoreText = colorize.gray(`...<${overflowLen} chars>`);
109
+ return line.substring(0, maxLen - charsMoreText.length) + charsMoreText;
110
+ } else {
111
+ return line;
112
+ }
113
+ }
114
+
115
+ function underlineLine(line: string, startIdx: number = -1, endIdx: number = +Infinity) {
116
+ if (startIdx === -1) {
117
+ startIdx = padSize(line);
118
+ }
119
+
120
+ endIdx = Math.min(endIdx, line.length);
121
+ return whitespace(startIdx) + '^'.repeat(Math.max(endIdx - startIdx, 1));
122
+ }
123
+
124
+ function whitespace(len: number): string {
125
+ return ' '.repeat(len);
126
+ }
127
+
128
+ function leftPad(len: number, str: string): string {
129
+ return whitespace(len - str.length) + str;
130
+ }
131
+
132
+ function padSize(line: string): number {
133
+ for (let i = 0; i < line.length; i++) {
134
+ if (line[i] !== ' ') return i;
135
+ }
136
+ return line.length;
137
+ }
138
+
139
+ export function getLineColLocation(location: LocationObject): LineColLocationObject {
140
+ if (location.pointer === undefined) return location;
141
+
142
+ const { source, pointer, reportOnKey } = location;
143
+ const ast = source.getAst(yamlAst.safeLoad) as YAMLNode;
144
+ const astNode = getAstNodeByPointer(ast, pointer, !!reportOnKey);
145
+ return {
146
+ ...location,
147
+ pointer: undefined,
148
+ ...positionsToLoc(source.body, astNode?.startPosition ?? 1, astNode?.endPosition ?? 1),
149
+ };
150
+ }
151
+
152
+ function positionsToLoc(
153
+ source: string,
154
+ startPos: number,
155
+ endPos: number
156
+ ): { start: Loc; end: Loc } {
157
+ let currentLine = 1;
158
+ let currentCol = 1;
159
+ let start: Loc = { line: 1, col: 1 };
160
+
161
+ for (let i = 0; i < endPos - 1; i++) {
162
+ if (i === startPos - 1) {
163
+ start = { line: currentLine, col: currentCol + 1 };
164
+ }
165
+ if (source[i] === '\n') {
166
+ currentLine++;
167
+ currentCol = 1;
168
+ if (i === startPos - 1) {
169
+ start = { line: currentLine, col: currentCol };
170
+ }
171
+
172
+ if (source[i + 1] === '\r') i++; // TODO: test it
173
+ continue;
174
+ }
175
+ currentCol++;
176
+ }
177
+
178
+ const end = startPos === endPos ? { ...start } : { line: currentLine, col: currentCol + 1 };
179
+ return { start, end };
180
+ }
181
+
182
+ export function getAstNodeByPointer(root: YAMLNode, pointer: string, reportOnKey: boolean) {
183
+ const pointerSegments = parsePointer(pointer);
184
+ if (root === undefined) {
185
+ return undefined;
186
+ }
187
+
188
+ let currentNode = root;
189
+ for (const key of pointerSegments) {
190
+ if (currentNode.kind === yamlAst.Kind.MAP) {
191
+ const mapping = currentNode.mappings.find((m) => m.key.value === key);
192
+ if (!mapping) break;
193
+ currentNode = mapping as YAMLNode;
194
+ if (!mapping?.value) break; // If node has value - return value, if not - return node itself
195
+ currentNode = mapping.value as YAMLNode;
196
+ } else if (currentNode.kind === yamlAst.Kind.SEQ) {
197
+ const elem = currentNode.items[parseInt(key, 10)] as YAMLNode;
198
+ if (!elem) break;
199
+ currentNode = elem as YAMLNode;
200
+ }
201
+ }
202
+
203
+ if (!reportOnKey) {
204
+ return currentNode;
205
+ } else {
206
+ const parent = currentNode.parent as YAMLNode;
207
+ if (!parent) return currentNode;
208
+ if (parent.kind === yamlAst.Kind.SEQ) {
209
+ return currentNode;
210
+ } else if (parent.kind === yamlAst.Kind.MAPPING) {
211
+ return parent.key;
212
+ } else {
213
+ return currentNode;
214
+ }
215
+ }
216
+ }
@@ -0,0 +1,375 @@
1
+ import * as path from 'path';
2
+ import { colorOptions, colorize, logger } from '../logger';
3
+ import { output } from '../output';
4
+
5
+ const coreVersion = require('../../package.json').version;
6
+
7
+ import { NormalizedProblem, ProblemSeverity, LineColLocationObject, LocationObject } from '../walk';
8
+ import { getCodeframe, getLineColLocation } from './codeframes';
9
+ import { env } from '../env';
10
+ import { isAbsoluteUrl } from '../ref-utils';
11
+
12
+ export type Totals = {
13
+ errors: number;
14
+ warnings: number;
15
+ ignored: number;
16
+ };
17
+
18
+ const ERROR_MESSAGE = {
19
+ INVALID_SEVERITY_LEVEL: 'Invalid severity level; accepted values: error or warn',
20
+ };
21
+
22
+ const BG_COLORS = {
23
+ warn: (str: string) => colorize.bgYellow(colorize.black(str)),
24
+ error: colorize.bgRed,
25
+ };
26
+
27
+ const COLORS = {
28
+ warn: colorize.yellow,
29
+ error: colorize.red,
30
+ };
31
+
32
+ const SEVERITY_NAMES = {
33
+ warn: 'Warning',
34
+ error: 'Error',
35
+ };
36
+
37
+ const CODECLIMATE_SEVERITY_MAPPING = {
38
+ error: 'critical',
39
+ warn: 'minor',
40
+ };
41
+
42
+ const MAX_SUGGEST = 5;
43
+
44
+ function severityToNumber(severity: ProblemSeverity) {
45
+ return severity === 'error' ? 1 : 2;
46
+ }
47
+
48
+ export type OutputFormat =
49
+ | 'codeframe'
50
+ | 'stylish'
51
+ | 'json'
52
+ | 'checkstyle'
53
+ | 'codeclimate'
54
+ | 'summary';
55
+
56
+ export function getTotals(problems: (NormalizedProblem & { ignored?: boolean })[]): Totals {
57
+ let errors = 0;
58
+ let warnings = 0;
59
+ let ignored = 0;
60
+
61
+ for (const m of problems) {
62
+ if (m.ignored) {
63
+ ignored++;
64
+ continue;
65
+ }
66
+ if (m.severity === 'error') errors++;
67
+ if (m.severity === 'warn') warnings++;
68
+ }
69
+
70
+ return {
71
+ errors,
72
+ warnings,
73
+ ignored,
74
+ };
75
+ }
76
+
77
+ export function formatProblems(
78
+ problems: (NormalizedProblem & { ignored?: boolean })[],
79
+ opts: {
80
+ maxProblems?: number;
81
+ cwd?: string;
82
+ format?: OutputFormat;
83
+ color?: boolean;
84
+ totals: Totals;
85
+ version: string;
86
+ }
87
+ ) {
88
+ const {
89
+ maxProblems = 100,
90
+ cwd = process.cwd(),
91
+ format = 'codeframe',
92
+ color = colorOptions.enabled,
93
+ totals = getTotals(problems),
94
+ version = coreVersion,
95
+ } = opts;
96
+
97
+ colorOptions.enabled = color; // force colors if specified
98
+
99
+ const totalProblems = problems.length;
100
+ problems = problems.filter((m) => !m.ignored);
101
+ const ignoredProblems = totalProblems - problems.length;
102
+
103
+ problems = problems
104
+ .sort((a, b) => severityToNumber(a.severity) - severityToNumber(b.severity))
105
+ .slice(0, maxProblems);
106
+
107
+ if (!totalProblems && format !== 'json') return;
108
+
109
+ switch (format) {
110
+ case 'json':
111
+ outputJSON();
112
+ break;
113
+ case 'codeframe':
114
+ for (let i = 0; i < problems.length; i++) {
115
+ const problem = problems[i];
116
+ logger.info(`${formatCodeframe(problem, i)}\n`);
117
+ }
118
+ break;
119
+ case 'stylish': {
120
+ const groupedByFile = groupByFiles(problems);
121
+ for (const [file, { ruleIdPad, locationPad: positionPad, fileProblems }] of Object.entries(
122
+ groupedByFile
123
+ )) {
124
+ logger.info(`${colorize.blue(isAbsoluteUrl(file) ? file : path.relative(cwd, file))}:\n`);
125
+
126
+ for (let i = 0; i < fileProblems.length; i++) {
127
+ const problem = fileProblems[i];
128
+ logger.info(`${formatStylish(problem, positionPad, ruleIdPad)}\n`);
129
+ }
130
+
131
+ logger.info('\n');
132
+ }
133
+ break;
134
+ }
135
+ case 'checkstyle': {
136
+ const groupedByFile = groupByFiles(problems);
137
+
138
+ output.write('<?xml version="1.0" encoding="UTF-8"?>\n');
139
+ output.write('<checkstyle version="4.3">\n');
140
+
141
+ for (const [file, { fileProblems }] of Object.entries(groupedByFile)) {
142
+ output.write(
143
+ `<file name="${xmlEscape(isAbsoluteUrl(file) ? file : path.relative(cwd, file))}">\n`
144
+ );
145
+ fileProblems.forEach(formatCheckstyle);
146
+ output.write(`</file>\n`);
147
+ }
148
+
149
+ output.write(`</checkstyle>\n`);
150
+ break;
151
+ }
152
+ case 'codeclimate':
153
+ outputForCodeClimate();
154
+ break;
155
+ case 'summary':
156
+ formatSummary(problems);
157
+ break;
158
+ }
159
+
160
+ if (totalProblems - ignoredProblems > maxProblems) {
161
+ logger.info(
162
+ `< ... ${totalProblems - maxProblems} more problems hidden > ${colorize.gray(
163
+ 'increase with `--max-problems N`'
164
+ )}\n`
165
+ );
166
+ }
167
+
168
+ function outputForCodeClimate() {
169
+ const issues = problems.map((p) => {
170
+ const location = p.location[0]; // TODO: support multiple location
171
+ const lineCol = getLineColLocation(location);
172
+ return {
173
+ description: p.message,
174
+ location: {
175
+ path: isAbsoluteUrl(location.source.absoluteRef)
176
+ ? location.source.absoluteRef
177
+ : path.relative(cwd, location.source.absoluteRef),
178
+ lines: {
179
+ begin: lineCol.start.line,
180
+ },
181
+ },
182
+ severity: CODECLIMATE_SEVERITY_MAPPING[p.severity],
183
+ fingerprint: `${p.ruleId}${p.location.length > 0 ? '-' + p.location[0].pointer : ''}`,
184
+ };
185
+ });
186
+ output.write(JSON.stringify(issues, null, 2));
187
+ }
188
+
189
+ function outputJSON() {
190
+ const resultObject = {
191
+ totals,
192
+ version,
193
+ problems: problems.map((p) => {
194
+ const problem = {
195
+ ...p,
196
+ location: p.location.map((location: any) => ({
197
+ ...location,
198
+ source: {
199
+ ref: isAbsoluteUrl(location.source.absoluteRef)
200
+ ? location.source.absoluteRef
201
+ : path.relative(cwd, location.source.absoluteRef),
202
+ },
203
+ })),
204
+ from: p.from
205
+ ? {
206
+ ...p.from,
207
+ source: {
208
+ ref: isAbsoluteUrl(p.from?.source.absoluteRef)
209
+ ? p.from?.source.absoluteRef
210
+ : path.relative(cwd, p.from?.source.absoluteRef || cwd),
211
+ },
212
+ }
213
+ : undefined,
214
+ };
215
+
216
+ if (env.FORMAT_JSON_WITH_CODEFRAMES) {
217
+ const location = p.location[0]; // TODO: support multiple locations
218
+ const loc = getLineColLocation(location);
219
+ (problem as any).codeframe = getCodeframe(loc, color);
220
+ }
221
+ return problem;
222
+ }),
223
+ };
224
+ output.write(JSON.stringify(resultObject, null, 2));
225
+ }
226
+
227
+ function getBgColor(problem: NormalizedProblem) {
228
+ const { severity } = problem;
229
+ if (!BG_COLORS[severity]) {
230
+ throw new Error(ERROR_MESSAGE.INVALID_SEVERITY_LEVEL);
231
+ }
232
+ return BG_COLORS[severity];
233
+ }
234
+
235
+ function formatCodeframe(problem: NormalizedProblem, idx: number) {
236
+ const bgColor = getBgColor(problem);
237
+ const location = problem.location[0]; // TODO: support multiple locations
238
+ const relativePath = isAbsoluteUrl(location.source.absoluteRef)
239
+ ? location.source.absoluteRef
240
+ : path.relative(cwd, location.source.absoluteRef);
241
+ const loc = getLineColLocation(location);
242
+ const atPointer = location.pointer ? colorize.gray(`at ${location.pointer}`) : '';
243
+ const fileWithLoc = `${relativePath}:${loc.start.line}:${loc.start.col}`;
244
+ return (
245
+ `[${idx + 1}] ${bgColor(fileWithLoc)} ${atPointer}\n\n` +
246
+ `${problem.message}\n\n` +
247
+ formatDidYouMean(problem) +
248
+ getCodeframe(loc, color) +
249
+ '\n\n' +
250
+ formatFrom(cwd, problem.from) +
251
+ `${SEVERITY_NAMES[problem.severity]} was generated by the ${colorize.blue(
252
+ problem.ruleId
253
+ )} rule.\n\n`
254
+ );
255
+ }
256
+
257
+ function formatStylish(problem: OnlyLineColProblem, locationPad: number, ruleIdPad: number) {
258
+ const color = COLORS[problem.severity];
259
+ if (!SEVERITY_NAMES[problem.severity]) {
260
+ return 'Error not found severity. Please check your config file. Allowed values: `warn,error,off`';
261
+ }
262
+ const severityName = color(SEVERITY_NAMES[problem.severity].toLowerCase().padEnd(7));
263
+ const { start } = problem.location[0];
264
+ return ` ${`${start.line}:${start.col}`.padEnd(
265
+ locationPad
266
+ )} ${severityName} ${problem.ruleId.padEnd(ruleIdPad)} ${problem.message}`;
267
+ }
268
+
269
+ function formatCheckstyle(problem: OnlyLineColProblem) {
270
+ const { line, col } = problem.location[0].start;
271
+ const severity = problem.severity == 'warn' ? 'warning' : 'error';
272
+ const message = xmlEscape(problem.message);
273
+ const source = xmlEscape(problem.ruleId);
274
+ output.write(
275
+ `<error line="${line}" column="${col}" severity="${severity}" message="${message}" source="${source}" />\n`
276
+ );
277
+ }
278
+ }
279
+
280
+ function formatSummary(problems: NormalizedProblem[]): void {
281
+ const counts: Record<string, { count: number; severity: ProblemSeverity }> = {};
282
+ for (const problem of problems) {
283
+ counts[problem.ruleId] = counts[problem.ruleId] || { count: 0, severity: problem.severity };
284
+ counts[problem.ruleId].count++;
285
+ }
286
+ const sorted = Object.entries(counts).sort(([, a], [, b]) => {
287
+ const severityDiff = severityToNumber(a.severity) - severityToNumber(b.severity);
288
+ return severityDiff || b.count - a.count;
289
+ });
290
+
291
+ for (const [ruleId, info] of sorted) {
292
+ const color = COLORS[info.severity];
293
+ const severityName = color(SEVERITY_NAMES[info.severity].toLowerCase().padEnd(7));
294
+ logger.info(`${severityName} ${ruleId}: ${info.count}\n`);
295
+ }
296
+
297
+ logger.info('\n');
298
+ }
299
+
300
+ function formatFrom(cwd: string, location?: LocationObject) {
301
+ if (!location) return '';
302
+ const relativePath = path.relative(cwd, location.source.absoluteRef);
303
+ const loc = getLineColLocation(location);
304
+ const fileWithLoc = `${relativePath}:${loc.start.line}:${loc.start.col}`;
305
+ const atPointer = location.pointer ? colorize.gray(`at ${location.pointer}`) : '';
306
+
307
+ return `referenced from ${colorize.blue(fileWithLoc)} ${atPointer} \n\n`;
308
+ }
309
+
310
+ function formatDidYouMean(problem: NormalizedProblem) {
311
+ if (problem.suggest.length === 0) return '';
312
+
313
+ if (problem.suggest.length === 1) {
314
+ return `Did you mean: ${problem.suggest[0]} ?\n\n`;
315
+ } else {
316
+ return `Did you mean:\n - ${problem.suggest.slice(0, MAX_SUGGEST).join('\n - ')}\n\n`;
317
+ }
318
+ }
319
+
320
+ type OnlyLineColProblem = Omit<NormalizedProblem, 'location'> & {
321
+ location: LineColLocationObject[];
322
+ };
323
+
324
+ const groupByFiles = (problems: NormalizedProblem[]) => {
325
+ const fileGroups: Record<
326
+ string,
327
+ {
328
+ locationPad: number;
329
+ ruleIdPad: number;
330
+ fileProblems: OnlyLineColProblem[];
331
+ }
332
+ > = {};
333
+ for (const problem of problems) {
334
+ const absoluteRef = problem.location[0].source.absoluteRef; // TODO: multiple errors
335
+ fileGroups[absoluteRef] = fileGroups[absoluteRef] || {
336
+ fileProblems: [],
337
+ ruleIdPad: 0,
338
+ locationPad: 0,
339
+ };
340
+
341
+ const mappedProblem = { ...problem, location: problem.location.map(getLineColLocation) };
342
+ fileGroups[absoluteRef].fileProblems.push(mappedProblem);
343
+ fileGroups[absoluteRef].ruleIdPad = Math.max(
344
+ problem.ruleId.length,
345
+ fileGroups[absoluteRef].ruleIdPad
346
+ );
347
+
348
+ fileGroups[absoluteRef].locationPad = Math.max(
349
+ Math.max(...mappedProblem.location.map((loc) => `${loc.start.line}:${loc.start.col}`.length)),
350
+ fileGroups[absoluteRef].locationPad
351
+ );
352
+ }
353
+
354
+ return fileGroups;
355
+ };
356
+
357
+ function xmlEscape(s: string): string {
358
+ // eslint-disable-next-line no-control-regex
359
+ return s.replace(/[<>&"'\x00-\x1F\x7F\u0080-\uFFFF]/gu, (char) => {
360
+ switch (char) {
361
+ case '<':
362
+ return '&lt;';
363
+ case '>':
364
+ return '&gt;';
365
+ case '&':
366
+ return '&amp;';
367
+ case '"':
368
+ return '&quot;';
369
+ case "'":
370
+ return '&apos;';
371
+ default:
372
+ return `&#${char.charCodeAt(0)};`;
373
+ }
374
+ });
375
+ }