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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.


for (var token in grammar) {
if (grammar.hasOwnProperty(token)) {

Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions components/prism-smarty.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@
'keyword': /\b(?:false|off|on|no|true|yes)\b/
};

// Comments are inserted at top so that they can
// surround markup
Prism.languages.insertBefore('smarty', 'tag', {
'smarty-comment': {
pattern: /\{\*[\s\S]*?\*\}/,
alias: ['smarty','comment']
}
});

// Tokenize all inline Smarty expressions
Prism.hooks.add('before-tokenize', function(env) {
var smartyPattern = /\{\*[\s\S]*?\*\}|\{[\s\S]+?\}/g;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-smarty.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ var _ = _self.Prism = {
var grammar = root[inside];
var ret = {};

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

for (var token in grammar) {
if (grammar.hasOwnProperty(token)) {

Expand Down