1/** 2 * @fileOverview Any non-ASCII characters in lib/ will increase the size 3 * of the compiled node binary. This linter rule ensures that 4 * any such character is reported. 5 * @author Sarat Addepalli <sarat.addepalli@gmail.com> 6 */ 7 8'use strict'; 9 10//------------------------------------------------------------------------------ 11// Rule Definition 12//------------------------------------------------------------------------------ 13 14const nonAsciiRegexPattern = /[^\r\n\x20-\x7e]/; 15const suggestions = { 16 '’': '\'', 17 '‛': '\'', 18 '‘': '\'', 19 '“': '"', 20 '‟': '"', 21 '”': '"', 22 '«': '"', 23 '»': '"', 24 '—': '-' 25}; 26 27module.exports = (context) => { 28 29 const reportIfError = (node, sourceCode) => { 30 31 const matches = sourceCode.text.match(nonAsciiRegexPattern); 32 33 if (!matches) return; 34 35 const offendingCharacter = matches[0]; 36 const offendingCharacterPosition = matches.index; 37 const suggestion = suggestions[offendingCharacter]; 38 39 let message = `Non-ASCII character '${offendingCharacter}' detected.`; 40 41 message = suggestion ? 42 `${message} Consider replacing with: ${suggestion}` : 43 message; 44 45 context.report({ 46 node, 47 message, 48 loc: sourceCode.getLocFromIndex(offendingCharacterPosition), 49 fix: (fixer) => { 50 return fixer.replaceText( 51 node, 52 suggestion ? `${suggestion}` : '' 53 ); 54 } 55 }); 56 }; 57 58 return { 59 Program: (node) => reportIfError(node, context.getSourceCode()) 60 }; 61}; 62 63module.exports.meta = { 64 fixable: 'code' 65}; 66