Skip to content

Commit e232cb5

Browse files
author
Elad Ben-Israel
authored
feat: internal accessibility (#390)
Respect the `@internal` jsdoc tag on types and members. Any type of member marked internal will not be exposed in the API of the module and in .d.ts. files (through `--strip-internal`). Members (properties/methods) that are marked as `@internal` must also have an underscore prefix (i.e. `_myMethod`) and vice versa in order to ensure that implementers or subclasses won't modify accessibility. If a class implements (or an interface extends) an internal or private interface, that interface is erased from the implementer's API. So it is possible to use internal interfaces as bases, while still maintaining API integrity (related to #287). Fixes #388 BREAKING CHANGE: member names that begin with underscore now must be marked as "@internal" in their jsdocs, which will cause them to disappear from type declaration files and jsii APIs.
1 parent 7cc65a1 commit e232cb5

File tree

44 files changed

+1409
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1409
-26
lines changed

packages/jsii-calc/lib/compliance.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,3 +1335,92 @@ export class Constructors {
13351335
return new PrivateClass();
13361336
}
13371337
}
1338+
1339+
// internal can be used to represent members that can only be accessed from the current module
1340+
export class StripInternal {
1341+
public youSeeMe = 'hello';
1342+
1343+
/**
1344+
* This is an internal thing
1345+
* @internal
1346+
*/
1347+
public _youDontSeeMeAlthoughIamPublic = 'world'
1348+
}
1349+
1350+
/**
1351+
* @internal
1352+
*/
1353+
export class InternalClass {
1354+
public iAmNotHere = 'yes';
1355+
}
1356+
1357+
/**
1358+
* @internal
1359+
*/
1360+
export interface IInternalInterface {
1361+
prop: string;
1362+
}
1363+
1364+
/**
1365+
* @internal
1366+
*/
1367+
export enum InternalEnum {
1368+
Member1 = 12,
1369+
Member2 = 23
1370+
}
1371+
1372+
export interface IInterfaceWithInternal {
1373+
visible(): void;
1374+
1375+
/** @internal */
1376+
_hidden(): void;
1377+
}
1378+
1379+
export class ImplementsInterfaceWithInternal implements IInterfaceWithInternal {
1380+
visible() { }
1381+
1382+
/** @internal */
1383+
_hidden() { }
1384+
1385+
/** @internal */
1386+
_alsoHidden() { }
1387+
1388+
/** @internal */
1389+
_propertiesToo?: string;
1390+
}
1391+
1392+
export class ImplementsInterfaceWithInternalSubclass extends ImplementsInterfaceWithInternal {
1393+
/** @internal */
1394+
_alsoHidden() { }
1395+
1396+
/**
1397+
* @internal
1398+
*/
1399+
public _propertiesToo?: string;
1400+
}
1401+
1402+
//
1403+
// hidden interface erasure
1404+
// if a class/interface uses a hidden (private/internal) interface as base, the base will
1405+
// be erased from the API
1406+
//
1407+
1408+
interface IPrivateInterface {
1409+
private: string;
1410+
}
1411+
1412+
export interface ExtendsInternalInterface extends IInternalInterface {
1413+
boom: boolean
1414+
}
1415+
1416+
export class ImplementInternalInterface implements IInternalInterface {
1417+
prop = 'implement me'
1418+
}
1419+
1420+
export class ImplementsPrivateInterface implements IPrivateInterface {
1421+
public private = 'i came from private into the light'
1422+
}
1423+
1424+
export interface ExtendsPrivateInterface extends IPrivateInterface {
1425+
moreThings: string[];
1426+
}

packages/jsii-calc/test/assembly.jsii

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,43 @@
14781478
}
14791479
]
14801480
},
1481+
"jsii-calc.ExtendsInternalInterface": {
1482+
"assembly": "jsii-calc",
1483+
"datatype": true,
1484+
"fqn": "jsii-calc.ExtendsInternalInterface",
1485+
"kind": "interface",
1486+
"name": "ExtendsInternalInterface",
1487+
"properties": [
1488+
{
1489+
"abstract": true,
1490+
"name": "boom",
1491+
"type": {
1492+
"primitive": "boolean"
1493+
}
1494+
}
1495+
]
1496+
},
1497+
"jsii-calc.ExtendsPrivateInterface": {
1498+
"assembly": "jsii-calc",
1499+
"datatype": true,
1500+
"fqn": "jsii-calc.ExtendsPrivateInterface",
1501+
"kind": "interface",
1502+
"name": "ExtendsPrivateInterface",
1503+
"properties": [
1504+
{
1505+
"abstract": true,
1506+
"name": "moreThings",
1507+
"type": {
1508+
"collection": {
1509+
"elementtype": {
1510+
"primitive": "string"
1511+
},
1512+
"kind": "array"
1513+
}
1514+
}
1515+
}
1516+
]
1517+
},
14811518
"jsii-calc.GiveMeStructs": {
14821519
"assembly": "jsii-calc",
14831520
"fqn": "jsii-calc.GiveMeStructs",
@@ -1649,6 +1686,18 @@
16491686
}
16501687
]
16511688
},
1689+
"jsii-calc.IInterfaceWithInternal": {
1690+
"assembly": "jsii-calc",
1691+
"fqn": "jsii-calc.IInterfaceWithInternal",
1692+
"kind": "interface",
1693+
"methods": [
1694+
{
1695+
"abstract": true,
1696+
"name": "visible"
1697+
}
1698+
],
1699+
"name": "IInterfaceWithInternal"
1700+
},
16521701
"jsii-calc.IInterfaceWithMethods": {
16531702
"assembly": "jsii-calc",
16541703
"fqn": "jsii-calc.IInterfaceWithMethods",
@@ -1776,6 +1825,74 @@
17761825
}
17771826
]
17781827
},
1828+
"jsii-calc.ImplementInternalInterface": {
1829+
"assembly": "jsii-calc",
1830+
"fqn": "jsii-calc.ImplementInternalInterface",
1831+
"initializer": {
1832+
"initializer": true
1833+
},
1834+
"kind": "class",
1835+
"name": "ImplementInternalInterface",
1836+
"properties": [
1837+
{
1838+
"name": "prop",
1839+
"type": {
1840+
"primitive": "string"
1841+
}
1842+
}
1843+
]
1844+
},
1845+
"jsii-calc.ImplementsInterfaceWithInternal": {
1846+
"assembly": "jsii-calc",
1847+
"fqn": "jsii-calc.ImplementsInterfaceWithInternal",
1848+
"initializer": {
1849+
"initializer": true
1850+
},
1851+
"interfaces": [
1852+
{
1853+
"fqn": "jsii-calc.IInterfaceWithInternal"
1854+
}
1855+
],
1856+
"kind": "class",
1857+
"methods": [
1858+
{
1859+
"name": "visible",
1860+
"overrides": {
1861+
"fqn": "jsii-calc.IInterfaceWithInternal"
1862+
}
1863+
}
1864+
],
1865+
"name": "ImplementsInterfaceWithInternal"
1866+
},
1867+
"jsii-calc.ImplementsInterfaceWithInternalSubclass": {
1868+
"assembly": "jsii-calc",
1869+
"base": {
1870+
"fqn": "jsii-calc.ImplementsInterfaceWithInternal"
1871+
},
1872+
"fqn": "jsii-calc.ImplementsInterfaceWithInternalSubclass",
1873+
"initializer": {
1874+
"initializer": true
1875+
},
1876+
"kind": "class",
1877+
"name": "ImplementsInterfaceWithInternalSubclass"
1878+
},
1879+
"jsii-calc.ImplementsPrivateInterface": {
1880+
"assembly": "jsii-calc",
1881+
"fqn": "jsii-calc.ImplementsPrivateInterface",
1882+
"initializer": {
1883+
"initializer": true
1884+
},
1885+
"kind": "class",
1886+
"name": "ImplementsPrivateInterface",
1887+
"properties": [
1888+
{
1889+
"name": "private",
1890+
"type": {
1891+
"primitive": "string"
1892+
}
1893+
}
1894+
]
1895+
},
17791896
"jsii-calc.ImplictBaseOfBase": {
17801897
"assembly": "jsii-calc",
17811898
"datatype": true,
@@ -3318,6 +3435,23 @@
33183435
],
33193436
"name": "StringEnum"
33203437
},
3438+
"jsii-calc.StripInternal": {
3439+
"assembly": "jsii-calc",
3440+
"fqn": "jsii-calc.StripInternal",
3441+
"initializer": {
3442+
"initializer": true
3443+
},
3444+
"kind": "class",
3445+
"name": "StripInternal",
3446+
"properties": [
3447+
{
3448+
"name": "youSeeMe",
3449+
"type": {
3450+
"primitive": "string"
3451+
}
3452+
}
3453+
]
3454+
},
33213455
"jsii-calc.Sum": {
33223456
"assembly": "jsii-calc",
33233457
"base": {
@@ -3949,5 +4083,5 @@
39494083
}
39504084
},
39514085
"version": "0.7.15",
3952-
"fingerprint": "IWSOEhdZzuvrss5K2WBjZCawXayV13yCAKTj/kJ9+mo="
4086+
"fingerprint": "iMxRj3lsHKNzSiBrjCyBH6Pp7Uvo+1Sxh/jN3hW+3nA="
39534087
}

0 commit comments

Comments
 (0)