├── .gitattributes ├── .github └── workflows │ └── build.yml ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── index.html ├── package.json └── spec.emu /.gitattributes: -------------------------------------------------------------------------------- 1 | index.html -diff merge=ours 2 | spec.js -diff merge=ours 3 | spec.css -diff merge=ours 4 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Deploy spec 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | - uses: actions/setup-node@v1 12 | with: 13 | node-version: '12.x' 14 | - run: npm install 15 | - run: npm run build 16 | - name: commit changes 17 | uses: elstudio/actions-js-build/commit@v3 18 | with: 19 | commitMessage: "fixup: [spec] `npm run build`" 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # Only apps should have lockfiles 40 | yarn.lock 41 | package-lock.json 42 | npm-shrinkwrap.json 43 | pnpm-lock.yaml 44 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ECMA TC39 and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Enum proposal 2 | 3 | > [!NOTE] 4 | > See 5 | 6 | ## Champion group & Authors 7 | 8 | - [Ron Buckton](https://github.com/rbuckton/) 9 | - [Rick Waldron](https://github.com/rwaldron/) 10 | - [Jack Works](https://github.com/Jack-Works/) 11 | 12 | ## Motivation 13 | 14 | There is a common need today to create related but _distinguishable values_, and there are many different ways to do it, 15 | and you can easily make mistakes. 16 | 17 | Let's take an example: https://nodejs.org/dist/latest-v17.x/docs/api/fs.html#fs_file_open_constants 18 | 19 | ```js 20 | import { open, constants } from 'node:fs' 21 | 22 | const { O_RDWR, S_IFDIR } = constants 23 | ``` 24 | 25 | - No difference between file open and file type constants other than convention (i.e.,`O_`,`S_`) 26 | - No useful information when debugging (i.e., what does a mode of `1280` mean?) 27 | 28 | ```js 29 | export const MESSAGE_TYPE = Object.freeze({ 30 | CONNECT: 'connect', 31 | DISCONNECT: 'disconnect', 32 | MESSAGE: 'message', 33 | ERROR: 'error', 34 | }) 35 | 36 | export function createMessage(type, ...args) { 37 | if (type === MESSAGE_TYPE.CONNECT) return { type: MESSAGE_TYPE.CONNECT } 38 | if (type === MESSAGE_TYPE.DISCONNECT) return { type: MESSAGE_TYPE.DISCONNECT } 39 | if (type === MESSAGE_TYPE.MESSAGE) return { type: MESSAGE_TYPE.MESSAGE, message: args[0] } 40 | if (type === MESSAGE_TYPE.ERROR) return { type: MESSAGE_TYPE.ERROR, error: args[0] } 41 | throw new TypeError('Invalid message type') 42 | } 43 | ``` 44 | 45 | We want to provide a better way to create those values. 46 | 47 | Enum can help: 48 | 49 | - Make code more readable 50 | - Hint useful information to the toolchain (e.g. constant inlining, exhaustiveness checks, ...) 51 | - All distinct values are naturally grouped in the syntax 52 | 53 | ```js 54 | enum MessageType { 55 | CONNECT, 56 | DISCONNECT, 57 | MESSAGE, 58 | ERROR, 59 | } 60 | match (val) { 61 | // ~~~~~~~~ Error: Missing case unhandled: val.type might be MessageType.ERROR 62 | when ({type: ${MessageType.CONNECT}}) -> startListening() 63 | when ({type: ${MessageType.DISCONNECT}}) -> stopListening() 64 | when ({type: ${MessageType.MESSAGE}, message}) -> event.emit('message', message) 65 | } 66 | ``` 67 | 68 | ## Goal 69 | 70 | - number enums 71 | - string enums 72 | - symbol enums 73 | - Some helper functions. e.g.: 74 | 75 | ```js 76 | Enum.format(FileOpen, mode) // ["CREAT", "EXCL"] 77 | ``` 78 | 79 | ## Why a frozen object is not enough? 80 | 81 | Even you can create an enum today like: 82 | 83 | ```js 84 | export const MESSAGE_TYPE = Object.freeze({ 85 | CONNECT: 'connect', 86 | DISCONNECT: 'disconnect', 87 | MESSAGE: 'message', 88 | ERROR: 'error', 89 | }) 90 | ``` 91 | 92 | You will still need to create helper functions to 93 | 94 | - lookup enum key from the value 95 | - turn bitflag into human-readable flags 96 | - parsing (if a primitive value is a member of the enum) 97 | 98 | Engine will still need to do a runtime lookup when accessing those values. 99 | 100 | ## Future steps, ADT enum 101 | 102 | **What is ADT enum?** 103 | 104 | Plain enum is "related distinguishable **primitive values**", and ADT enum is "related distinguishable **rich data 105 | structures**". 106 | 107 | With ADT enum, the example above can be rewritten as: 108 | 109 | ```js 110 | enum MessageType { 111 | CONNECT, 112 | DISCONNECT, 113 | MESSAGE(message), 114 | ERROR(error), 115 | } 116 | match (val) { 117 | // ~~~~~~~~ Error: Missing case in the match clause: MessageType.ERROR 118 | when (${MessageType.CONNECT}) -> startListening() 119 | when (${MessageType.DISCONNECT}) -> stopListening() 120 | when (${MessageType.MESSAGE} with message) -> event.emit('message', message) 121 | } 122 | ``` 123 | 124 | This part is more complicated, but also much more useful. It'll be a challenge to work on this, therefore we want to 125 | exclude it from the current proposal. 126 | 127 | We're interested to develop the idea of ADT enum and make sure that plain enum will not block the possibility that we 128 | adding this feature in the future. 129 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Proposal Title Goes Here
2519 |

