|
1 |
- {"version":3,"sources":["../src/bin/generateRule.js"],"names":["ruleName","options","process","argv","recommended","includes","console","error","test","ruleNamesPath","ruleNames","JSON","parse","fs","readFile","push","sort","writeFile","stringify","log","ruleTemplate","camelCasedRuleName","_","camelCase","rulePath","ruleTestTemplate","ruleTestPath","ruleReadmeTemplate","ruleReadmePath","replaceInOrder","path","oldRegex","checkName","newLine","oldIsCamel","offsets","readme","replace","matchedLine","n1","offset","str","oldRule","oldRuleB","alreadyIncluded","itemIndex","findIndex","item","undefined","pop","length","slice","repeat","chdir","__dirname"],"mappings":";;AAUA;;AAGA;;AACA;;AAGA;;AACA;;;;;;;;AAEA;AAEA,MAAM,IAAKA,QAAL,EAAe,GAAGC,OAAlB,IAA6BC,OAAO,CAACC,IAA3C;AAEA,MAAMC,WAAW,GAAGH,OAAO,CAACI,QAAR,CAAiB,eAAjB,CAApB;;AAEA,CAAC,YAAY;AACX,MAAI,CAACL,QAAL,EAAe;AACbM,IAAAA,OAAO,CAACC,KAAR,CAAc,2BAAd;AAEA;AACD;;AACD,MAAK,QAAD,CAAWC,IAAX,CAAgBR,QAAhB,CAAJ,EAA+B;AAC7BM,IAAAA,OAAO,CAACC,KAAR,CAAc,+CAAd;AAEA;AACD;;AAED,QAAME,aAAa,GAAG,6BAAtB;AACA,QAAMC,SAAS,GAAGC,IAAI,CAACC,KAAL,CAAW,MAAMC,kBAAGC,QAAH,CACjCL,aADiC,EAClB,MADkB,CAAjB,CAAlB;;AAGA,MAAI,CAACC,SAAS,CAACL,QAAV,CAAmBL,QAAnB,CAAL,EAAmC;AACjCU,IAAAA,SAAS,CAACK,IAAV,CAAef,QAAf;AACAU,IAAAA,SAAS,CAACM,IAAV;AACD;;AAED,QAAMH,kBAAGI,SAAH,CAAaR,aAAb,EAA4BE,IAAI,CAACO,SAAL,CAAeR,SAAf,EAA0B,IAA1B,EAAgC,CAAhC,IAAqC,IAAjE,CAAN;AACAJ,EAAAA,OAAO,CAACa,GAAR,CAAY,WAAZ,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cAAc,CAAC;AACnBG,IAAAA,SAAS,EAAE,aADQ;AAEnBC,IAAAA,OAAO,EAAG,GAAE,IAAImB,MAAJ,CAAW,CAAX,CAAc,IAAGpD,QAAS,MAAKqB,kBAAmB,GAF3C;AAGnBU,IAAAA,QAAQ,EAAE,sCAHS;AAInBD,IAAAA,IAAI,EAAE;AAJa,GAAD,CAApB;AAOA,qEAAa,qBAAb;AAEA;AACF;AACA;AACA;AACA;AACA;AAEE;;AACA5B,EAAAA,OAAO,CAACmD,KAAR,CAAc,mBAAQC,SAAR,EAAmB,QAAnB,CAAd;AACA,QAAM,yBAAK,CACT;AACA1B,EAAAA,cAFS,EAGTF,YAHS,EAITF,QAJS,CAAL,CAAN;AAMD,CAxMD","sourcesContent":["/* eslint-disable no-console -- CLI */\n\n/**\n * @example\n *\n * ```shell\n * npm run create-rule my-new-rule --recommended\n * ```\n */\n\nimport {\n existsSync,\n} from 'fs';\nimport fs from 'fs/promises';\nimport {\n resolve,\n} from 'path';\nimport _ from 'lodash';\nimport open from 'open-editor';\n\n// Todo: Would ideally have prompts, e.g., to ask for whether type was problem/layout, etc.\n\nconst [, , ruleName, ...options] = process.argv;\n\nconst recommended = options.includes('--recommended');\n\n(async () => {\n if (!ruleName) {\n console.error('Please supply a rule name');\n\n return;\n }\n if ((/[A-Z]/u).test(ruleName)) {\n console.error('Please ensure the rule has no capital letters');\n\n return;\n }\n\n const ruleNamesPath = './test/rules/ruleNames.json';\n const ruleNames = JSON.parse(await fs.readFile(\n ruleNamesPath, 'utf8',\n ));\n if (!ruleNames.includes(ruleName)) {\n ruleNames.push(ruleName);\n ruleNames.sort();\n }\n\n await fs.writeFile(ruleNamesPath, JSON.stringify(ruleNames, null, 2) + '\\n');\n console.log('ruleNames', ruleNames);\n\n const ruleTemplate = `import iterateJsdoc from '../iterateJsdoc';\n\nexport default iterateJsdoc(({\n context,\n utils,\n}) => {\n // Rule here\n}, {\n iterateAllJsdocs: true,\n meta: {\n docs: {\n description: '',\n url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-${ruleName}',\n },\n schema: [\n {\n additionalProperies: false,\n properties: {\n // Option properties here (or remove the object)\n },\n type: 'object',\n },\n ],\n type: 'suggestion',\n },\n});\n`;\n\n const camelCasedRuleName = _.camelCase(ruleName);\n\n const rulePath = `./src/rules/${camelCasedRuleName}.js`;\n\n if (!existsSync(rulePath)) {\n await fs.writeFile(rulePath, ruleTemplate);\n }\n\n const ruleTestTemplate = `export default {\n invalid: [\n {\n code: \\`\n \\`,\n errors: [{\n line: '',\n message: '',\n }],\n },\n ],\n valid: [\n {\n code: \\`\n \\`,\n },\n ],\n};\n`;\n\n const ruleTestPath = `./test/rules/assertions/${camelCasedRuleName}.js`;\n if (!existsSync(ruleTestPath)) {\n await fs.writeFile(ruleTestPath, ruleTestTemplate);\n }\n\n const ruleReadmeTemplate = `### \\`${ruleName}\\`\n\n|||\n|---|---|\n|Context|everywhere|\n|Tags|\\`\\`|\n|Recommended|${recommended ? 'true' : 'false'}|\n|Settings||\n|Options||\n\n<!-- assertions ${camelCasedRuleName} -->\n`;\n\n const ruleReadmePath = `./.README/rules/${ruleName}.md`;\n if (!existsSync(ruleReadmePath)) {\n await fs.writeFile(ruleReadmePath, ruleReadmeTemplate);\n }\n\n const replaceInOrder = async ({path, oldRegex, checkName, newLine, oldIsCamel}) => {\n const offsets = [];\n\n let readme = await fs.readFile(path, 'utf8');\n readme.replace(\n oldRegex,\n (matchedLine, n1, offset, str, {oldRule}) => {\n offsets.push({\n matchedLine,\n offset,\n oldRule,\n });\n },\n );\n\n offsets.sort(({oldRule}, {oldRule: oldRuleB}) => {\n // eslint-disable-next-line no-extra-parens\n return oldRule < oldRuleB ? -1 : (oldRule > oldRuleB ? 1 : 0);\n });\n\n let alreadyIncluded = false;\n const itemIndex = offsets.findIndex(({oldRule}) => {\n alreadyIncluded ||= oldIsCamel ? camelCasedRuleName === oldRule : ruleName === oldRule;\n\n return oldIsCamel ? camelCasedRuleName < oldRule : ruleName < oldRule;\n });\n let item = itemIndex !== undefined && offsets[itemIndex];\n if (item && itemIndex === 0 &&\n\n // This property would not always be sufficient but in this case it is.\n oldIsCamel\n ) {\n item.offset = 0;\n }\n if (!item) {\n item = offsets.pop();\n item.offset += item.matchedLine.length;\n }\n if (alreadyIncluded) {\n console.log(`Rule name is already present in ${checkName}.`);\n } else {\n readme = readme.slice(0, item.offset) +\n (item.offset ? '\\n' : '') +\n newLine +\n (item.offset ? '' : '\\n') +\n readme.slice(item.offset);\n\n await fs.writeFile(path, readme);\n }\n };\n\n await replaceInOrder({\n checkName: 'README',\n newLine: `{\"gitdown\": \"include\", \"file\": \"./rules/${ruleName}.md\"}`,\n oldRegex: /\\n\\{\"gitdown\": \"include\", \"file\": \".\\/rules\\/(?<oldRule>[^.]*).md\"\\}/gu,\n path: './.README/README.md',\n });\n\n await replaceInOrder({\n checkName: 'index import',\n newLine: `import ${camelCasedRuleName} from './rules/${camelCasedRuleName}';`,\n oldIsCamel: true,\n oldRegex: /\\nimport (?<oldRule>[^ ]*) from '.\\/rules\\/\\1';/gu,\n path: './src/index.js',\n });\n\n await replaceInOrder({\n checkName: 'index recommended',\n newLine: `${' '.repeat(8)}'jsdoc/${ruleName}': '${recommended ? 'warn' : 'off'}',`,\n oldRegex: /\\n\\s{8}'jsdoc\\/(?<oldRule>[^']*)': '[^']*',/gu,\n path: './src/index.js',\n });\n\n await replaceInOrder({\n checkName: 'index rules',\n newLine: `${' '.repeat(4)}'${ruleName}': ${camelCasedRuleName},`,\n oldRegex: /\\n\\s{4}'(?<oldRule>[^']*)': [^,]*,/gu,\n path: './src/index.js',\n });\n\n await import('./generateReadme.js');\n\n /*\n console.log('Paths to open for further editing\\n');\n console.log(`open ${ruleReadmePath}`);\n console.log(`open ${rulePath}`);\n console.log(`open ${ruleTestPath}\\n`);\n */\n\n // Set chdir as somehow still in operation from other test\n process.chdir(resolve(__dirname, '../../'));\n await open([\n // Could even add editor line column numbers like `${rulePath}:1:1`\n ruleReadmePath,\n ruleTestPath,\n rulePath,\n ]);\n})();\n"],"file":"generateRule.js"}
|