7 ms·
I got to 14 (now 15!) in javascript so far: async function* foo() { return yield delete void await null in this instanceof typeof new class extends
by bluepnume 8y ago
I got to 14 (now 15!) in javascript so far:
async function* foo() {
return yield delete void await null in this instanceof typeof new class extends async function () {} {}
}
(not sure whether the code needs to successfully run? But it at least parses)
What's really fun is seeing how babel transpiled the above function:
https://babeljs.io/repl/#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=IYZwngdgxgBAZgV2gFwJYHsICp7vQCgEoYBvAKBkqsoCcBTZBGiGMVOgGwBMYvOG6MYAHdgqZDAgIOHGKhbIAFqhByIIZMGh10cGMjAAHHXoh1hMKB1Cq6AD2R0IXVaEixEKDCyKkAvv4UVH5AA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=es2015%2Ces2016%2Ces2017%2Creact%2Cstage-0%2Cstage-1%2Cstage-2%2Cstage-3%2Ces2015-loose&prettier=false&targets=&version=6.26.0&envVersion= https://babeljs.io/repl/#?babili=false&browsers=&build=&buil...
EDIT: here's an explanation https://medium.com/@bluepnume/how-many-keywords-can-you-place-in-a-row-in-javascript-fa9046b4c1ff https://medium.com/@bluepnume/how-many-keywords-can-you-plac...
- goatlover 8y agoIt runs successfully in Chrome returning: foo {<suspended>} That's the craziest statement I've ever seen!
- jastanton 8y agoThat's because a generator is being returned from `foo` (the yield syntax gives you this). Try calling next() on the return value of foo. I get: Promise {<rejected>: TypeError: Class extends value async function () {} is not a constructor or null at foo
- deathanatos 8y agoWell, you are correct that that parses. But how? I'm with it right up to around "new class extends"; `new` I'm okay with, but doesn't `new` require a symbol (the name of the type to allocate) after it? I tried parsing "class extends async function" as an expression that boiled down to a type that we could new, but I get lost at "class extends" — don't we need a class name there? (Also, I didn't think `class` could be used in an expression…)
- goatlover 8y agoDoes it mean that you're creating an anonymous new class that inherits from an anonymous async function, since it's really prototype objects all the way down in JS? In Chrome: > (new class {}).__proto__ {constructor: ƒ}
- a1369209993 8y agoI think it works out to: return yield delete void await( (null in this) instanceof typeof( new (class /*anon*/ extends (async function () {}) {}) ) ) Maybe? Is `await foo in bar` a special case in javascript?
- chrismorgan 8y agoHere it is with line breaks and as many extra legal parens as I can add without redundancy, showing all the unary and binary operators involved: return ( yield ( delete ( void ( await ( (null) in ( (this) instanceof ( typeof ( new ( class extends ( async function () {} ) {} )() ) ) ) ) ) ) ) ) `class extends X {}` is an anonymous class extending X. Syntactically, X is an arbitrary expression, which `async function () {}` is. At runtime, X must be a constructor on pain of TypeError, and an async function isn’t; this could be considered grounds for disqualification, as under no circumstances will it execute without exceptions. `new` does not require an identifier or path to follow it: it can be an expression. So `new (foo)` is fine. It’s just kinda greedy as regards an open paren of a function call: `new foo.bar(baz)(quux)` is equivalent to `(new (foo.bar)(baz))(quux)` rather than any alternative grouping. Remember also that `new foo` is legal, and equivalent to `new foo()`.
- deathanatos 8y agoTIL class expressions and anonymous classes are a thing that exists in JavaScript.
- chrismorgan 8y ago
- gwbas1c 8y agoThat's only 4, can't be broken by parentheses or braces
- lifeformed 8y agoI assume you are on mobile - keep scrolling right on the code, some reason HN doesn't wrap text on mobile.
- james-mcelwain 8y agoYou can slip `void` in after delete an indefinite number of times. You can also do an indefinite number of `new class extends class extends class extends class {} {} {} {} {}`.
- Technetium_Hat 8y agoIf you are in a module context, you could add "export" in front of the whole thing.
- chrismorgan 8y agoThe count is from `return` on; the `async function* foo() {` part is excluded due to the `* foo() {`. Thus `export` isn’t applicable.
- sandebert 8y agoThis looks like something a fuzzer would come up with.
- lhorie 8y agonull is technically not a keyword in JS[1]. And fun fact, neither are true, false, undefined, NaN, let, of[2], static[3], as[4], from[4], get[5] and set[5]. [1] https://www.ecma-international.org/ecma-262/9.0/index.html#prod-Keyword https://www.ecma-international.org/ecma-262/9.0/index.html#p... [2] as in `for (foo of bar) {}` [3] as in `class { static foo() {} }` [4] as in `import {foo as bar} from 'baz'` [5] as in `{get foo() {}, set foo() {}}`