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

Linting event emitter returned by API can emit done before user listens it #103

Closed
mondeja opened this issue May 15, 2024 · 0 comments · Fixed by #107
Closed

Linting event emitter returned by API can emit done before user listens it #103

mondeja opened this issue May 15, 2024 · 0 comments · Fixed by #107
Labels

Comments

@mondeja
Copy link
Member

mondeja commented May 15, 2024

According to the example of the README, you must call lintSource to return a Linting instance and then listen the done event on that to check that the linting has been terminated. The problem is that Linting can have another state different to linting at that point.

These two tests are reproducing the behaviour:

it('should succeed when disabled for a valid SVG', function () {
return testSucceeds('<svg></svg>', {rules: {valid: false}});
});
it('should succeed when disabled for an invalid SVG', function () {
return testSucceeds(
`<svg><path "M20.013 10.726l.001-.028A6.346"/></svg>`,
{rules: {valid: false}},
);
});

That's the reason why I added this check to the tester before calling on('done', ... ):

svglint/test/helpers.js

Lines 25 to 38 in 3ce8e62

// TODO: there is a race condition here. The this.lint() method
// of the Linting class is called in the constructor, so it's possible
// that the linting is already done before we call the on('done')
// event listener. Removing the next condition will make some `valid`
// rules tests fail.
if (linting.state === linting.STATES.success) {
resolve();
} else if (linting.state !== linting.STATES.linting) {
reject(
new Error(
`Linting failed (${linting.state}): ${inspect(config)}`,
),
);
}

Is a race condition that happens because Linting.lint() is called at Linting constructor and can call Linting.emit('done') before the user listen the event. The most suitable solution is to not call this.lint() on Linting constructor and force to the user to call it after setting event listeners. This is a breaking change.

@mondeja mondeja added the bug label May 15, 2024
@mondeja mondeja changed the title Linting event emitter returned by API can emit done before listen Linting event emitter returned by API can emit done before user listens it May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant