eslint.config.cjs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. const fs = require('fs');
  2. const globals = require('globals');
  3. const htmlParser = require('@html-eslint/parser');
  4. const htmlPlugin = require('@html-eslint/eslint-plugin');
  5. const pluginJs = require('@eslint/js');
  6. const pluginReference = require('eslint-plugin-html');
  7. const stylistic = require('@stylistic/eslint-plugin');
  8. if (process && process.env && process.env.npm_command && !fs.existsSync('./platform/web/eslint.config.cjs')) {
  9. throw Error('eslint must be run from the Godot project root folder');
  10. }
  11. const emscriptenGlobals = {
  12. 'Browser': true,
  13. 'ERRNO_CODES': true,
  14. 'FS': true,
  15. 'GL': true,
  16. 'HEAP32': true,
  17. 'HEAP8': true,
  18. 'HEAPF32': true,
  19. 'HEAPU8': true,
  20. 'IDBFS': true,
  21. 'LibraryManager': true,
  22. 'Module': true,
  23. 'UTF8ToString': true,
  24. '_emscripten_webgl_get_current_context': true,
  25. '_free': true,
  26. '_malloc': true,
  27. 'autoAddDeps': true,
  28. 'getValue': true,
  29. 'lengthBytesUTF8': true,
  30. 'mergeInto': true,
  31. 'runtimeKeepalivePop': true,
  32. 'runtimeKeepalivePush': true,
  33. 'setValue': true,
  34. 'stringToUTF8': true,
  35. 'stringToUTF8Array': true,
  36. 'wasmTable': true,
  37. };
  38. module.exports = [
  39. pluginJs.configs.all,
  40. stylistic.configs.customize({ jsx: false }),
  41. {
  42. rules: {
  43. 'consistent-this': ['error', 'me'], // enforce consistent naming when capturing the current execution context
  44. 'curly': ['error', 'all'], // enforce consistent brace style for all control statements
  45. 'no-else-return': ['error', { 'allowElseIf': true }], // disallow else blocks after return statements in if statements
  46. 'no-param-reassign': ['error', { 'props': false }], // disallow reassigning function parameters
  47. 'no-unused-vars': ['error', { 'args': 'none', 'caughtErrors': 'none' }], // disallow unused variables
  48. '@stylistic/arrow-parens': ['error', 'always'], // enforces the consistent use of parentheses in arrow functions
  49. '@stylistic/brace-style': ['error', '1tbs', { 'allowSingleLine': false }], // describes the placement of braces relative to their control statement and body
  50. '@stylistic/comma-dangle': ['error', {
  51. 'arrays': 'always-multiline',
  52. 'objects': 'always-multiline',
  53. 'imports': 'always-multiline',
  54. 'exports': 'always-multiline',
  55. 'functions': 'never',
  56. }], // enforces consistent use of trailing commas in object and array literals
  57. '@stylistic/indent': ['error', 'tab', { 'SwitchCase': 0 }], // enforces a consistent indentation style
  58. '@stylistic/indent-binary-ops': ['error', 'tab'], // indentation for binary operators in multiline expressions
  59. '@stylistic/multiline-ternary': ['error', 'always-multiline'], // enforces or disallows newlines between operands of a ternary expression
  60. '@stylistic/no-tabs': ['error', { 'allowIndentationTabs': true }], // looks for tabs anywhere inside a file: code, comments or anything else
  61. '@stylistic/quote-props': ['error', 'consistent'], // requires quotes around object literal property names
  62. '@stylistic/quotes': ['error', 'single'], // enforces the consistent use of either backticks, double, or single quotes
  63. '@stylistic/semi': ['error', 'always'], // enforces consistent use of semicolons
  64. '@stylistic/spaced-comment': ['error', 'always', { 'block': { 'exceptions': ['*'] } }], // enforce consistency of spacing after the start of a comment
  65. 'camelcase': 'off', // disable: camelcase naming convention
  66. 'capitalized-comments': 'off', // disable: enforce or disallow capitalization of the first letter of a comment
  67. 'complexity': 'off', // disable: enforce a maximum cyclomatic complexity allowed in a program
  68. 'dot-notation': 'off', // disable: enforce dot notation whenever possible
  69. 'eqeqeq': 'off', // disable: require the use of === and !==
  70. 'func-name-matching': 'off', // disable: require function names to match the name of the variable or property to which they are assigned
  71. 'func-names': 'off', // disable: checking named function expressions
  72. 'func-style': 'off', // disable: consistent use of either function declarations or expressions
  73. 'id-length': 'off', // disable: enforce minimum and maximum identifier lengths
  74. 'init-declarations': 'off', // disable: require or disallow initialization in variable declarations
  75. 'line-comment-position': 'off', // disable: enforce position of line comments
  76. 'max-classes-per-file': 'off', // disable: maximum number of classes per file
  77. 'max-lines': 'off', // disable: maximum number of lines per file
  78. 'max-lines-per-function': 'off', // disable: maximum number of lines of code in a function
  79. 'max-params': 'off', // disable: enforce a maximum number of parameters in function definitions
  80. 'max-statements': 'off', // disable: maximum number of statements allowed in function blocks
  81. 'multiline-comment-style': 'off', // disable: enforce a particular style for multiline comments
  82. 'new-cap': 'off', // disable: require constructor names to begin with a capital letter
  83. 'no-bitwise': 'off', // disable: disallow bitwise operators
  84. 'no-continue': 'off', // disable: disallow continue statements
  85. 'no-empty-function': 'off', // disable: disallow empty functions
  86. 'no-eq-null': 'off', // disable: disallow null comparisons without type-checking operators
  87. 'no-implicit-coercion': 'off', // disable: disallow shorthand type conversions
  88. 'no-inline-comments': 'off', // disable: disallow inline comments after code
  89. 'no-magic-numbers': 'off', // disable: disallow magic numbers
  90. 'no-negated-condition': 'off', // disable: disallow negated conditions
  91. 'no-plusplus': 'off', // disable: disallow the unary operators ++ and --
  92. 'no-self-assign': 'off', // disable: disallow assignments where both sides are exactly the same
  93. 'no-ternary': 'off', // disable: disallow ternary operators
  94. 'no-undefined': 'off', // disable: disallow the use of undefined as an identifier
  95. 'no-underscore-dangle': 'off', // disable: disallow dangling underscores in identifiers
  96. 'no-useless-assignment': 'off', // disable: disallow variable assignments when the value is not used
  97. 'no-warning-comments': 'off', // disable: disallow specified warning terms in comments
  98. 'object-shorthand': 'off', // disable: require or disallow method and property shorthand syntax for object literals
  99. 'one-var': 'off', // disable: enforce variables to be declared either together or separately in functions
  100. 'prefer-arrow-callback': 'off', // disable: require using arrow functions for callbacks
  101. 'prefer-destructuring': 'off', // disable: require destructuring from arrays and/or objects
  102. 'prefer-named-capture-group': 'off', // disable: enforce using named capture group in regular expression
  103. 'prefer-promise-reject-errors': 'off', // disable: require using Error objects as Promise rejection reasons
  104. 'prefer-rest-params': 'off', // disable: require rest parameters instead of arguments
  105. 'prefer-spread': 'off', // disable: require spread operators instead of .apply()
  106. 'require-unicode-regexp': 'off', // disable: enforce the use of u or v flag on RegExp
  107. 'sort-keys': 'off', // disable: require object keys to be sorted
  108. },
  109. },
  110. // jsdoc2rst (node)
  111. {
  112. files: ['js/jsdoc2rst/**/*.js', 'platform/web/js/jsdoc2rst/**/*.js'],
  113. languageOptions: {
  114. globals: globals.node,
  115. },
  116. },
  117. // engine files (browser)
  118. {
  119. files: ['js/engine/**/*.js', 'platform/web/js/engine/**/*.js'],
  120. languageOptions: {
  121. globals: {
  122. ...globals.browser,
  123. 'Features': true,
  124. 'Godot': true,
  125. 'InternalConfig': true,
  126. 'Preloader': true,
  127. },
  128. },
  129. },
  130. // libraries and modules (browser)
  131. {
  132. files: ['js/libs/**/*.js', 'platform/web/js/libs/**/*.js', 'modules/**/*.js'],
  133. languageOptions: {
  134. globals: {
  135. ...globals.browser,
  136. ...emscriptenGlobals,
  137. 'GodotConfig': true,
  138. 'GodotEventListeners': true,
  139. 'GodotFS': true,
  140. 'GodotOS': true,
  141. 'GodotAudio': true,
  142. 'GodotRuntime': true,
  143. 'IDHandler': true,
  144. 'XRWebGLLayer': true,
  145. },
  146. },
  147. },
  148. // javascript templates (service workers)
  149. {
  150. files: ['misc/dist/html/**/*.js'],
  151. languageOptions: {
  152. globals: {
  153. ...globals.browser,
  154. '___GODOT_CACHE___': true,
  155. '___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___': true,
  156. '___GODOT_OPT_CACHE___': true,
  157. },
  158. },
  159. },
  160. // html templates
  161. {
  162. files: ['misc/dist/html/**/*.html'],
  163. plugins: {
  164. '@html-eslint': htmlPlugin,
  165. 'eslint-plugin-html': pluginReference,
  166. },
  167. languageOptions: {
  168. parser: htmlParser,
  169. globals: {
  170. ...globals.browser,
  171. 'Engine': true,
  172. '$GODOT_CONFIG': true,
  173. '$GODOT_PROJECT_NAME': true,
  174. '$GODOT_THREADS_ENABLED': true,
  175. '___GODOT_THREADS_ENABLED___': true,
  176. },
  177. },
  178. rules: {
  179. ...htmlPlugin.configs.recommended.rules,
  180. '@html-eslint/indent': ['error', 'tab'],
  181. '@html-eslint/require-closing-tags': ['error', { 'selfClosing': 'never' }],
  182. 'no-alert': 'off',
  183. 'no-console': 'off',
  184. },
  185. },
  186. {
  187. ignores: [
  188. '**/eslint.config.cjs',
  189. '**/.eslintrc*.js',
  190. '**/*.externs.js',
  191. ],
  192. },
  193. ];