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

Make URLSearchParams compliant with the spec and browser #1055

Closed
wants to merge 1 commit into from

Conversation

ztplz
Copy link
Contributor

@ztplz ztplz commented Oct 21, 2018

According to MDN Docs keys, values and entries shouldn't a generator function, and need to return a Iterator.
For example:
in chrome and firefox:

var us = new URLSearchParams()
undefined
console.log(us.keys.constructor.name)
Function
undefined
console.log(us.values.constructor.name)
Function
undefined
console.log(us.entries.constructor.name)
Function

@kyranet
Copy link
Contributor

kyranet commented Oct 21, 2018

This needs tests to ensure this is spec compliant now

@ztplz
Copy link
Contributor Author

ztplz commented Oct 21, 2018

@kyranet Sure.
Output in chrome:

var us = new URLSearchParams
undefined
var keys = us.keys()
undefined
Symbol.iterator in keys
true
Object.prototype.toString.call(keys)
"[object Iterator]"

3 similar comments
@ztplz
Copy link
Contributor Author

ztplz commented Oct 21, 2018

@kyranet Sure.
Output in chrome:

var us = new URLSearchParams
undefined
var keys = us.keys()
undefined
Symbol.iterator in keys
true
Object.prototype.toString.call(keys)
"[object Iterator]"

@ztplz
Copy link
Contributor Author

ztplz commented Oct 21, 2018

@kyranet Sure.
Output in chrome:

var us = new URLSearchParams
undefined
var keys = us.keys()
undefined
Symbol.iterator in keys
true
Object.prototype.toString.call(keys)
"[object Iterator]"

@ztplz
Copy link
Contributor Author

ztplz commented Oct 21, 2018

@kyranet Sure.
Output in chrome:

var us = new URLSearchParams
undefined
var keys = us.keys()
undefined
Symbol.iterator in keys
true
Object.prototype.toString.call(keys)
"[object Iterator]"

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM

@kitsonk
Copy link
Contributor

kitsonk commented Oct 22, 2018

Generators are iterators. Generators are functions that codify iterators. Something like:

class Foo {
  *keys() {
    yield "foo";
    yield "bar";
  }
}

const foo = new Foo();
const keys = foo.keys();
console.log(Symbol.iterator in keys); // true

Is exactly the spec functionality, except that it logs out [object Generator] instead of [object Iterator] which I really argue is not something we should be concerned with, given that you get all the iterator functionality that is needed.

I think we need to align to what is being done in #1062 so until #1062 settles down and this gets realigned to that, we should hold off merging.

@ztplz
Copy link
Contributor Author

ztplz commented Oct 22, 2018

@kitsonk Okay, I see. Thanks for your explanation.

@ry
Copy link
Member

ry commented Oct 23, 2018

@ztplz @kitsonk Whats the status on this? It looks like it's not in the style Kitson is recommending?

@kitsonk
Copy link
Contributor

kitsonk commented Oct 24, 2018

@ry correct, it needs to follow the pattern established in #1062

@hayd
Copy link
Contributor

hayd commented Oct 24, 2018

and it needs tests

Copy link
Contributor

@kitsonk kitsonk left a comment

Choose a reason for hiding this comment

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

A few comments...

js/dom_types.ts Outdated
@@ -90,7 +90,7 @@ export interface ProgressEventInit extends EventInit {
total?: number;
}

export interface URLSearchParams {
export interface URLSearchParams extends DomIterable<string, string>{
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this has been through the formatter again. Prettier would have inserted a space before the opening brace.

js/globals.ts Outdated
@@ -6,7 +6,7 @@ import { globalEval } from "./global_eval";
import { libdeno } from "./libdeno";
import * as textEncoding from "./text_encoding";
import * as timers from "./timers";
import * as urlSearchParams from "./url_search_params";
import * as urlSearchParams_ from "./url_search_params";
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need to underscore this... it is only needed when there is a name collision in the global namespace, (e.g. console or fetch). Since there is no global variable named urlSearchParams it is safe to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I just thought it was the same as the browser.

@@ -24,19 +24,19 @@ export function DomIterableMixin<K, V, TBase extends Constructor>(
// tslint:disable-next-line:variable-name
const DomIterable = class extends Base {
*entries(): IterableIterator<[K, V]> {
for (const entry of (this as any)[dataSymbol].entries()) {
for (const entry of (this as any)[dataSymbol]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is also in #1056. Maybe the best thing to do is create a seperate change that includes this that both #1056 and this one can base off of.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think need to create a seperate change in here. for const entry of Array<k, v> as same as for const entry of Map<k, v>.

yield entry;
}
}

*keys(): IterableIterator<K> {
for (const key of (this as any)[dataSymbol].keys()) {
for (const [key,] of (this as any)[dataSymbol]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

#1056 has this as:

Suggested change
for (const [key,] of (this as any)[dataSymbol]) {
for (const [key] of (this as any)[dataSymbol]) {

which is better

export const URLSearchParams = DomIterableMixin<string, string, typeof URLSearchParamsBase>(
URLSearchParamsBase,
params
);
Copy link
Contributor

Choose a reason for hiding this comment

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

not run through the formatter or this would have been changed to break before EOF as well as on line 146 lacking spacing before the opening parenthesis.

.map(
tuple =>
`${encodeURIComponent(tuple[0])}=${encodeURIComponent(tuple[1])}`
)
.join("&");
}
}

// @internal
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be no need for @internal here.


// @internal
// tslint:disable:max-line-length
// tslint:disable-next-line:variable-name
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// tslint:disable-next-line:variable-name
// tslint:disable-next-line:variable-name max-line-length

But even then, Prettier should break the generic arguments up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

So yeah, now with the wrapping after running through prettier, max-line-length is redundant.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

This was preemptively approved. Please address @kitsonk's comments.

Copy link
Contributor

@kitsonk kitsonk left a comment

Choose a reason for hiding this comment

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

One minor nit, but other than that LGTM.

Also, it should cleanly apply, but we have overlapping changes with #1056.


// @internal
// tslint:disable:max-line-length
// tslint:disable-next-line:variable-name
Copy link
Contributor

Choose a reason for hiding this comment

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

So yeah, now with the wrapping after running through prettier, max-line-length is redundant.

@ztplz
Copy link
Contributor Author

ztplz commented Oct 29, 2018

@kitsonk Yes, the problem is think about how to reuse.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM - thank you - and sorry for the delay! Having some issues with appveyor build but will land once those are settled. I've rebased and squashed the branch.

@ry
Copy link
Member

ry commented Jan 9, 2019

Closing old PRs. Please open a new one if this hasn't been resolved yet.

@ry ry closed this Jan 9, 2019
@hayd
Copy link
Contributor

hayd commented Jan 9, 2019

Looks like #1049 superseded this anyway?

@kyranet
Copy link
Contributor

kyranet commented Jan 9, 2019

Looks like #1049 superseded this anyway?

No, #1049 is the original implementation, this PR fixed a bug where iterators aren't spec compliant.

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

Successfully merging this pull request may close these issues.

None yet

5 participants