{"version":3,"sources":["../../src/rules/checkExamples.js"],"names":["zeroBasedLineIndexAdjust","likelyNestedJSDocIndentSpace","preTagSpaceLength","hasCaptionRegex","escapeStringRegexp","str","replace","countChars","ch","match","RegExp","length","jsdoc","report","utils","exampleCodeRegex","getExampleCodeRegex","rejectExampleCodeRegex","getRejectExampleCodeRegex","noDefaultExampleRules","hasNoDefaultExampleRules","eslintrcForExamples","useEslintrcForExamples","filename","getMatchingFileName","baseConfig","getBaseConfig","configFile","getConfigFile","allowInlineConfig","reportUnusedDisableDirectives","rulePaths","rules","undefined","_","forEach","tags","tag","initialTag","source","initialTagLength","firstLinePrefixLength","slice","isCaptionRequired","test","nonJSPrefacingLines","nonJSPrefacingCols","idx","search","preMatchLines","preMatch","colDelta","lastIndexOf","n0","n1","index","indexOf","nonJSPreface","nonJSPrefaceLineCount","charsInLastLine","cli","CLIEngine","useEslintrc","messages","config","getConfigForFile","linter","Linter","linterRules","getRules","entries","reduce","obj","key","val","defineRules","verify","executeOnText","results","codeStartLine","line","codeStartCol","message","column","severity","ruleId","startLine","startCol"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;AAEA,IAAMA,wBAAwB,GAAG,CAAC,CAAlC;AACA,IAAMC,4BAA4B,GAAG,CAArC;AACA,IAAMC,iBAAiB,GAAG,CAA1B;AACA,IAAMC,eAAe,GAAG,6BAAxB;;AAEA,IAAMC,kBAAkB,GAAG,SAArBA,kBAAqB,CAACC,GAAD,EAAS;AAClC,SAAOA,GAAG,CAACC,OAAJ,CAAY,qBAAZ,EAAmC,MAAnC,CAAP;AACD,CAFD;;AAGA,IAAMC,UAAU,GAAG,SAAbA,UAAa,CAACF,GAAD,EAAMG,EAAN,EAAa;AAC9B,SAAO,CAACH,GAAG,CAACI,KAAJ,CAAU,IAAIC,MAAJ,CAAWN,kBAAkB,CAACI,EAAD,CAA7B,EAAmC,GAAnC,CAAV,KAAsD,EAAvD,EAA2DG,MAAlE;AACD,CAFD;;eA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Y,KAE3B;AACAD,MAAAA,IAAI,IAAI,CAAR,GAAYlC,kBAAkB,GAAGL,qBAAjC,GAAyDvC,iBAH9B,CAAZ,GAIbiF,MAJJ,CAF8D,CAQ9D;;AACAtE,MAAAA,MAAM,CACJ,eAAeuE,QAAQ,KAAK,CAAb,GAAiB,OAAjB,GAA2B,SAA1C,KACGC,MAAM,GAAG,OAAOA,MAAP,GAAgB,GAAnB,GAAyB,EADlC,IACwC,IADxC,GAEEH,OAHE,EAIJ,IAJI,EAKJ;AACEC,QAAAA,MAAM,EAAEI,QADV;AAEEP,QAAAA,IAAI,EAAEM;AAFR,OALI,CAAN;AAUD,KAnBD;AAoBD,GAnID;AAoID,CApLc,C","sourcesContent":["import _ from 'lodash';\nimport {CLIEngine, Linter} from 'eslint';\nimport iterateJsdoc from '../iterateJsdoc';\n\nconst zeroBasedLineIndexAdjust = -1;\nconst likelyNestedJSDocIndentSpace = 1;\nconst preTagSpaceLength = 1;\nconst hasCaptionRegex = /^\\s*.*?<\\/caption>/;\n\nconst escapeStringRegexp = (str) => {\n return str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n};\nconst countChars = (str, ch) => {\n return (str.match(new RegExp(escapeStringRegexp(ch), 'g')) || []).length;\n};\n\nexport default iterateJsdoc(({\n jsdoc,\n report,\n utils\n}) => {\n let exampleCodeRegex = utils.getExampleCodeRegex();\n let rejectExampleCodeRegex = utils.getRejectExampleCodeRegex();\n const noDefaultExampleRules = utils.hasNoDefaultExampleRules();\n const eslintrcForExamples = utils.useEslintrcForExamples();\n const filename = utils.getMatchingFileName();\n const baseConfig = utils.getBaseConfig();\n const configFile = utils.getConfigFile();\n const allowInlineConfig = utils.allowInlineConfig();\n const reportUnusedDisableDirectives = utils.reportUnusedDisableDirectives();\n\n // Make this configurable?\n const rulePaths = [];\n\n const rules = noDefaultExampleRules ? undefined : {\n // \"always\" newline rule at end unlikely in sample code\n 'eol-last': 0,\n\n // Wouldn't generally expect example paths to resolve relative to JS file\n 'import/no-unresolved': 0,\n\n // Snippets likely too short to always include import/export info\n 'import/unambiguous': 0,\n\n // Unlikely to have inadvertent debugging within examples\n 'no-console': 0,\n\n // Many variables in examples will be `undefined`\n 'no-undef': 0,\n\n // Common to define variables for clarity without always using them\n 'no-unused-vars': 0,\n\n // See import/no-unresolved\n 'node/no-missing-import': 0,\n 'node/no-missing-require': 0,\n\n // Can generally look nicer to pad a little even if code imposes more stringency\n 'padded-blocks': 0\n };\n\n exampleCodeRegex = exampleCodeRegex && new RegExp(exampleCodeRegex, '');\n rejectExampleCodeRegex = rejectExampleCodeRegex && new RegExp(rejectExampleCodeRegex, '');\n\n _.forEach(jsdoc.tags, (tag) => {\n if (tag.tag !== 'example') {\n return;\n }\n\n // If a space is present, we should ignore it\n const initialTag = tag.source.match(/^@example ?/);\n const initialTagLength = initialTag[0].length;\n const firstLinePrefixLength = preTagSpaceLength + initialTagLength;\n\n let source = tag.source.slice(initialTagLength);\n const match = source.match(hasCaptionRegex);\n\n if (utils.isCaptionRequired() && !match) {\n report('Caption is expected for examples.', null, tag);\n }\n\n // If we allow newlines in hasCaptionRegex, we should add to line count\n source = source.replace(hasCaptionRegex, '');\n\n if (exampleCodeRegex && !exampleCodeRegex.test(source) ||\n rejectExampleCodeRegex && rejectExampleCodeRegex.test(source)\n ) {\n return;\n }\n\n let nonJSPrefacingLines = 0;\n let nonJSPrefacingCols = 0;\n\n if (exampleCodeRegex) {\n const idx = source.search(exampleCodeRegex);\n\n // Strip out anything preceding user regex match (can affect line numbering)\n let preMatchLines = 0;\n\n const preMatch = source.slice(0, idx);\n\n preMatchLines = countChars(preMatch, '\\n');\n\n nonJSPrefacingLines = preMatchLines;\n\n const colDelta = preMatchLines ?\n preMatch.slice(preMatch.lastIndexOf('\\n') + 1).length - initialTagLength :\n preMatch.length;\n\n // Get rid of text preceding user regex match (even if it leaves valid JS, it\n // could cause us to count newlines twice)\n source = source.slice(idx);\n\n source = source.replace(exampleCodeRegex, (n0, n1) => {\n if (!n1) {\n return n0;\n }\n\n const index = n0.indexOf(n1);\n const nonJSPreface = n0.slice(0, index);\n const nonJSPrefaceLineCount = countChars(nonJSPreface, '\\n');\n\n nonJSPrefacingLines += nonJSPrefaceLineCount;\n\n // Ignore `preMatch` delta if newlines here\n if (nonJSPrefaceLineCount) {\n const charsInLastLine = nonJSPreface.slice(nonJSPreface.lastIndexOf('\\n') + 1).length;\n\n nonJSPrefacingCols += charsInLastLine - initialTagLength;\n } else {\n nonJSPrefacingCols += colDelta + nonJSPreface.length;\n }\n\n return n1;\n });\n }\n\n // Programmatic ESLint API: https://eslint.org/docs/developer-guide/nodejs-api\n const cli = new CLIEngine({\n allowInlineConfig,\n baseConfig,\n configFile,\n reportUnusedDisableDirectives,\n rulePaths,\n rules,\n useEslintrc: eslintrcForExamples\n });\n\n let messages;\n\n if (filename) {\n const config = cli.getConfigForFile(filename);\n const linter = new Linter();\n\n const linterRules = [...cli.getRules().entries()].reduce((obj, [key, val]) => {\n obj[key] = val;\n\n return obj;\n }, {});\n\n linter.defineRules(linterRules);\n\n messages = linter.verify(source, config, {\n filename,\n reportUnusedDisableDirectives\n });\n } else {\n ({results: [{messages}]} =\n cli.executeOnText(source));\n }\n\n // NOTE: `tag.line` can be 0 if of form `/** @tag ... */`\n const codeStartLine = tag.line + nonJSPrefacingLines;\n const codeStartCol = likelyNestedJSDocIndentSpace;\n\n messages.forEach(({message, line, column, severity, ruleId}) => {\n const startLine = codeStartLine + line + zeroBasedLineIndexAdjust;\n const startCol = codeStartCol + (\n\n // This might not work for line 0, but line 0 is unlikely for examples\n line <= 1 ? nonJSPrefacingCols + firstLinePrefixLength : preTagSpaceLength\n ) + column;\n\n // Could perhaps make fixable\n report(\n '@example ' + (severity === 2 ? 'error' : 'warning') +\n (ruleId ? ' (' + ruleId + ')' : '') + ': ' +\n message,\n null,\n {\n column: startCol,\n line: startLine\n }\n );\n });\n });\n});\n"],"file":"checkExamples.js"}