es-syntax.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 getOrSet = /^(?:g|s)et$/u
  12. const features = {
  13. //--------------------------------------------------------------------------
  14. // ES2015
  15. //--------------------------------------------------------------------------
  16. arrowFunctions: {
  17. ruleId: "no-arrow-functions",
  18. cases: [
  19. {
  20. supported: "4.0.0",
  21. messageId: "no-arrow-functions",
  22. },
  23. ],
  24. },
  25. binaryNumericLiterals: {
  26. ruleId: "no-binary-numeric-literals",
  27. cases: [
  28. {
  29. supported: "4.0.0",
  30. messageId: "no-binary-numeric-literals",
  31. },
  32. ],
  33. },
  34. blockScopedFunctions: {
  35. ruleId: "no-block-scoped-functions",
  36. cases: [
  37. {
  38. supported: "6.0.0",
  39. test: info => !info.isStrict,
  40. messageId: "no-block-scoped-functions-sloppy",
  41. },
  42. {
  43. supported: "4.0.0",
  44. messageId: "no-block-scoped-functions-strict",
  45. },
  46. ],
  47. },
  48. blockScopedVariables: {
  49. ruleId: "no-block-scoped-variables",
  50. cases: [
  51. {
  52. supported: "6.0.0",
  53. test: info => !info.isStrict,
  54. messageId: "no-block-scoped-variables-sloppy",
  55. },
  56. {
  57. supported: "4.0.0",
  58. messageId: "no-block-scoped-variables-strict",
  59. },
  60. ],
  61. },
  62. classes: {
  63. ruleId: "no-classes",
  64. cases: [
  65. {
  66. supported: "6.0.0",
  67. test: info => !info.isStrict,
  68. messageId: "no-classes-sloppy",
  69. },
  70. {
  71. supported: "4.0.0",
  72. messageId: "no-classes-strict",
  73. },
  74. ],
  75. },
  76. computedProperties: {
  77. ruleId: "no-computed-properties",
  78. cases: [
  79. {
  80. supported: "4.0.0",
  81. messageId: "no-computed-properties",
  82. },
  83. ],
  84. },
  85. defaultParameters: {
  86. ruleId: "no-default-parameters",
  87. cases: [
  88. {
  89. supported: "6.0.0",
  90. messageId: "no-default-parameters",
  91. },
  92. ],
  93. },
  94. destructuring: {
  95. ruleId: "no-destructuring",
  96. cases: [
  97. {
  98. supported: "6.0.0",
  99. messageId: "no-destructuring",
  100. },
  101. ],
  102. },
  103. forOfLoops: {
  104. ruleId: "no-for-of-loops",
  105. cases: [
  106. {
  107. supported: "0.12.0",
  108. messageId: "no-for-of-loops",
  109. },
  110. ],
  111. },
  112. generators: {
  113. ruleId: "no-generators",
  114. cases: [
  115. {
  116. supported: "4.0.0",
  117. messageId: "no-generators",
  118. },
  119. ],
  120. },
  121. modules: {
  122. ruleId: "no-modules",
  123. cases: [
  124. {
  125. supported: null,
  126. messageId: "no-modules",
  127. },
  128. ],
  129. },
  130. "new.target": {
  131. ruleId: "no-new-target",
  132. cases: [
  133. {
  134. supported: "5.0.0",
  135. messageId: "no-new-target",
  136. },
  137. ],
  138. },
  139. objectSuperProperties: {
  140. ruleId: "no-object-super-properties",
  141. cases: [
  142. {
  143. supported: "4.0.0",
  144. messageId: "no-object-super-properties",
  145. },
  146. ],
  147. },
  148. octalNumericLiterals: {
  149. ruleId: "no-octal-numeric-literals",
  150. cases: [
  151. {
  152. supported: "4.0.0",
  153. messageId: "no-octal-numeric-literals",
  154. },
  155. ],
  156. },
  157. propertyShorthands: {
  158. ruleId: "no-property-shorthands",
  159. cases: [
  160. {
  161. supported: "6.0.0",
  162. test: info =>
  163. info.node.shorthand && getOrSet.test(info.node.key.name),
  164. messageId: "no-property-shorthands-getset",
  165. },
  166. {
  167. supported: "4.0.0",
  168. messageId: "no-property-shorthands",
  169. },
  170. ],
  171. },
  172. regexpU: {
  173. ruleId: "no-regexp-u-flag",
  174. cases: [
  175. {
  176. supported: "6.0.0",
  177. messageId: "no-regexp-u-flag",
  178. },
  179. ],
  180. },
  181. regexpY: {
  182. ruleId: "no-regexp-y-flag",
  183. cases: [
  184. {
  185. supported: "6.0.0",
  186. messageId: "no-regexp-y-flag",
  187. },
  188. ],
  189. },
  190. restParameters: {
  191. ruleId: "no-rest-parameters",
  192. cases: [
  193. {
  194. supported: "6.0.0",
  195. messageId: "no-rest-parameters",
  196. },
  197. ],
  198. },
  199. spreadElements: {
  200. ruleId: "no-spread-elements",
  201. cases: [
  202. {
  203. supported: "5.0.0",
  204. messageId: "no-spread-elements",
  205. },
  206. ],
  207. },
  208. templateLiterals: {
  209. ruleId: "no-template-literals",
  210. cases: [
  211. {
  212. supported: "4.0.0",
  213. messageId: "no-template-literals",
  214. },
  215. ],
  216. },
  217. unicodeCodePointEscapes: {
  218. ruleId: "no-unicode-codepoint-escapes",
  219. cases: [
  220. {
  221. supported: "4.0.0",
  222. messageId: "no-unicode-codepoint-escapes",
  223. },
  224. ],
  225. },
  226. //--------------------------------------------------------------------------
  227. // ES2016
  228. //--------------------------------------------------------------------------
  229. exponentialOperators: {
  230. ruleId: "no-exponential-operators",
  231. cases: [
  232. {
  233. supported: "7.0.0",
  234. messageId: "no-exponential-operators",
  235. },
  236. ],
  237. },
  238. //--------------------------------------------------------------------------
  239. // ES2017
  240. //--------------------------------------------------------------------------
  241. asyncFunctions: {
  242. ruleId: "no-async-functions",
  243. cases: [
  244. {
  245. supported: "7.6.0",
  246. messageId: "no-async-functions",
  247. },
  248. ],
  249. },
  250. trailingCommasInFunctions: {
  251. ruleId: "no-trailing-function-commas",
  252. cases: [
  253. {
  254. supported: "8.0.0",
  255. messageId: "no-trailing-function-commas",
  256. },
  257. ],
  258. },
  259. //--------------------------------------------------------------------------
  260. // ES2018
  261. //--------------------------------------------------------------------------
  262. asyncIteration: {
  263. ruleId: "no-async-iteration",
  264. cases: [
  265. {
  266. supported: "10.0.0",
  267. messageId: "no-async-iteration",
  268. },
  269. ],
  270. },
  271. malformedTemplateLiterals: {
  272. ruleId: "no-malformed-template-literals",
  273. cases: [
  274. {
  275. supported: "8.10.0",
  276. messageId: "no-malformed-template-literals",
  277. },
  278. ],
  279. },
  280. regexpLookbehind: {
  281. ruleId: "no-regexp-lookbehind-assertions",
  282. cases: [
  283. {
  284. supported: "8.10.0",
  285. messageId: "no-regexp-lookbehind-assertions",
  286. },
  287. ],
  288. },
  289. regexpNamedCaptureGroups: {
  290. ruleId: "no-regexp-named-capture-groups",
  291. cases: [
  292. {
  293. supported: "10.0.0",
  294. messageId: "no-regexp-named-capture-groups",
  295. },
  296. ],
  297. },
  298. regexpS: {
  299. ruleId: "no-regexp-s-flag",
  300. cases: [
  301. {
  302. supported: "8.10.0",
  303. messageId: "no-regexp-s-flag",
  304. },
  305. ],
  306. },
  307. regexpUnicodeProperties: {
  308. ruleId: "no-regexp-unicode-property-escapes",
  309. cases: [
  310. {
  311. supported: "10.0.0",
  312. messageId: "no-regexp-unicode-property-escapes",
  313. },
  314. ],
  315. },
  316. restSpreadProperties: {
  317. ruleId: "no-rest-spread-properties",
  318. cases: [
  319. {
  320. supported: "8.3.0",
  321. messageId: "no-rest-spread-properties",
  322. },
  323. ],
  324. },
  325. //--------------------------------------------------------------------------
  326. // ES2019
  327. //--------------------------------------------------------------------------
  328. jsonSuperset: {
  329. ruleId: "no-json-superset",
  330. cases: [
  331. {
  332. supported: "10.0.0",
  333. messageId: "no-json-superset",
  334. },
  335. ],
  336. },
  337. optionalCatchBinding: {
  338. ruleId: "no-optional-catch-binding",
  339. cases: [
  340. {
  341. supported: "10.0.0",
  342. messageId: "no-optional-catch-binding",
  343. },
  344. ],
  345. },
  346. }
  347. const keywords = Object.keys(features)
  348. /**
  349. * Parses the options.
  350. * @param {RuleContext} context The rule context.
  351. * @returns {{version:Range,ignores:Set<string>}} Parsed value.
  352. */
  353. function parseOptions(context) {
  354. const raw = context.options[0] || {}
  355. const filePath = context.getFilename()
  356. const version = getConfiguredNodeVersion(raw.version, filePath)
  357. const ignores = new Set(raw.ignores || [])
  358. return Object.freeze({ version, ignores })
  359. }
  360. /**
  361. * Find the scope that a given node belongs to.
  362. * @param {Scope} initialScope The initial scope to find.
  363. * @param {Node} node The AST node.
  364. * @returns {Scope} The scope that the node belongs to.
  365. */
  366. function nomalizeScope(initialScope, node) {
  367. let scope = getInnermostScope(initialScope, node)
  368. while (scope && scope.block === node) {
  369. scope = scope.upper
  370. }
  371. return scope
  372. }
  373. /**
  374. * Merge two visitors.
  375. * @param {Visitor} x The visitor which is assigned.
  376. * @param {Visitor} y The visitor which is assigning.
  377. * @returns {Visitor} `x`.
  378. */
  379. function merge(x, y) {
  380. for (const key of Object.keys(y)) {
  381. if (typeof x[key] === "function") {
  382. if (x[key]._handlers == null) {
  383. const fs = [x[key], y[key]]
  384. x[key] = dispatch.bind(null, fs)
  385. x[key]._handlers = fs
  386. } else {
  387. x[key]._handlers.push(y[key])
  388. }
  389. } else {
  390. x[key] = y[key]
  391. }
  392. }
  393. return x
  394. }
  395. /**
  396. * Dispatch all given functions with a node.
  397. * @param {function[]} handlers The function list to call.
  398. * @param {Node} node The AST node to be handled.
  399. * @returns {void}
  400. */
  401. function dispatch(handlers, node) {
  402. for (const h of handlers) {
  403. h(node)
  404. }
  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 nomalizeScope(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 merge(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/v8.0.1/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. },
  601. create(context) {
  602. return defineVisitor(context, parseOptions(context))
  603. },
  604. }