您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

74 行
2.1 KiB

3 年前
  1. 'use strict';
  2. const createAstUtils = require('../util/ast');
  3. function newLayer() {
  4. return {
  5. describeTitles: [],
  6. testTitles: []
  7. };
  8. }
  9. function isFirstArgLiteral(node) {
  10. return node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal';
  11. }
  12. module.exports = {
  13. meta: {
  14. type: 'suggestion',
  15. docs: {
  16. description: 'Disallow identical titles'
  17. }
  18. },
  19. create(context) {
  20. const astUtils = createAstUtils(context.settings);
  21. const titleLayers = [ newLayer() ];
  22. function handlTestCaseTitles(titles, node, title) {
  23. if (astUtils.isTestCase(node)) {
  24. if (titles.includes(title)) {
  25. context.report({
  26. node,
  27. message: 'Test title is used multiple times in the same test suite.'
  28. });
  29. }
  30. titles.push(title);
  31. }
  32. }
  33. function handlTestSuiteTitles(titles, node, title) {
  34. if (!astUtils.isDescribe(node)) {
  35. return;
  36. }
  37. if (titles.includes(title)) {
  38. context.report({
  39. node,
  40. message: 'Test suite title is used multiple times.'
  41. });
  42. }
  43. titles.push(title);
  44. }
  45. return {
  46. CallExpression(node) {
  47. const currentLayer = titleLayers[titleLayers.length - 1];
  48. if (astUtils.isDescribe(node)) {
  49. titleLayers.push(newLayer());
  50. }
  51. if (!isFirstArgLiteral(node)) {
  52. return;
  53. }
  54. const title = node.arguments[0].value;
  55. handlTestCaseTitles(currentLayer.testTitles, node, title);
  56. handlTestSuiteTitles(currentLayer.describeTitles, node, title);
  57. },
  58. 'CallExpression:exit'(node) {
  59. if (astUtils.isDescribe(node)) {
  60. titleLayers.pop();
  61. }
  62. }
  63. };
  64. }
  65. };