|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const utils = require('../utils'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Checks if the given token is a comma token or not. |
| 7 | + * From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js |
| 8 | + * @param {Token} token The token to check. |
| 9 | + * @returns {boolean} `true` if the token is a comma token. |
| 10 | + */ |
| 11 | +function isCommaToken (token) { |
| 12 | + return token.value === ',' && token.type === 'Punctuator'; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks if the given token is an opening brace token or not. |
| 17 | + * From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js |
| 18 | + * @param {Token} token The token to check. |
| 19 | + * @returns {boolean} `true` if the token is an opening brace token. |
| 20 | + */ |
| 21 | +function isOpeningBraceToken (token) { |
| 22 | + return token.value === '{' && token.type === 'Punctuator'; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Checks if the given token is a closing brace token or not. |
| 27 | + * From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js |
| 28 | + * @param {Token} token The token to check. |
| 29 | + * @returns {boolean} `true` if the token is a closing brace token. |
| 30 | + */ |
| 31 | +function isClosingBraceToken (token) { |
| 32 | + return token.value === '}' && token.type === 'Punctuator'; |
| 33 | +} |
| 34 | + |
| 35 | +module.exports = { |
| 36 | + meta: { |
| 37 | + type: 'problem', |
| 38 | + docs: { |
| 39 | + description: 'disallow the test case property `only`', |
| 40 | + category: 'Tests', |
| 41 | + recommended: false, |
| 42 | + }, |
| 43 | + schema: [], |
| 44 | + messages: { |
| 45 | + foundOnly: |
| 46 | + 'The test case property `only` can be used during development, but should not be checked-in, since it prevents all the tests from running.', |
| 47 | + removeOnly: 'Remove `only`.', |
| 48 | + }, |
| 49 | + hasSuggestions: true, |
| 50 | + }, |
| 51 | + |
| 52 | + create (context) { |
| 53 | + return { |
| 54 | + Program (ast) { |
| 55 | + for (const testRun of utils.getTestInfo(context, ast)) { |
| 56 | + for (const test of [...testRun.valid, ...testRun.invalid]) { |
| 57 | + if (test.type === 'ObjectExpression') { |
| 58 | + // Test case object: { code: 'const x = 123;', ... } |
| 59 | + |
| 60 | + const onlyProperty = test.properties.find( |
| 61 | + property => |
| 62 | + property.key.type === 'Identifier' && |
| 63 | + property.key.name === 'only' && |
| 64 | + property.value.type === 'Literal' && |
| 65 | + property.value.value |
| 66 | + ); |
| 67 | + |
| 68 | + if (onlyProperty) { |
| 69 | + context.report({ |
| 70 | + node: onlyProperty, |
| 71 | + messageId: 'foundOnly', |
| 72 | + suggest: [ |
| 73 | + { |
| 74 | + messageId: 'removeOnly', |
| 75 | + *fix (fixer) { |
| 76 | + const sourceCode = context.getSourceCode(); |
| 77 | + |
| 78 | + const tokenBefore = sourceCode.getTokenBefore(onlyProperty); |
| 79 | + const tokenAfter = sourceCode.getTokenAfter(onlyProperty); |
| 80 | + if ( |
| 81 | + (isCommaToken(tokenBefore) && isCommaToken(tokenAfter)) || // In middle of properties |
| 82 | + (isOpeningBraceToken(tokenBefore) && isCommaToken(tokenAfter)) // At beginning of properties |
| 83 | + ) { |
| 84 | + yield fixer.remove(tokenAfter); |
| 85 | + } |
| 86 | + if (isCommaToken(tokenBefore) && isClosingBraceToken(tokenAfter)) { // At end of properties |
| 87 | + yield fixer.remove(tokenBefore); |
| 88 | + } |
| 89 | + |
| 90 | + yield fixer.remove(onlyProperty); |
| 91 | + }, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + } |
| 96 | + } else if ( |
| 97 | + test.type === 'CallExpression' && |
| 98 | + test.callee.type === 'MemberExpression' && |
| 99 | + test.callee.object.type === 'Identifier' && |
| 100 | + test.callee.object.name === 'RuleTester' && |
| 101 | + test.callee.property.type === 'Identifier' && |
| 102 | + test.callee.property.name === 'only' |
| 103 | + ) { |
| 104 | + // RuleTester.only('const x = 123;'); |
| 105 | + context.report({ node: test.callee, messageId: 'foundOnly' }); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + }; |
| 111 | + }, |
| 112 | +}; |
0 commit comments