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

ordered-list-marker-value: fix to support 0 as start #219

Merged
merged 5 commits into from
Oct 27, 2019

Conversation

alexandrtovmach
Copy link
Contributor

@alexandrtovmach alexandrtovmach commented Oct 22, 2019

Here's small patch that allow to use 0 as start value in ordered list for https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-ordered-list-marker-value rule.

Why is that need? Because lists like that should be valid:

0. zero
1. one
2. two
3. three

but it's not with current implementation:

Marker should be 2, was 1 ordered-list-marker-value remark-lint

Example

I'm working now on remark lint preset for Gatsby docs and faced with this issue here:
image

That's not a BREAKING CHANGE and tests seems okay

@codecov-io

This comment has been minimized.

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would mostly work! I would feel a bit more comfortable that this isn’t a breaking change with something that checks if node.start is null/undefined, something like this (if allowed by xo/prettier though):

    var shouldBe = pref === 'one' ? 1 : node.start == null ? 1 : node.start

@alexandrtovmach
Copy link
Contributor Author

@wooorm Not sure that I understand your point =) I just tried to avoid some critical changes, and simply replaced start value. Do you want me to refactor this like you said?

@wooorm
Copy link
Member

wooorm commented Oct 22, 2019

Yes, that’s what I meant!

With your proposal, if node.start is undefined or null, it should be 0. With my suggestion, that case should be 1 instead. But both allow node.start to be 0 (which you are solving)!

@alexandrtovmach
Copy link
Contributor Author

@wooorm Updated with strict equality instead of node.start == null. But... I still not sure about that, because new logic is can't handle all cases like NaN and empty string:

var values = [undefined, null, 0, 1, NaN, ''];
var pref = 'one';

function test1(valuesArr) {
	return valuesArr.map(el => (pref === 'one' ? 1 : el) || 0)
};


function test2(valuesArr) {
	function getStartValue(value) {
        if (typeof value !== "undefined" && value !== null) {
            return value
        } else {
            return 0;
        }
    }
    return valuesArr.map(el => pref === 'one' ? 1 : getStartValue(el))
};

console.log(test1(values)); // [1, 1, 1, 1, 1, 1]
console.log(test2(values)); // [1, 1, 1, 1, 1, 1]
var pref = 'ordered';
console.log(test1(values)); // [0, 0, 0, 1, 0, 0]
console.log(test2(values)); // [0, 0, 0, 1, NaN, ""]

@alexandrtovmach
Copy link
Contributor Author

@wooorm Could you take a look?

@alexandrtovmach
Copy link
Contributor Author

@wooorm Friendly ping

@wooorm
Copy link
Member

wooorm commented Oct 26, 2019

Thanks for the pings and sorry for not getting back to you sooner!

@wooorm
Copy link
Member

wooorm commented Oct 26, 2019

We don’t have to care about weird values, because node.start should be a number, but it is optional! So null and undefined are both allowed, according to mdast.

Here is a script I wrote to compare the three algorithms. Algorithm A is the one I proposed, B is your earlier proposal, and C your latest proposal

var values = [null, undefined, 0, 1] // Values that `node.start` could have
var preferences = ['single', 'ordered', 'one'] // Options for the rule

preferences.forEach(p => {
  console.log('\npreference: %s', p)

  values.forEach(function(d) {
    var a = p === 'one' ? 1 : d == null ? 1 : d
    var b = (p === 'one' ? 1 : d) || 0
    var c = p === 'one' ? 1 : typeof d !== 'undefined' && d !== null ? d : 0

    console.log('%s: %s | %s | %s', String(d).padStart(10), a, b, c)
  })
})

This gives the result:

preference: single
      null: 1 | 0 | 0
 undefined: 1 | 0 | 0
         0: 0 | 0 | 0
         1: 1 | 1 | 1

preference: ordered
      null: 1 | 0 | 0
 undefined: 1 | 0 | 0
         0: 0 | 0 | 0
         1: 1 | 1 | 1

preference: one
      null: 1 | 1 | 1
 undefined: 1 | 1 | 1
         0: 1 | 1 | 1
         1: 1 | 1 | 1

In these results, we can ignore the one preferences. Those are all the same.
We can also ignore single OR ordered, because they have the same results. So now we just care about, let’s say, single.

Your algorithms now all prefer to start with 0. This may be a breaking change because sometimes nodes don’t have a start, and used to result in a 1.
With my proposed algorithm A, null and undefined are treated as before (1), whereas only an explicit 0 results in a 0!

@alexandrtovmach
Copy link
Contributor Author

@wooorm Thank you for detailed explanation. Updated code with your algorithm

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, this is great!

One more nit 😬 Could you add a test? Basically these and these lines, above or below those, but (starting) with 0s? Thanks!

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! 😊

@wooorm wooorm merged commit 72529df into remarkjs:master Oct 27, 2019
@alexandrtovmach alexandrtovmach deleted the patch/allow-zero-start-for-ol branch October 27, 2019 19:01
@wooorm wooorm changed the title Updated validation to allow 0 as start value for ordered list ordered-list-marker-value: fix to support 0 as start Oct 27, 2019
@wooorm wooorm added 🐛 type/bug This is a problem 👶 semver/patch This is a backwards-compatible fix 🗄 area/interface This affects the public interface 💪 phase/solved Post is done labels Oct 27, 2019
@alexandrtovmach
Copy link
Contributor Author

@wooorm I can't find this changes on NPM. Does it released?

@wooorm
Copy link
Member

wooorm commented Dec 1, 2019

@wooorm wooorm removed the 💪 phase/solved Post is done label Dec 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 👶 semver/patch This is a backwards-compatible fix 🐛 type/bug This is a problem
Development

Successfully merging this pull request may close these issues.

3 participants