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

Useless insertBefore call #1677

Merged
merged 3 commits into from
Jan 24, 2019

Conversation

RunDevelopment
Copy link
Member

This PR removes the useless insertBefore call in Smarty.

It didn't do anything because Smarty did not have a tag token which causes insertBefore to just copy the whole language definition without doing anything.

Why did Smarty have this in the first place?
I'm guessing that it's a leftover from before markup templating.

I also added a little check to catch cases like this in the future (throw, technically).

@@ -107,6 +107,10 @@ var _ = _self.Prism = {
var grammar = root[inside];
var ret = {};

if (!grammar.hasOwnProperty(before)) {
throw new Error('Cannot insert before "' + before + '" in "' + inside + '"');
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think I'd include this check, tbh, since this is something that should be controlled / fixed at "develop-time", rather than runtime.

Copy link
Member Author

Choose a reason for hiding this comment

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

My only idea on how to check this at "develop-time" would be what I did here.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, what I meant was that we should be verifying as we develop that we're not including unnecessary calls to insertBefore. Otherwise, we're shopping extra bytes that don't actually do anything at runtime.

Copy link
Member Author

Choose a reason for hiding this comment

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

we should be verifying as we develop

Agreed, and the error is supposed to make all tests related to that language fail so that the dev notices that he/she broke something.

Example:

languages.foo = extend('bar', {});
insertBefore('foo', 'baz', { ... }); // 'baz' is defined in 'bar'

If somebody then removes bar, that will break foo without him/her ever knowing.
Sure, tests are supposed to prevent that from happening but I think it's better to check at the source of the error.

Also, another reason why I think that check is necessary: How do you expect insertBefore to behave when before is not present in root[inside]?
My first guess wouldn't have been: It just creates a deep copy of root[inside] and updates all references it can find.
I actually expect this to throw an error in that case.

Copy link
Member

Choose a reason for hiding this comment

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

The basic problem is that we're adding code to throw in a case that, if we code up the languages right, will never happen in production, so we're shipping code to end users that only benefits developers.

I understand the perspective of what's "right" for the function to do, but I think that only applies insofar as a function like this is primarily internal, rather than an API we are exposing to users.

Copy link
Member Author

Choose a reason for hiding this comment

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

Then an insertBefore.test (like here) might be the best solution.
It behaves "right" during development but doesn't include the checks for the users.

Copy link
Member

Choose a reason for hiding this comment

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

That would be better. Or a custom ESLint plugin that checks this (it should be verifiable statically).

Copy link
Member

Choose a reason for hiding this comment

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

If you're planning on going the ESLint route, let's just merge this without the check and implement that separately.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll just remove the check for now so we can merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants