3 ms·
> My proposed solution here is to try to capture the spirit of Python’s slice syntax rather than the exact syntax itself. We can accomplish this using an altern
by zawerf 8y ago
> My proposed solution here is to try to capture the spirit of Python’s slice syntax rather than the exact syntax itself. We can accomplish this using an alternative syntax where we use double square brackets and commas in place of colons: array[::-1] => array[[,,-1]], array[:n] => array[[,n]], array[-5::2] => array[[-5,,2]], and so on. This might not be quite as concise as Python’s syntax, but it only requires two extra keystrokes per slice and I think it’s as close as we’re gonna get.
I wonder if it makes more sense to write a babel plugin to support the python syntax. Since everyone using es6 features are transpiling anyway.
- alangpierce 8y agoA downside to introducing your own custom syntax with a Babel plugin is that it's likely to break most tools that run on your source code, like editors, ESLint, and Prettier. (And, of course, it might conflict with future ES features.) Also note that Babel's parser isn't pluginizable (normal Babel plugins just run during the transform step), so the typical way to add syntax is to fork the parser and run everything against the forked parser: https://babeljs.io/docs/en/next/babel-parser.html#will-the-babel-parser-support-a-plugin-system https://babeljs.io/docs/en/next/babel-parser.html#will-the-b...
- taion 8y agoThere's currently a TC39 stage 1 proposal to add something very like the Python syntax to JavaScript: https://github.com/tc39/proposal-slice-notation https://github.com/tc39/proposal-slice-notation. There's an active Babel feature request to implement exactly that proposal: https://github.com/babel/proposals/issues/41 https://github.com/babel/proposals/issues/41. As such, it'd definitely make a lot of sense to implement this!