Skip to content

Commit b8e2085

Browse files
committed
fix(check-examples): include column delta preceding regex mtch when regex has no groups
testing: bump mocha timeout to 5000ms (one test needs to retrieve a file) testing(check-examples): increase coverage (`matchingFileName` and regex with no groups)
1 parent cdc3048 commit b8e2085

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps",
6565
"create-readme": "gitdown ./.README/README.md --output-file ./README.md && npm run add-assertions",
6666
"lint": "eslint ./src ./test",
67-
"test-cov": "BABEL_ENV=test nyc mocha --recursive --require @babel/register --reporter progress",
68-
"test": "BABEL_ENV=test nyc --reporter text-summary mocha --recursive --require @babel/register --reporter progress"
67+
"test-cov": "BABEL_ENV=test nyc mocha --recursive --require @babel/register --reporter progress --timeout 5000",
68+
"test": "BABEL_ENV=test nyc --reporter text-summary mocha --recursive --require @babel/register --reporter progress --timeout 5000"
6969
},
7070
"nyc": {
7171
"require": [

src/rules/checkExamples.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,17 @@ export default iterateJsdoc(({
108108
source = source.slice(idx);
109109

110110
source = source.replace(exampleCodeRegex, (n0, n1) => {
111-
if (!n1) {
112-
return n0;
111+
let nonJSPreface;
112+
let nonJSPrefaceLineCount;
113+
if (n1) {
114+
const index = n0.indexOf(n1);
115+
nonJSPreface = n0.slice(0, index);
116+
nonJSPrefaceLineCount = countChars(nonJSPreface, '\n');
117+
} else {
118+
nonJSPreface = '';
119+
nonJSPrefaceLineCount = 0;
113120
}
114121

115-
const index = n0.indexOf(n1);
116-
const nonJSPreface = n0.slice(0, index);
117-
const nonJSPrefaceLineCount = countChars(nonJSPreface, '\n');
118-
119122
nonJSPrefacingLines += nonJSPrefaceLineCount;
120123

121124
// Ignore `preMatch` delta if newlines here
@@ -127,7 +130,7 @@ export default iterateJsdoc(({
127130
nonJSPrefacingCols += colDelta + nonJSPreface.length;
128131
}
129132

130-
return n1;
133+
return n1 || n0;
131134
});
132135
}
133136

test/rules/assertions/checkExamples.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,55 @@ export default {
297297
noDefaultExampleRules: true
298298
}
299299
}
300+
},
301+
{
302+
code: `
303+
/**
304+
* @example quux2()
305+
*/
306+
function quux2 () {
307+
308+
}
309+
`,
310+
errors: [
311+
{
312+
message: '@example error (semi): Missing semicolon.'
313+
}
314+
],
315+
settings: {
316+
jsdoc: {
317+
matchingFileName: 'test/jsdocUtils.js'
318+
}
319+
}
320+
},
321+
{
322+
code: `
323+
/**
324+
* @example // begin
325+
alert('hello')
326+
// end
327+
*/
328+
function quux () {
329+
330+
}
331+
`,
332+
errors: [
333+
{
334+
message: '@example warning (semi): Missing semicolon.'
335+
}
336+
],
337+
settings: {
338+
jsdoc: {
339+
baseConfig: {
340+
rules: {
341+
semi: ['warn', 'always']
342+
}
343+
},
344+
eslintrcForExamples: false,
345+
exampleCodeRegex: '// begin[\\s\\S]*// end',
346+
noDefaultExampleRules: true
347+
}
348+
}
300349
}
301350
],
302351
valid: [

0 commit comments

Comments
 (0)