Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix trim bug in trimCode #342

Merged
merged 3 commits into from Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -388,15 +388,15 @@ function quux (foo) {
}
// Message: Expected JSDoc block to be aligned.

/**
/**
* @param {Number} foo
*/
function quux (foo) {

}
// Message: Expected JSDoc block to be aligned.

/**
/**
* @param {Number} foo
*/
function quux (foo) {
Expand Down Expand Up @@ -3410,7 +3410,7 @@ An option object may have the following key:
The following patterns are considered problems:

````js
/**
/**
* @param {HerType} baz - Foo.
*/
function quux(foo, bar, baz) {
Expand All @@ -3419,7 +3419,7 @@ function quux(foo, bar, baz) {
// Settings: {"jsdoc":{"preferredTypes":{"HerType":1000}}}
// Message: Invalid `settings.jsdoc.preferredTypes`. Values must be falsy, a string, or an object.

/**
/**
* @param {HerType} baz - Foo.
*/
function quux(foo, bar, baz) {
Expand All @@ -3446,7 +3446,7 @@ function quux(foo, bar) {
// Options: [{"definedTypes":["MyType"]}]
// Message: The type 'HisType' is undefined.

/**
/**
* @param {MyType} foo - Bar.
* @param {HisType} bar - Foo.
* @param {HerType} baz - Foo.
Expand All @@ -3458,7 +3458,7 @@ function quux(foo, bar, baz) {
// Options: [{"definedTypes":["MyType"]}]
// Message: The type 'HisType' is undefined.

/**
/**
* @param {MyType} foo - Bar.
* @param {HisType} bar - Foo.
* @param {HerType} baz - Foo.
Expand Down Expand Up @@ -3642,7 +3642,7 @@ function quux(foo, bar) {
}
// Options: [{"definedTypes":["MyType","HisType"]}]

/**
/**
* @param {MyType} foo - Bar.
* @param {HisType} bar - Foo.
* @param {HerType} baz - Foo.
Expand All @@ -3653,7 +3653,7 @@ function quux(foo, bar, baz) {
// Settings: {"jsdoc":{"preferredTypes":{"hertype":{"replacement":"HerType"},"histype":"HisType"}}}
// Options: [{"definedTypes":["MyType"]}]

/**
/**
* @param {MyType} foo - Bar.
* @param {HisType} bar - Foo.
* @param {HerType} baz - Foo.
Expand Down
12 changes: 7 additions & 5 deletions src/bin/readme-assertions.js
Expand Up @@ -7,18 +7,20 @@ import _ from 'lodash';
import glob from 'glob';

const trimCode = (code) => {
let lines = code.trim().split('\n');
let lines = code.replace(/^\n/, '').trimEnd().split('\n');

const indendation = lines[lines.length - 1].match(/^\s+/);
const firsLineIndentation = lines[0].match(/^\s+/);
const lastLineIndentation = lines[lines.length - 1].match(/^\s+/);

const indentSize = indendation ? indendation[0].length : 0;
const firstIndentSize = firsLineIndentation ? firsLineIndentation[0].length : 0;
const lastIndentSize = lastLineIndentation ? lastLineIndentation[0].length : 0;

lines = lines.map((line, index) => {
if (index === 0) {
return line;
return line.slice(Math.min(firstIndentSize, lastIndentSize));
}

return line.slice(indentSize);
return line.slice(lastIndentSize);
});

return lines.join('\n');
Expand Down
2 changes: 1 addition & 1 deletion test/rules/assertions/checkExamples.js
Expand Up @@ -637,7 +637,7 @@ export default {
},
{
code: `
/**
/**
* @example
* foo(function (err) {
* throw err;
Expand Down