-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
error if invalid 'before' argument is passed to Map#addLayer #5401
Conversation
2371f4e
to
429c8c9
Compare
src/style/style.js
Outdated
@@ -568,7 +568,10 @@ class Style extends Evented { | |||
|
|||
layer.setEventedParent(this, {layer: {id: id}}); | |||
|
|||
const index = before ? this._order.indexOf(before) : this._order.length; | |||
const beforeIndex = before ? this._order.indexOf(before) : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mollymerp is 0
the top or bottom of the stack? Hitting map.style._order
in my console, the first layer in the array is visually under all other layers on the map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this is misleading – beforeIndex
isn't actually used unless before
exists so the 0
value never gets assigned to index
. I made it 0 instead of null
to appease flow but I could make this clearer.
429c8c9
to
73b2a80
Compare
src/style/style.js
Outdated
|
||
const beforeIndex = before ? this._order.indexOf(before) : -1; | ||
const index = before && beforeIndex !== -1 ? beforeIndex : this._order.length; | ||
if (before && beforeIndex === -1) util.warnOnce(`Layer with id "${before}" does not exist on this map. Adding layer to the top of the stack.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's report this via this.fire('error', ...)
for consistency with non-existent layer IDs in moveLayer
, removeLayer
, etc.
test/unit/style/style.test.js
Outdated
}] | ||
})); | ||
const layer = {id: 'c', type: 'background'}; | ||
t.stub(console, 'error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bind a handler for the error
event rather than stubbing console.error
(see the subsequent test for example).
src/style/style.js
Outdated
|
||
const beforeIndex = before ? this._order.indexOf(before) : -1; | ||
const index = before && beforeIndex !== -1 ? beforeIndex : this._order.length; | ||
if (before && beforeIndex === -1) this.fire('error', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding to the top of the stack, return early without making changes in the case of an error.
src/style/style.js
Outdated
@@ -568,7 +568,12 @@ class Style extends Evented { | |||
|
|||
layer.setEventedParent(this, {layer: {id: id}}); | |||
|
|||
const index = before ? this._order.indexOf(before) : this._order.length; | |||
|
|||
const beforeIndex = before ? this._order.indexOf(before) : -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this._order.length
can never be -1
, so the conditionals can be simplified:
const index = before ? this._order.indexOf(before) : this._order.length;
if (index === -1) {
// error
}
this._order.splice(index, ...);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah good point! I still had to use if (before && index === -1)
for flow to allow me to include before
in the error message.
dec81e9
to
8c022d3
Compare
8c022d3
to
6dcc1ab
Compare
fix #3868
Launch Checklist