Bluesky app fork with some witchin' additions 馃挮
at main 188 lines 4.3 kB view raw
1const {RuleTester} = require('eslint') 2const tseslint = require('typescript-eslint') 3const linguiMsgRule = require('../lingui-msg-rule') 4 5const ruleTester = new RuleTester({ 6 languageOptions: { 7 parser: tseslint.parser, 8 parserOptions: { 9 ecmaFeatures: { 10 jsx: true, 11 }, 12 ecmaVersion: 'latest', 13 sourceType: 'module', 14 }, 15 }, 16}) 17 18describe('lingui-msg-rule', () => { 19 const tests = { 20 valid: [ 21 // msg template literal 22 { 23 code: ` 24const {_} = useLingui() 25const x = _(msg\`Hello\`) 26 `, 27 }, 28 // msg template literal with interpolation 29 { 30 code: ` 31const {_} = useLingui() 32const name = 'World' 33const x = _(msg\`Hello \${name}\`) 34 `, 35 }, 36 // plural macro 37 { 38 code: ` 39const {_} = useLingui() 40const count = 5 41const x = _(plural(count, {one: '# item', other: '# items'})) 42 `, 43 }, 44 // select macro 45 { 46 code: ` 47const {_} = useLingui() 48const gender = 'female' 49const x = _(select(gender, {male: 'He', female: 'She', other: 'They'})) 50 `, 51 }, 52 // selectOrdinal macro 53 { 54 code: ` 55const {_} = useLingui() 56const position = 1 57const x = _(selectOrdinal(position, {one: '#st', two: '#nd', few: '#rd', other: '#th'})) 58 `, 59 }, 60 // msg function call with object (descriptor form) 61 { 62 code: ` 63const {_} = useLingui() 64const x = _(msg({message: 'Hello'})) 65 `, 66 }, 67 // msg function call with object and context 68 { 69 code: ` 70const {_} = useLingui() 71const x = _(msg({message: 'Hello', context: 'greeting'})) 72 `, 73 }, 74 ], 75 invalid: [ 76 // Plain string literal (single quotes) - with auto-fix 77 { 78 code: ` 79const {_} = useLingui() 80const x = _('Bad') 81 `, 82 output: ` 83const {_} = useLingui() 84const x = _(msg\`Bad\`) 85 `, 86 errors: [{messageId: 'missingMsg'}], 87 }, 88 // Plain string literal (double quotes) - with auto-fix 89 { 90 code: ` 91const {_} = useLingui() 92const x = _("Bad") 93 `, 94 output: ` 95const {_} = useLingui() 96const x = _(msg\`Bad\`) 97 `, 98 errors: [{messageId: 'missingMsg'}], 99 }, 100 // Template literal without msg tag - with auto-fix 101 { 102 code: ` 103const {_} = useLingui() 104const x = _(\`Bad\`) 105 `, 106 output: ` 107const {_} = useLingui() 108const x = _(msg\`Bad\`) 109 `, 110 errors: [{messageId: 'missingMsg'}], 111 }, 112 // Template literal with interpolation - with auto-fix 113 { 114 code: ` 115const {_} = useLingui() 116const name = 'World' 117const x = _(\`Hello \${name}\`) 118 `, 119 output: ` 120const {_} = useLingui() 121const name = 'World' 122const x = _(msg\`Hello \${name}\`) 123 `, 124 errors: [{messageId: 'missingMsg'}], 125 }, 126 // String with backticks that need escaping 127 { 128 code: ` 129const {_} = useLingui() 130const x = _('Use \\\`code\\\` here') 131 `, 132 output: ` 133const {_} = useLingui() 134const x = _(msg\`Use \\\`code\\\` here\`) 135 `, 136 errors: [{messageId: 'missingMsg'}], 137 }, 138 // Variable/identifier - no auto-fix possible 139 { 140 code: ` 141const {_} = useLingui() 142const message = 'Hello' 143const x = _(message) 144 `, 145 output: null, 146 errors: [{messageId: 'missingMsg'}], 147 }, 148 // Arbitrary function call - no auto-fix possible 149 { 150 code: ` 151const {_} = useLingui() 152const x = _(getMessage()) 153 `, 154 output: null, 155 errors: [{messageId: 'missingMsg'}], 156 }, 157 // Empty call - no auto-fix possible 158 { 159 code: ` 160const {_} = useLingui() 161const x = _() 162 `, 163 output: null, 164 errors: [{messageId: 'missingMsg'}], 165 }, 166 // Tagged template with wrong tag - no auto-fix (would need to replace tag) 167 { 168 code: ` 169const {_} = useLingui() 170const x = _(html\`Hello\`) 171 `, 172 output: null, 173 errors: [{messageId: 'missingMsg'}], 174 }, 175 // Number literal - no auto-fix possible 176 { 177 code: ` 178const {_} = useLingui() 179const x = _(123) 180 `, 181 output: null, 182 errors: [{messageId: 'missingMsg'}], 183 }, 184 ], 185 } 186 187 ruleTester.run('lingui-msg-rule', linguiMsgRule, tests) 188})