|
| 1 | +### `invariant-mutable-properties` |
| 2 | + |
| 3 | +A prototype of a [`typescript-eslint`](https://github.com/typescript-eslint/typescript-eslint) rule |
| 4 | +that treats mutable properties as invariant. |
| 5 | + |
| 6 | +This rule uses [`ts-simple-type`](https://github.com/runem/ts-simple-type) for type checking |
| 7 | +because there is no public type checking API in TypeScript [yet](https://github.com/microsoft/TypeScript/issues/9879). |
| 8 | + |
| 9 | +----- |
| 10 | + |
| 11 | +### Configuration |
| 12 | + |
| 13 | +```ts |
| 14 | +interface Config { |
| 15 | + //Defaults to `true` |
| 16 | + //If ts-simple-type crashes, file an issue with ts-simple-type repo. |
| 17 | + //At the moment, it will crash for complex types and some generic functions. |
| 18 | + //You might want to set this to `false` if ts-simple-type crashes too often. |
| 19 | + "reportTsSimpleTypeCrash": boolean, |
| 20 | + //Defaults to `true` |
| 21 | + //If the rule crashes, file an issue |
| 22 | + "reportRuleCrash": boolean |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +See [`.eslintrc-base.json`](.eslintrc-base.json) for more details |
| 27 | + |
| 28 | +----- |
| 29 | + |
| 30 | +### Testing |
| 31 | + |
| 32 | +1. Clone this repo |
| 33 | +1. `npm install` |
| 34 | +1. `npm run test` |
| 35 | + |
| 36 | ++ You may build with `npm run build`. |
| 37 | ++ You may lint with `npm run lint` |
| 38 | + |
| 39 | +----- |
| 40 | + |
| 41 | +### Examples |
| 42 | + |
| 43 | +At the moment, TypeScript treats mutable properties as covariant, |
| 44 | +```ts |
| 45 | +const src : { x : number } = { x : 34 }; |
| 46 | +/** |
| 47 | + * Assignment allowed, `x` is covariant |
| 48 | + */ |
| 49 | +const dst : { x : number|string } = src; |
| 50 | + |
| 51 | +console.log(src.x); //34 |
| 52 | +dst.x = "Oops!"; |
| 53 | +console.log(src.x); //Oops! |
| 54 | +``` |
| 55 | +[Playground](http://www.typescriptlang.org/play/#code/MYewdgzgLgBBBOwYC4YG8YA8UzAVwFsAjAU3hgF8YBedLHAZgBZKBuAKAHoAqb9mbjACCECAEsA5mAIkwsAIYAbRSADuJACYAaGAANMumGIgxQAN3nwx8uf26d2oSLA3QcGbKnzEyAH2hWYBKUNHCIHI7gECCKJAB0KhIAFAjAcZgAlKwwnJzM7K5Q6aEARADyIAAOEACEJRxO0bEJIMmp6Vk5nBXVNUA) |
| 56 | + |
| 57 | +----- |
| 58 | + |
| 59 | +As you can see from the above example, this is not safe. |
| 60 | + |
| 61 | +This rule treats mutable properties as invariant, |
| 62 | +```ts |
| 63 | +const src : { x : number } = { x : 34 }; |
| 64 | +/** |
| 65 | + * Lint Error: Mutable properties are invariant; x |
| 66 | + */ |
| 67 | +const dst : { x : number|string } = src; |
| 68 | +``` |
| 69 | + |
| 70 | +----- |
| 71 | + |
| 72 | +`readonly` properties are still covariant, |
| 73 | +```ts |
| 74 | +const src : { x : number } = { x : 34 }; |
| 75 | +/** |
| 76 | + * Assignment allowed, `readonly x` is covariant |
| 77 | + */ |
| 78 | +const dst : { readonly x : number|string } = src; |
| 79 | +``` |
| 80 | + |
| 81 | +----- |
| 82 | + |
| 83 | +See [tests](test/src/rules/invariant-mutable-properties.ts) for more examples. |
| 84 | + |
| 85 | +----- |
| 86 | + |
| 87 | +### TODO |
| 88 | + |
| 89 | ++ Proper support for generics (and tests!) |
| 90 | ++ Proper checking for object/array literals (and tests!) |
| 91 | + |
| 92 | + Right now, it always assumes object/array literals are safe but this is not true. |
| 93 | + |
| 94 | + Ctrl+F `@todo` for examples |
| 95 | + |
| 96 | ++ More tests |
| 97 | + |
| 98 | ++ Use more stable type-checking API |
| 99 | + |
| 100 | + `ts-simple-type` crashes with generic functions and complex types often, at the moment. |
| 101 | + |
| 102 | +----- |
| 103 | + |
| 104 | +The rule does not handle generics properly yet, |
| 105 | +```ts |
| 106 | +function foo<SrcT extends { x : number }> (src : SrcT) { |
| 107 | + //Right now, the rule thinks this is safe |
| 108 | + //but this is not true |
| 109 | + const dst : { x : number } = src; |
| 110 | + //Imagine SrcT is { x : 2 } |
| 111 | + //Boom, src.x now has 1 instead of 2 |
| 112 | + dst.x = 1; |
| 113 | +} |
| 114 | +const src : { x : 2 } = { x : 2 }; |
| 115 | +console.log(src.x); //2 |
| 116 | +foo<{ x : 2 }>(src); |
| 117 | +console.log(src.x); //1 |
| 118 | +``` |
| 119 | +[Playground](http://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABMOcA8BlAThAKogUwA8oCwATAZ0QG9EjEAuRMEAWwCMCtEBfAPkQAKSjiaJseAJS0AUIgWIA9EoBKMAOYALKCzgB3ADSIoWgoiwgANudMwwAa2p3qMapQCGwAvMUqOILouiG56QZY+iogQCJS6VLrMdAzMrJzcfIgAvIiiEADcvgoqAJJsHhr25pL4ocniAEx8RcpKAEKobMZ5AHQMYAaIWh7UAIwhYHEEHuSIcMCIDS0JfdmIo4W8sjGTunni9cxNvGuHi3yFO5RwNj1WcBoiOH1S+a1LKOhnx-xPEK-bWI3Ah3B5-F5vFSjWRAA) |
| 120 | + |
| 121 | +----- |
| 122 | + |
| 123 | +The rule does not handle object and array literals properly yet, |
| 124 | +```ts |
| 125 | +const src : { |
| 126 | + nested : { |
| 127 | + doNotMutateMePlease : string, |
| 128 | + } |
| 129 | +} = { |
| 130 | + nested : { |
| 131 | + doNotMutateMePlease : "please?" |
| 132 | + } |
| 133 | +}; |
| 134 | +//Right now, the rule thinks object literals are **ALWAYS** safe |
| 135 | +//but this is not true |
| 136 | +const dst : { |
| 137 | + nested : { |
| 138 | + doNotMutateMePlease : string|number, |
| 139 | + } |
| 140 | +} = { |
| 141 | + ...src, |
| 142 | +}; |
| 143 | +//Boom. |
| 144 | +//src.nested.doNotMutateMePlease is now number and not string |
| 145 | +console.log(src.nested.doNotMutateMePlease); //"please?" |
| 146 | +dst.nested.doNotMutateMePlease = 34; |
| 147 | +console.log(src.nested.doNotMutateMePlease); //34 |
| 148 | +``` |
| 149 | +[Playground](http://www.typescriptlang.org/play/#code/MYewdgzgLgBBBOwYC4YG8BQNszAU2jwBMV0scKiQA5EKAWQFcoBDKPevABQBs8WIeUtHgBLMAHMANOWwBfDHJgBeMhXyESqTBUo06TVu069+g0gCIADnwF4A-BdkwFcgNwYA9J4BKoiQAWsGAgAO5SMFABQvCMfJEB4gDWEDAgAEYAVnjAsDyi7PAsPKks8EIAVBUAggAyAOrVAJoAylVwLABmeF6e6cwJoqlDuHSRsT2gkLBE0KQ6OBrsWmq62FS0DMxsHNy25qgi4hIAPmCMALbpePAyFK4qq9gAdK8IwDLuvQBCICAXz1672eS2Izw2Bm2xj2ZiEIxCoVwl2u8BgLDAJBCsCOkgwUwgID4zx4IAkAApgaCiOD9FsjLtTHYAJRuGDeaz7BxOWZQEEEZY0zaGHYmTmPADMABYPPjCXhiaSKYg+ZpBZD6aLYSy2Z4pUA) |
| 150 | + |
| 151 | + |
| 152 | +----- |
| 153 | + |
| 154 | +I am not very familiar with TypeScript's API, or `typescript-eslint`'s API. |
| 155 | + |
| 156 | +If someone out there is more familiar with them and |
| 157 | +agrees making mutable properties invariant would be pretty cool, |
| 158 | +please improve this rule and/or create a better version of it! |
0 commit comments