jsonc.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import type { Linter } from 'eslint';
  2. export async function jsonc(): Promise<Linter.FlatConfig[]> {
  3. const [pluginJsonc, parserJsonc] = await Promise.all([
  4. import('eslint-plugin-jsonc'),
  5. import('jsonc-eslint-parser'),
  6. ] as const);
  7. return [
  8. {
  9. files: ['**/*.json', '**/*.json5', '**/*.jsonc', '*.code-workspace'],
  10. languageOptions: {
  11. parser: parserJsonc as any,
  12. },
  13. plugins: {
  14. jsonc: pluginJsonc as any,
  15. },
  16. rules: {
  17. 'jsonc/no-bigint-literals': 'error',
  18. 'jsonc/no-binary-expression': 'error',
  19. 'jsonc/no-binary-numeric-literals': 'error',
  20. 'jsonc/no-dupe-keys': 'error',
  21. 'jsonc/no-escape-sequence-in-identifier': 'error',
  22. 'jsonc/no-floating-decimal': 'error',
  23. 'jsonc/no-hexadecimal-numeric-literals': 'error',
  24. 'jsonc/no-infinity': 'error',
  25. 'jsonc/no-multi-str': 'error',
  26. 'jsonc/no-nan': 'error',
  27. 'jsonc/no-number-props': 'error',
  28. 'jsonc/no-numeric-separators': 'error',
  29. 'jsonc/no-octal': 'error',
  30. 'jsonc/no-octal-escape': 'error',
  31. 'jsonc/no-octal-numeric-literals': 'error',
  32. 'jsonc/no-parenthesized': 'error',
  33. 'jsonc/no-plus-sign': 'error',
  34. 'jsonc/no-regexp-literals': 'error',
  35. 'jsonc/no-sparse-arrays': 'error',
  36. 'jsonc/no-template-literals': 'error',
  37. 'jsonc/no-undefined-value': 'error',
  38. 'jsonc/no-unicode-codepoint-escapes': 'error',
  39. 'jsonc/no-useless-escape': 'error',
  40. 'jsonc/space-unary-ops': 'error',
  41. 'jsonc/valid-json-number': 'error',
  42. 'jsonc/vue-custom-block/no-parsing-error': 'error',
  43. },
  44. },
  45. sortTsconfig(),
  46. sortPackageJson(),
  47. ];
  48. }
  49. function sortPackageJson(): Linter.FlatConfig {
  50. return {
  51. files: ['**/package.json'],
  52. rules: {
  53. 'jsonc/sort-array-values': [
  54. 'error',
  55. {
  56. order: { type: 'asc' },
  57. pathPattern: '^files$|^pnpm.neverBuiltDependencies$',
  58. },
  59. ],
  60. 'jsonc/sort-keys': [
  61. 'error',
  62. {
  63. order: [
  64. 'publisher',
  65. 'name',
  66. 'description',
  67. 'version',
  68. 'author',
  69. 'private',
  70. 'displayName',
  71. 'type',
  72. 'license',
  73. 'funding',
  74. 'homepage',
  75. 'repository',
  76. 'bugs',
  77. 'keywords',
  78. 'categories',
  79. 'scripts',
  80. 'files',
  81. 'sideEffects',
  82. 'bin',
  83. 'main',
  84. 'module',
  85. 'unpkg',
  86. 'jsdelivr',
  87. 'types',
  88. 'typesVersions',
  89. 'imports',
  90. 'exports',
  91. 'publishConfig',
  92. 'icon',
  93. 'activationEvents',
  94. 'contributes',
  95. 'peerDependencies',
  96. 'peerDependenciesMeta',
  97. 'dependencies',
  98. 'optionalDependencies',
  99. 'devDependencies',
  100. 'engines',
  101. 'packageManager',
  102. 'pnpm',
  103. 'overrides',
  104. 'resolutions',
  105. 'husky',
  106. 'simple-git-hooks',
  107. 'lint-staged',
  108. 'eslintConfig',
  109. ],
  110. pathPattern: '^$',
  111. },
  112. {
  113. order: { type: 'asc' },
  114. pathPattern: '^(?:dev|peer|optional|bundled)?[Dd]ependencies(Meta)?$',
  115. },
  116. {
  117. order: { type: 'asc' },
  118. pathPattern: '^(?:resolutions|overrides|pnpm.overrides)$',
  119. },
  120. {
  121. order: ['types', 'import', 'require', 'default'],
  122. pathPattern: '^exports.*$',
  123. },
  124. ],
  125. },
  126. };
  127. }
  128. function sortTsconfig(): Linter.FlatConfig {
  129. return {
  130. files: [
  131. '**/tsconfig.json',
  132. '**/tsconfig.*.json',
  133. 'internal/tsconfig/*.json',
  134. ],
  135. rules: {
  136. 'jsonc/sort-keys': [
  137. 'error',
  138. {
  139. order: [
  140. 'extends',
  141. 'compilerOptions',
  142. 'references',
  143. 'files',
  144. 'include',
  145. 'exclude',
  146. ],
  147. pathPattern: '^$',
  148. },
  149. {
  150. order: [
  151. /* Projects */
  152. 'incremental',
  153. 'composite',
  154. 'tsBuildInfoFile',
  155. 'disableSourceOfProjectReferenceRedirect',
  156. 'disableSolutionSearching',
  157. 'disableReferencedProjectLoad',
  158. /* Language and Environment */
  159. 'target',
  160. 'jsx',
  161. 'jsxFactory',
  162. 'jsxFragmentFactory',
  163. 'jsxImportSource',
  164. 'lib',
  165. 'moduleDetection',
  166. 'noLib',
  167. 'reactNamespace',
  168. 'useDefineForClassFields',
  169. 'emitDecoratorMetadata',
  170. 'experimentalDecorators',
  171. /* Modules */
  172. 'baseUrl',
  173. 'rootDir',
  174. 'rootDirs',
  175. 'customConditions',
  176. 'module',
  177. 'moduleResolution',
  178. 'moduleSuffixes',
  179. 'noResolve',
  180. 'paths',
  181. 'resolveJsonModule',
  182. 'resolvePackageJsonExports',
  183. 'resolvePackageJsonImports',
  184. 'typeRoots',
  185. 'types',
  186. 'allowArbitraryExtensions',
  187. 'allowImportingTsExtensions',
  188. 'allowUmdGlobalAccess',
  189. /* JavaScript Support */
  190. 'allowJs',
  191. 'checkJs',
  192. 'maxNodeModuleJsDepth',
  193. /* Type Checking */
  194. 'strict',
  195. 'strictBindCallApply',
  196. 'strictFunctionTypes',
  197. 'strictNullChecks',
  198. 'strictPropertyInitialization',
  199. 'allowUnreachableCode',
  200. 'allowUnusedLabels',
  201. 'alwaysStrict',
  202. 'exactOptionalPropertyTypes',
  203. 'noFallthroughCasesInSwitch',
  204. 'noImplicitAny',
  205. 'noImplicitOverride',
  206. 'noImplicitReturns',
  207. 'noImplicitThis',
  208. 'noPropertyAccessFromIndexSignature',
  209. 'noUncheckedIndexedAccess',
  210. 'noUnusedLocals',
  211. 'noUnusedParameters',
  212. 'useUnknownInCatchVariables',
  213. /* Emit */
  214. 'declaration',
  215. 'declarationDir',
  216. 'declarationMap',
  217. 'downlevelIteration',
  218. 'emitBOM',
  219. 'emitDeclarationOnly',
  220. 'importHelpers',
  221. 'importsNotUsedAsValues',
  222. 'inlineSourceMap',
  223. 'inlineSources',
  224. 'mapRoot',
  225. 'newLine',
  226. 'noEmit',
  227. 'noEmitHelpers',
  228. 'noEmitOnError',
  229. 'outDir',
  230. 'outFile',
  231. 'preserveConstEnums',
  232. 'preserveValueImports',
  233. 'removeComments',
  234. 'sourceMap',
  235. 'sourceRoot',
  236. 'stripInternal',
  237. /* Interop Constraints */
  238. 'allowSyntheticDefaultImports',
  239. 'esModuleInterop',
  240. 'forceConsistentCasingInFileNames',
  241. 'isolatedModules',
  242. 'preserveSymlinks',
  243. 'verbatimModuleSyntax',
  244. /* Completeness */
  245. 'skipDefaultLibCheck',
  246. 'skipLibCheck',
  247. ],
  248. pathPattern: '^compilerOptions$',
  249. },
  250. ],
  251. },
  252. };
  253. }