Skip to content

Commit

Permalink
Downlevel export star (#8)
Browse files Browse the repository at this point in the history
* add export * as ns test

* Downlevel export * as ns from 'x'

To:

```ts
import * as ns from 'x'
export { ns }
```

This doesn't generate an intermediate name for now. I think this is only
required in case the file already declares `ns` as something else.

* correctly emit temp alias

* Update prettierignore
  • Loading branch information
sandersn authored Jan 15, 2020
1 parent 481252d commit 6bfba0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
baselines/
test/
2 changes: 2 additions & 0 deletions baselines/ts3.4/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export namespace N {
abstract r: boolean;
}
}
import * as rex_1 from "src/test";
export { rex_1 as rex } from "src/test";
40 changes: 36 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const assert = require("assert");
function doTransform(k) {
/**
* @param {Node} n
* @return {Node}
* @return {import("typescript").VisitResult<Node>}
*/
const transform = function(n) {
if (ts.isGetAccessor(n)) {
// get x(): number => x: number
let flags = ts.getCombinedModifierFlags(n);
if (!getMatchingAccessor(n, "get")) {
flags |= ts.ModifierFlags.Readonly;
Expand All @@ -29,8 +30,10 @@ function doTransform(k) {
/*initialiser*/ undefined
);
} else if (ts.isSetAccessor(n)) {
// set x(value: number) => x: number
let flags = ts.getCombinedModifierFlags(n);
if (getMatchingAccessor(n, "set")) {
return /** @type {*} */ (undefined);
return undefined;
} else {
assert(n.parameters && n.parameters.length);
return ts.createProperty(
Expand All @@ -42,6 +45,36 @@ function doTransform(k) {
/*initialiser*/ undefined
);
}
} else if (
ts.isExportDeclaration(n) &&
n.exportClause &&
n.moduleSpecifier &&
ts.isNamespaceExport(n.exportClause)
) {
// export * as ns from 'x'
// =>
// import * as ns_1 from 'x'
// export { ns_1 as ns }
const tempName = ts.createUniqueName(n.exportClause.name.getText());
return [
ts.createImportDeclaration(
n.decorators,
n.modifiers,
ts.createImportClause(
/*name*/ undefined,
ts.createNamespaceImport(tempName)
),
n.moduleSpecifier
),
ts.createExportDeclaration(
undefined,
undefined,
ts.createNamedExports([
ts.createExportSpecifier(tempName, n.exportClause.name)
]),
n.moduleSpecifier
)
];
}
return ts.visitEachChild(n, transform, k);
};
Expand Down Expand Up @@ -87,11 +120,10 @@ function main(src, target) {
);
const checker = program.getTypeChecker(); // just used for setting parent pointers right now
const files = mapDefined(program.getRootFileNames(), program.getSourceFile);
const resultat = ts.transform(files, [doTransform]);
const printer = ts.createPrinter({
newLine: ts.NewLineKind.CarriageReturnLineFeed
});
for (const t of resultat.transformed) {
for (const t of ts.transform(files, [doTransform]).transformed) {
const f = /** @type {import("typescript").SourceFile} */ (t);
const targetPath = path.join(target, f.fileName.slice(src.length));
sh.mkdir("-p", path.dirname(targetPath));
Expand Down
2 changes: 2 additions & 0 deletions test/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export namespace N {
abstract set r(value: boolean);
}
}

export * as rex from "src/test";

0 comments on commit 6bfba0d

Please sign in to comment.