Stage -1 Draft / April 25, 2025

Proposal Title Goes Here

2528 | 2529 | 2530 |

1 This is an emu-clause

2531 |

This is an algorithm:

2532 |
  1. Let proposal be undefined.
  2. If IsAccepted(proposal), then
    1. Let stage be 0.
  3. Else,
    1. Let stage be -1.
  4. Return ? ToString(proposal).
2533 |
2534 |

A Copyright & Software License

2535 | 2536 |

Copyright Notice

2537 |

© 2025 Your Name(s) Here

2538 | 2539 |

Software License

2540 |

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

2541 | 2542 |

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

2543 | 2544 |
    2545 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 2546 |
  3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  4. 2547 |
  5. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
  6. 2548 |
2549 | 2550 |

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2551 | 2552 |
2553 |
-------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "proposal-enum", 4 | "description": "A proposal of enum for ECMAScript.", 5 | "scripts": { 6 | "start": "npm run build-loose -- --watch", 7 | "build": "npm run build-loose -- --strict", 8 | "build-loose": "ecmarkup --load-biblio @tc39/ecma262-biblio --verbose spec.emu index.html --lint-spec" 9 | }, 10 | "homepage": "https://github.com/Jack-Works/proposal-enum#readme", 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/Jack-Works/proposal-enum.git" 14 | }, 15 | "license": "MIT", 16 | "devDependencies": { 17 | "@tc39/ecma262-biblio": "^2.0.2288", 18 | "ecmarkup": "^12.0.2" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
 7 | title: Proposal Title Goes Here
 8 | stage: -1
 9 | contributors: Your Name(s) Here
10 | 
11 | 12 | 13 |

This is an emu-clause

14 |

This is an algorithm:

15 | 16 | 1. Let _proposal_ be *undefined*. 17 | 1. If IsAccepted(_proposal_), then 18 | 1. Let _stage_ be *0*. 19 | 1. Else, 20 | 1. Let _stage_ be *-1*. 21 | 1. Return ? ToString(_proposal_). 22 | 23 |
24 | --------------------------------------------------------------------------------