├── .gitattributes ├── .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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 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 | # Private Declarations 2 | 3 | A proposal to add Private Declarations, allowing trusted code _outside_ of the class lexical scope to access private state. 4 | 5 | ```js 6 | private #hello; 7 | class Example { 8 | outer #hello = 'world!'; 9 | 10 | hello() { 11 | return this.#hello; 12 | } 13 | } 14 | 15 | const ex = new Example(); 16 | console.log(ex.hello()); // => 'world!' 17 | console.log(ex.#hello); // => 'world!' 18 | ``` 19 | 20 | This also allows us to bring private state to regular objects! 21 | 22 | ```js 23 | private #hello; 24 | function Example() { 25 | return { 26 | outer #hello: 'world', 27 | 28 | hello() { 29 | return this.#hello; 30 | } 31 | } 32 | } 33 | 34 | const ex = Example(); 35 | console.log(ex.hello()); // => 'world!' 36 | console.log(ex.#hello); // => 'world!' 37 | ``` 38 | 39 | Possible [later proposals](https://docs.google.com/presentation/d/1Zu9uCFMUU4zLwBVSd3OOxtsm-CYyYvJIryLVGW5leoA/edit#slide=id.g4d82425673_0_79) 40 | can allow sharing private declarations to friendly module. 41 | 42 | ## Champions 43 | 44 | - Justin Ridgewell ([@jridgewell](https://github.com/jridgewell/)) 45 | 46 | ## Status 47 | 48 | Current [Stage](https://tc39.es/process-document/): 1 49 | 50 | ## Guiding Use Cases 51 | 52 | ### "Protected" state 53 | 54 | Protected state is a valuable visibility state when implementing class 55 | hierarchies. For for instance, a hook pattern might be used to allow 56 | subclasses to override code: 57 | 58 | ```js 59 | // https://github.com/Polymer/lit-html/blob/1a51eb54/src/lib/parts.ts 60 | 61 | private #createPart; 62 | 63 | class AttributeCommitter { 64 | //... 65 | 66 | outer #createPart() { 67 | return new AttributePart(this); 68 | } 69 | } 70 | 71 | class PropertyCommitter extends AttributeCommitter { 72 | outer #createPart() { 73 | return new PropertyPart(this); 74 | } 75 | } 76 | ``` 77 | 78 | Here, `AttributeCommitter` explicitly allows trusted (written in the 79 | same source file) subclasses to override the behavior of the 80 | `#createPart` method. By default, a normal `AttributePart` is returned. 81 | But `PropertyCommitter` works only on properties and would return a 82 | `PropertyPart`. All other code is free to be inherited via normal 83 | publicly visible fields/methods from `AttributeCommitter`. 84 | 85 | **Note** that this does not privilege code outside the file to override 86 | the `#createPart` method, as the `#createPart` private declaration is 87 | visible only in the scope where it is declared. 88 | 89 | ### Friend classes/functions 90 | 91 | For prior art in C++, see https://en.wikipedia.org/wiki/Friend_function. 92 | 93 | The AMP Project has a particular staged linting pattern that works well 94 | with friendly functions that guard access to restricted code. To begin 95 | with, statically used functions are considerably easier to lint for than 96 | object-scoped method calls because we do not need to know the objects 97 | type. So its much easier to determine if this is a restricted call or 98 | just a non-restricted call that uses the same method name. Eg, it's 99 | easier to tell that a static export `registerExtendedTemplate` is 100 | restricted vs `obj.registerExtendedTemplate`. 101 | 102 | ```js 103 | // https://github.com/ampproject/amphtml/blob/18baa9da/src/service/template-impl.js 104 | 105 | private #registerTemplate; 106 | 107 | // Exported so that it may be intalled on the global and shared 108 | // across split bundles. 109 | export class Templates { 110 | outer #registerTemplate() { 111 | //... 112 | } 113 | } 114 | 115 | // The code privileged to register templates with the shared class 116 | // instance. Importing and using is statically analyzable, and must pass 117 | // a linter. 118 | export function registerExtendedTemplate() { 119 | const templatesService = getService('templates'); 120 | return templatesService.#registerTemplate(...arguments); 121 | } 122 | ``` 123 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Private Declarations

Stage 1 Draft / January 20, 2020

Private Declarations

1824 | 1825 | 1826 |

1 Scope

1827 |

1828 | This is the spec text of the Private Declarations proposal in ECMAScript. 1829 | 1830 |

1831 |

1832 | TODO. 1833 | 1834 |

1835 |
1836 |

A Copyright & Software License

1837 | 1838 |

Copyright Notice

1839 |

© 2020 Justin Ridgewell

1840 | 1841 |

Software License

1842 |

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.

1843 | 1844 |

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

1845 | 1846 |
    1847 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 1848 |
  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. 1849 |
  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. 1850 |
1851 | 1852 |

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.

1853 | 1854 |
1855 |
-------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "proposal-private-declarations", 4 | "description": "A proposal to allow trusted code _outside_ of the class lexical scope to access private state", 5 | "scripts": { 6 | "build": "ecmarkup spec.emu index.html" 7 | }, 8 | "homepage": "https://github.com/tc39/proposal-private-declarations#readme", 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/tc39/proposal-private-declarations.git" 12 | }, 13 | "license": "MIT", 14 | "devDependencies": { 15 | "ecmarkup": "^3.11.5" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
 7 | title: Private Declarations
 8 | status: proposal
 9 | stage: 1
10 | contributors: Justin Ridgewell
11 | location: https://tc39.es/proposal-private-declarations
12 | 
13 | 14 | 15 |

Scope

16 |

17 | This is the spec text of the Private Declarations proposal in ECMAScript. 18 |

19 |

20 | TODO. 21 |

22 |
23 | --------------------------------------------------------------------------------