|
| 1 | +// Generate editorconfig templates from built-in eslint configs. |
| 2 | + |
| 3 | +// Supported rules: |
| 4 | +// indent_style |
| 5 | +// indent_size |
| 6 | +// end_of_line |
| 7 | +// trim_trailing_whitespace |
| 8 | +// insert_final_newline |
| 9 | +// max_line_length |
| 10 | + |
| 11 | +const fs = require('fs') |
| 12 | +const path = require('path') |
| 13 | +const CLIEngine = require('eslint').CLIEngine |
| 14 | + |
| 15 | +// Convert eslint rules to editorconfig rules. |
| 16 | +function convertRules (config) { |
| 17 | + const result = {} |
| 18 | + |
| 19 | + const eslintRules = new CLIEngine({ |
| 20 | + useEslintrc: false, |
| 21 | + baseConfig: { |
| 22 | + extends: [require.resolve(`@vue/eslint-config-${config}`)] |
| 23 | + } |
| 24 | + }).getConfigForFile().rules |
| 25 | + |
| 26 | + const getRuleOptions = (ruleName, defaultOptions = []) => { |
| 27 | + const ruleConfig = eslintRules[ruleName] |
| 28 | + |
| 29 | + if (!ruleConfig || ruleConfig === 0 || ruleConfig === 'off') { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + if (Array.isArray(ruleConfig) && (ruleConfig[0] === 0 || ruleConfig[0] === 'off')) { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if (Array.isArray(ruleConfig) && ruleConfig.length > 1) { |
| 38 | + return ruleConfig.slice(1) |
| 39 | + } |
| 40 | + |
| 41 | + return defaultOptions |
| 42 | + } |
| 43 | + |
| 44 | + // https://eslint.org/docs/rules/indent |
| 45 | + const indent = getRuleOptions('indent', [4]) |
| 46 | + if (indent) { |
| 47 | + result.indent_style = indent[0] === 'tab' ? 'tab' : 'space' |
| 48 | + |
| 49 | + if (typeof indent[0] === 'number') { |
| 50 | + result.indent_size = indent[0] |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // https://eslint.org/docs/rules/linebreak-style |
| 55 | + const linebreakStyle = getRuleOptions('linebreak-style', ['unix']) |
| 56 | + if (linebreakStyle) { |
| 57 | + result.end_of_line = linebreakStyle[0] === 'unix' ? 'lf' : 'crlf' |
| 58 | + } |
| 59 | + |
| 60 | + // https://eslint.org/docs/rules/no-trailing-spaces |
| 61 | + const noTrailingSpaces = getRuleOptions('no-trailing-spaces', [{ skipBlankLines: false, ignoreComments: false }]) |
| 62 | + if (noTrailingSpaces) { |
| 63 | + if (!noTrailingSpaces[0].skipBlankLines && !noTrailingSpaces[0].ignoreComments) { |
| 64 | + result.trim_trailing_whitespace = true |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // https://eslint.org/docs/rules/eol-last |
| 69 | + const eolLast = getRuleOptions('eol-last', ['always']) |
| 70 | + if (eolLast) { |
| 71 | + result.insert_final_newline = eolLast[0] !== 'never' |
| 72 | + } |
| 73 | + |
| 74 | + // https://eslint.org/docs/rules/max-len |
| 75 | + const maxLen = getRuleOptions('max-len', [{ code: 80 }]) |
| 76 | + if (maxLen) { |
| 77 | + // To simplify the implementation logic, we only read from the `code` option. |
| 78 | + |
| 79 | + // `max-len` has an undocumented array-style configuration, |
| 80 | + // where max code length specified directly as integers |
| 81 | + // (used by `eslint-config-airbnb`). |
| 82 | + |
| 83 | + if (typeof maxLen[0] === 'number') { |
| 84 | + result.max_line_length = maxLen[0] |
| 85 | + } else { |
| 86 | + result.max_line_length = maxLen[0].code |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return result |
| 91 | +} |
| 92 | + |
| 93 | +exports.buildEditorConfig = function buildEditorConfig () { |
| 94 | + console.log('Building EditorConfig files...') |
| 95 | + // Get built-in eslint configs |
| 96 | + const configList = fs.readdirSync(path.resolve(__dirname, '../packages/@vue/')) |
| 97 | + .map(name => { |
| 98 | + const matched = /eslint-config-(\w+)/.exec(name) |
| 99 | + return matched && matched[1] |
| 100 | + }) |
| 101 | + .filter(x => x) |
| 102 | + |
| 103 | + configList.forEach(config => { |
| 104 | + let content = '[*.{js,jsx,ts,tsx,vue}]\n' |
| 105 | + |
| 106 | + const editorconfig = convertRules(config) |
| 107 | + |
| 108 | + // `eslint-config-prettier` & `eslint-config-typescript` do not have any style rules |
| 109 | + if (!Object.keys(editorconfig).length) { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + for (const [key, value] of Object.entries(editorconfig)) { |
| 114 | + content += `${key} = ${value}\n` |
| 115 | + } |
| 116 | + |
| 117 | + const templateDir = path.resolve(__dirname, `../packages/@vue/cli-plugin-eslint/generator/template/${config}`) |
| 118 | + if (!fs.existsSync(templateDir)) { |
| 119 | + fs.mkdirSync(templateDir) |
| 120 | + } |
| 121 | + fs.writeFileSync(`${templateDir}/_editorconfig`, content) |
| 122 | + }) |
| 123 | + console.log('EditorConfig files up-to-date.') |
| 124 | +} |
0 commit comments