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

Fix: yield* should throw if not iterable #636

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/runtime/runtime.js
Expand Up @@ -487,7 +487,7 @@ var runtime = (function (exports) {
};

function values(iterable) {
if (iterable) {
if (iterable || iterable === "") {
var iteratorMethod = iterable[iteratorSymbol];
if (iteratorMethod) {
return iteratorMethod.call(iterable);
Expand Down Expand Up @@ -517,8 +517,7 @@ var runtime = (function (exports) {
}
}

// Return an iterator with no values.
return { next: doneResult };
throw new TypeError(typeof iterable + " is not iterable");
}
exports.values = values;

Expand Down
22 changes: 22 additions & 0 deletions test/tests.es6.js
Expand Up @@ -1504,6 +1504,28 @@ describe("delegated yield", function() {
check(gen(iterator), [], "1foo");
});

it("should work with empty string", function() {
function* f() {
yield* "";
};

assert.deepEqual(f().next(), { value: undefined, done: true });
});

it("should throw if not iterable", function() {
function* f(x) {
yield* x;
};

assert.throws(() => f(undefined).next(), TypeError);
assert.throws(() => f(null).next(), TypeError);
assert.throws(() => f(false).next(), TypeError);
assert.throws(() => f(true).next(), TypeError);
assert.throws(() => f(0).next(), TypeError);
assert.throws(() => f(1).next(), TypeError);
assert.throws(() => f({}).next(), TypeError);
});

it("should throw if the delegated iterable's iterator doesn't have .next", function() {
var it = function* () {
yield* {
Expand Down