es-syntax.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { rules: esRules } = require("eslint-plugin-es")
  7. const { getInnermostScope } = require("eslint-utils")
  8. const { Range } = require("semver") //eslint-disable-line no-unused-vars
  9. const getConfiguredNodeVersion = require("../../util/get-configured-node-version")
  10. const getSemverRange = require("../../util/get-semver-range")
  11. const mergeVisitorsInPlace = require("../../util/merge-visitors-in-place")
  12. const getOrSet = /^(?:g|s)et$/u
  13. const features = {
  14. //--------------------------------------------------------------------------
  15. // ES2015
  16. //--------------------------------------------------------------------------
  17. arrowFunctions: {
  18. ruleId: "no-arrow-functions",
  19. cases: [
  20. {
  21. supported: "4.0.0",
  22. messageId: "no-arrow-functions",
  23. },
  24. ],
  25. },
  26. binaryNumericLiterals: {
  27. ruleId: "no-binary-numeric-literals",
  28. cases: [
  29. {
  30. supported: "4.0.0",
  31. messageId: "no-binary-numeric-literals",
  32. },
  33. ],
  34. },
  35. blockScopedFunctions: {
  36. ruleId: "no-block-scoped-functions",
  37. cases: [
  38. {
  39. supported: "6.0.0",
  40. test: info => !info.isStrict,
  41. messageId: "no-block-scoped-functions-sloppy",
  42. },
  43. {
  44. supported: "4.0.0",
  45. messageId: "no-block-scoped-functions-strict",
  46. },
  47. ],
  48. },
  49. blockScopedVariables: {
  50. ruleId: "no-block-scoped-variables",
  51. cases: [
  52. {
  53. supported: "6.0.0",
  54. test: info => !info.isStrict,
  55. messageId: "no-block-scoped-variables-sloppy",
  56. },
  57. {
  58. supported: "4.0.0",
  59. messageId: "no-block-scoped-variables-strict",
  60. },
  61. ],
  62. },
  63. classes: {
  64. ruleId: "no-classes",
  65. cases: [
  66. {
  67. supported: "6.0.0",
  68. test: info => !info.isStrict,
  69. messageId: "no-classes-sloppy",
  70. },
  71. {
  72. supported: "4.0.0",
  73. messageId: "no-classes-strict",
  74. },
  75. ],
  76. },
  77. computedProperties: {
  78. ruleId: "no-computed-properties",
  79. cases: [
  80. {
  81. supported: "4.0.0",
  82. messageId: "no-computed-properties",
  83. },
  84. ],
  85. },
  86. defaultParameters: {
  87. ruleId: "no-default-parameters",
  88. cases: [
  89. {
  90. supported: "6.0.0",
  91. messageId: "no-default-parameters",
  92. },
  93. ],
  94. },
  95. destructuring: {
  96. ruleId: "no-destructuring",
  97. cases: [
  98. {
  99. supported: "6.0.0",
  100. messageId: "no-destructuring",
  101. },
  102. ],
  103. },
  104. forOfLoops: {
  105. ruleId: "no-for-of-loops",
  106. cases: [
  107. {
  108. supported: "0.12.0",
  109. messageId: "no-for-of-loops",
  110. },
  111. ],
  112. },
  113. generators: {
  114. ruleId: "no-generators",
  115. cases: [
  116. {
  117. supported: "4.0.0",
  118. messageId: "no-generators",
  119. },
  120. ],
  121. },
  122. modules: {
  123. ruleId: "no-modules",
  124. cases: [
  125. {
  126. supported: null,
  127. messageId: "no-modules",
  128. },
  129. ],
  130. },
  131. "new.target": {
  132. ruleId: "no-new-target",
  133. cases: [
  134. {
  135. supported: "5.0.0",
  136. messageId: "no-new-target",
  137. },
  138. ],
  139. },
  140. objectSuperProperties: {
  141. ruleId: "no-object-super-properties",
  142. cases: [
  143. {
  144. supported: "4.0.0",
  145. messageId: "no-object-super-properties",
  146. },
  147. ],
  148. },
  149. octalNumericLiterals: {
  150. ruleId: "no-octal-numeric-literals",
  151. cases: [
  152. {
  153. supported: "4.0.0",
  154. messageId: "no-octal-numeric-literals",
  155. },
  156. ],
  157. },
  158. propertyShorthands: {
  159. ruleId: "no-property-shorthands",
  160. cases: [
  161. {
  162. supported: "6.0.0",
  163. test: info =>
  164. info.node.shorthand && getOrSet.test(info.node.key.name),
  165. messageId: "no-property-shorthands-getset",
  166. },
  167. {
  168. supported: "4.0.0",
  169. messageId: "no-property-shorthands",
  170. },
  171. ],
  172. },
  173. regexpU: {
  174. ruleId: "no-regexp-u-flag",
  175. cases: [
  176. {
  177. supported: "6.0.0",
  178. messageId: "no-regexp-u-flag",
  179. },
  180. ],
  181. },
  182. regexpY: {
  183. ruleId: "no-regexp-y-flag",
  184. cases: [
  185. {
  186. supported: "6.0.0",
  187. messageId: "no-regexp-y-flag",
  188. },
  189. ],
  190. },
  191. restParameters: {
  192. ruleId: "no-rest-parameters",
  193. cases: [
  194. {
  195. supported: "6.0.0",
  196. messageId: "no-rest-parameters",
  197. },
  198. ],
  199. },
  200. spreadElements: {
  201. ruleId: "no-spread-elements",
  202. cases: [
  203. {
  204. supported: "5.0.0",
  205. messageId: "no-spread-elements",
  206. },
  207. ],
  208. },
  209. templateLiterals: {
  210. ruleId: "no-template-literals",
  211. cases: [
  212. {
  213. supported: "4.0.0",
  214. messageId: "no-template-literals",
  215. },
  216. ],
  217. },
  218. unicodeCodePointEscapes: {
  219. ruleId: "no-unicode-codepoint-escapes",
  220. cases: [
  221. {
  222. supported: "4.0.0",
  223. messageId: "no-unicode-codepoint-escapes",
  224. },
  225. ],
  226. },
  227. //--------------------------------------------------------------------------
  228. // ES2016
  229. //--------------------------------------------------------------------------
  230. exponentialOperators: {
  231. ruleId: "no-exponential-operators",
  232. cases: [
  233. {
  234. supported: "7.0.0",
  235. messageId: "no-exponential-operators",
  236. },
  237. ],
  238. },
  239. //--------------------------------------------------------------------------
  240. // ES2017
  241. //--------------------------------------------------------------------------
  242. asyncFunctions: {
  243. ruleId: "no-async-functions",
  244. cases: [
  245. {
  246. supported: "7.6.0",
  247. messageId: "no-async-functions",
  248. },
  249. ],
  250. },
  251. trailingCommasInFunctions: {
  252. ruleId: "no-trailing-function-commas",
  253. cases: [
  254. {
  255. supported: "8.0.0",
  256. messageId: "no-trailing-function-commas",
  257. },
  258. ],
  259. },
  260. //--------------------------------------------------------------------------
  261. // ES2018
  262. //--------------------------------------------------------------------------
  263. asyncIteration: {
  264. ruleId: "no-async-iteration",
  265. cases: [
  266. {
  267. supported: "10.0.0",
  268. messageId: "no-async-iteration",
  269. },
  270. ],
  271. },
  272. malformedTemplateLiterals: {
  273. ruleId: "no-malformed-template-literals",
  274. cases: [
  275. {
  276. supported: "8.10.0",
  277. messageId: "no-malformed-template-literals",
  278. },
  279. ],
  280. },
  281. regexpLookbehind: {
  282. ruleId: "no-regexp-lookbehind-assertions",
  283. cases: [
  284. {
  285. supported: "8.10.0",
  286. messageId: "no-regexp-lookbehind-assertions",
  287. },
  288. ],
  289. },
  290. regexpNamedCaptureGroups: {
  291. ruleId: "no-regexp-named-capture-groups",
  292. cases: [
  293. {
  294. supported: "10.0.0",
  295. messageId: "no-regexp-named-capture-groups",
  296. },
  297. ],
  298. },
  299. regexpS: {
  300. ruleId: "no-regexp-s-flag",
  301. cases: [
  302. {
  303. supported: "8.10.0",
  304. messageId: "no-regexp-s-flag",
  305. },
  306. ],
  307. },
  308. regexpUnicodeProperties: {
  309. ruleId: "no-regexp-unicode-property-escapes",
  310. cases: [
  311. {
  312. supported: "10.0.0",
  313. messageId: "no-regexp-unicode-property-escapes",
  314. },
  315. ],
  316. },
  317. restSpreadProperties: {
  318. ruleId: "no-rest-spread-properties",
  319. cases: [
  320. {
  321. supported: "8.3.0",
  322. messageId: "no-rest-spread-properties",
  323. },
  324. ],
  325. },
  326. //--------------------------------------------------------------------------
  327. // ES2019
  328. //--------------------------------------------------------------------------
  329. jsonSuperset: {
  330. ruleId: "no-json-superset",
  331. cases: [
  332. {
  333. supported: "10.0.0",
  334. messageId: "no-json-superset",
  335. },
  336. ],
  337. },
  338. optionalCatchBinding: {
  339. ruleId: "no-optional-catch-binding",
  340. cases: [
  341. {
  342. supported: "10.0.0",
  343. messageId: "no-optional-catch-binding",
  344. },
  345. ],
  346. },
  347. //--------------------------------------------------------------------------
  348. // ES2020
  349. //--------------------------------------------------------------------------
  350. bigint: {
  351. ruleId: "no-bigint",
  352. cases: [
  353. {
  354. supported: "10.4.0",
  355. test: info => info.node.type === "Literal",
  356. messageId: "no-bigint",
  357. },
  358. {
  359. supported: null,
  360. test: ({ node }) =>
  361. node.type === "Literal" &&
  362. (node.parent.type === "Property" ||
  363. node.parent.type === "MethodDefinition") &&
  364. !node.parent.computed &&
  365. node.parent.key === node,
  366. messageId: "no-bigint-property-names",
  367. },
  368. ],
  369. },
  370. dynamicImport: {
  371. ruleId: "no-dynamic-import",
  372. cases: [
  373. {
  374. supported: null,
  375. messageId: "no-dynamic-import",
  376. },
  377. ],
  378. },
  379. }
  380. const keywords = Object.keys(features)
  381. /**
  382. * Parses the options.
  383. * @param {RuleContext} context The rule context.
  384. * @returns {{version:Range,ignores:Set<string>}} Parsed value.
  385. */
  386. function parseOptions(context) {
  387. const raw = context.options[0] || {}
  388. const filePath = context.getFilename()
  389. const version = getConfiguredNodeVersion(raw.version, filePath)
  390. const ignores = new Set(raw.ignores || [])
  391. return Object.freeze({ version, ignores })
  392. }
  393. /**
  394. * Find the scope that a given node belongs to.
  395. * @param {Scope} initialScope The initial scope to find.
  396. * @param {Node} node The AST node.
  397. * @returns {Scope} The scope that the node belongs to.
  398. */
  399. function normalizeScope(initialScope, node) {
  400. let scope = getInnermostScope(initialScope, node)
  401. while (scope && scope.block === node) {
  402. scope = scope.upper
  403. }
  404. return scope
  405. }
  406. /**
  407. * Define the visitor object as merging the rules of eslint-plugin-es.
  408. * @param {RuleContext} context The rule context.
  409. * @param {{version:Range,ignores:Set<string>}} options The options.
  410. * @returns {object} The defined visitor.
  411. */
  412. function defineVisitor(context, options) {
  413. const testInfoPrototype = {
  414. get isStrict() {
  415. return normalizeScope(context.getScope(), this.node).isStrict
  416. },
  417. }
  418. /**
  419. * Check whether a given case object is full-supported on the configured node version.
  420. * @param {{supported:string}} aCase The case object to check.
  421. * @returns {boolean} `true` if it's supporting.
  422. */
  423. function isNotSupportingVersion(aCase) {
  424. return (
  425. !aCase.supported ||
  426. options.version.intersects(getSemverRange(`<${aCase.supported}`))
  427. )
  428. }
  429. /**
  430. * Define the predicate function to check whether a given case object is supported on the configured node version.
  431. * @param {Node} node The node which is reported.
  432. * @returns {function(aCase:{supported:string}):boolean} The predicate function.
  433. */
  434. function isNotSupportingOn(node) {
  435. return aCase =>
  436. isNotSupportingVersion(aCase) &&
  437. (!aCase.test || aCase.test({ node, __proto__: testInfoPrototype }))
  438. }
  439. return (
  440. keywords
  441. // Omit full-supported features and ignored features by options
  442. // because this rule never reports those.
  443. .filter(
  444. keyword =>
  445. !options.ignores.has(keyword) &&
  446. features[keyword].cases.some(isNotSupportingVersion)
  447. )
  448. // Merge remaining features with overriding `context.report()`.
  449. .reduce((visitor, keyword) => {
  450. const { ruleId, cases } = features[keyword]
  451. const rule = esRules[ruleId]
  452. const thisContext = {
  453. __proto__: context,
  454. // Override `context.report()` then:
  455. // - ignore if it's supported.
  456. // - override reporting messages.
  457. report(descriptor) {
  458. // Set additional information.
  459. if (descriptor.data) {
  460. descriptor.data.version = options.version.raw
  461. } else {
  462. descriptor.data = { version: options.version.raw }
  463. }
  464. descriptor.fix = undefined
  465. // Test and report.
  466. const node = descriptor.node
  467. const hitCase = cases.find(isNotSupportingOn(node))
  468. if (hitCase) {
  469. descriptor.messageId = hitCase.messageId
  470. descriptor.data.supported = hitCase.supported
  471. super.report(descriptor)
  472. }
  473. },
  474. }
  475. return mergeVisitorsInPlace(visitor, rule.create(thisContext))
  476. }, {})
  477. )
  478. }
  479. module.exports = {
  480. meta: {
  481. docs: {
  482. description:
  483. "disallow unsupported ECMAScript syntax on the specified version",
  484. category: "Possible Errors",
  485. recommended: true,
  486. url:
  487. "https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/no-unsupported-features/es-syntax.md",
  488. },
  489. type: "problem",
  490. fixable: null,
  491. schema: [
  492. {
  493. type: "object",
  494. properties: {
  495. version: {
  496. type: "string",
  497. },
  498. ignores: {
  499. type: "array",
  500. items: {
  501. enum: Object.keys(features),
  502. },
  503. uniqueItems: true,
  504. },
  505. },
  506. additionalProperties: false,
  507. },
  508. ],
  509. messages: {
  510. //------------------------------------------------------------------
  511. // ES2015
  512. //------------------------------------------------------------------
  513. "no-arrow-functions":
  514. "Arrow functions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  515. "no-binary-numeric-literals":
  516. "Binary numeric literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  517. "no-block-scoped-functions-strict":
  518. "Block-scoped functions in strict mode are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  519. "no-block-scoped-functions-sloppy":
  520. "Block-scoped functions in non-strict mode are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  521. "no-block-scoped-variables-strict":
  522. "Block-scoped variables in strict mode are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  523. "no-block-scoped-variables-sloppy":
  524. "Block-scoped variables in non-strict mode are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  525. "no-classes-strict":
  526. "Classes in strict mode are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  527. "no-classes-sloppy":
  528. "Classes in non-strict mode are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  529. "no-computed-properties":
  530. "Computed properties are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  531. "no-default-parameters":
  532. "Default parameters are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  533. "no-destructuring":
  534. "Destructuring is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  535. "no-for-of-loops":
  536. "'for-of' loops are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  537. "no-generators":
  538. "Generator functions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  539. "no-modules":
  540. "Import and export declarations are not supported yet.",
  541. "no-new-target":
  542. "'new.target' is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  543. "no-object-super-properties":
  544. "'super' in object literals is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  545. "no-octal-numeric-literals":
  546. "Octal numeric literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  547. "no-property-shorthands":
  548. "Property shorthands are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  549. "no-property-shorthands-getset":
  550. "Property shorthands of 'get' and 'set' are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  551. "no-regexp-u-flag":
  552. "RegExp 'u' flag is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  553. "no-regexp-y-flag":
  554. "RegExp 'y' flag is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  555. "no-rest-parameters":
  556. "Rest parameters are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  557. "no-spread-elements":
  558. "Spread elements are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  559. "no-template-literals":
  560. "Template literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  561. "no-unicode-codepoint-escapes":
  562. "Unicode code point escapes are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  563. //------------------------------------------------------------------
  564. // ES2016
  565. //------------------------------------------------------------------
  566. "no-exponential-operators":
  567. "Exponential operators are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  568. //------------------------------------------------------------------
  569. // ES2017
  570. //------------------------------------------------------------------
  571. "no-async-functions":
  572. "Async functions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  573. "no-trailing-function-commas":
  574. "Trailing commas in function syntax are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  575. //------------------------------------------------------------------
  576. // ES2018
  577. //------------------------------------------------------------------
  578. "no-async-iteration":
  579. "Async iteration is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  580. "no-malformed-template-literals":
  581. "Malformed template literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  582. "no-regexp-lookbehind-assertions":
  583. "RegExp lookbehind assertions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  584. "no-regexp-named-capture-groups":
  585. "RegExp named capture groups are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  586. "no-regexp-s-flag":
  587. "RegExp 's' flag is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  588. "no-regexp-unicode-property-escapes":
  589. "RegExp Unicode property escapes are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  590. "no-rest-spread-properties":
  591. "Rest/spread properties are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  592. //------------------------------------------------------------------
  593. // ES2019
  594. //------------------------------------------------------------------
  595. "no-json-superset":
  596. "'\\u{{code}}' in string literals is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  597. "no-optional-catch-binding":
  598. "The omission of 'catch' binding is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  599. //------------------------------------------------------------------
  600. // ES2020
  601. //------------------------------------------------------------------
  602. "no-bigint":
  603. "Bigint literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  604. "no-bigint-property-names":
  605. "Bigint literal property names are not supported yet.",
  606. "no-dynamic-import":
  607. "'import()' expressions are not supported yet.",
  608. },
  609. },
  610. create(context) {
  611. return defineVisitor(context, parseOptions(context))
  612. },
  613. }