rollup-plugin-dts
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -0,1 +1,5 @@ | ||
### 1.1.4 2019-06-21 | ||
- fix issues around default exports and overrides | ||
### 1.1.3 2019-06-21 | ||
@@ -2,0 +6,0 @@ |
@@ -454,10 +454,43 @@ 'use strict'; | ||
} | ||
removeModifier(node, kind = ts.SyntaxKind.ExportKeyword) { | ||
for (const mod of node.modifiers || []) { | ||
if (mod.kind === kind) { | ||
const start = node.getStart(); | ||
const end = mod.end + 1; | ||
this.pushRaw(removeNested({ start, end })); | ||
/** | ||
* This will fix up the modifiers of a declaration. | ||
* We want to remove `export (default)?` modifiers, and in that case add a | ||
* missing `declare`. All the others should be untouched. | ||
*/ | ||
fixModifiers(node) { | ||
if (!node.modifiers) { | ||
return; | ||
} | ||
const modifiers = []; | ||
let hasDeclare = false; | ||
let start = Infinity; | ||
let end = 0; | ||
for (const mod of node.modifiers) { | ||
if (mod.kind !== ts.SyntaxKind.ExportKeyword && mod.kind !== ts.SyntaxKind.DefaultKeyword) { | ||
modifiers.push(mod.getText()); | ||
} | ||
if (mod.kind === ts.SyntaxKind.DeclareKeyword) { | ||
hasDeclare = true; | ||
} | ||
start = Math.min(start, mod.getStart()); | ||
end = Math.max(end, mod.getEnd()); | ||
} | ||
// function and class *must* have a `declare` modifier | ||
if (!hasDeclare && (ts.isClassDeclaration(node) || ts.isFunctionDeclaration(node))) { | ||
modifiers.unshift("declare"); | ||
} | ||
const newModifiers = modifiers.join(" "); | ||
if (!newModifiers && end) { | ||
end += 1; | ||
} | ||
const original = this.transformer.sourceFile.text.slice(start, end); | ||
const middle = start + newModifiers.length; | ||
this.pushRaw(removeNested({ start: middle, end })); | ||
if (newModifiers) { | ||
this.transformer.fixups.push({ | ||
original, | ||
replaceWith: newModifiers, | ||
range: { start, end: middle }, | ||
}); | ||
} | ||
} | ||
@@ -747,2 +780,3 @@ convertParametersAndType(node) { | ||
this.fixups = []; | ||
this.declarations = new Map(); | ||
this.exports = new Set(); | ||
@@ -762,3 +796,3 @@ this.ast = createProgram(sourceFile); | ||
this.fixups.push({ | ||
identifier, | ||
replaceWith: identifier, | ||
original, | ||
@@ -773,22 +807,28 @@ range, | ||
maybeMarkAsExported(node, id) { | ||
if (matchesModifier(node, ts.ModifierFlags.ExportDefault)) { | ||
const start = node.pos; | ||
this.pushStatement(createDefaultExport(id, { start, end: start })); | ||
return true; | ||
const loc = { start: node.pos, end: node.pos }; | ||
if (!matchesModifier(node, ts.ModifierFlags.Export) || !node.modifiers) { | ||
return false; | ||
} | ||
else if (matchesModifier(node, ts.ModifierFlags.Export)) { | ||
const start = node.pos; | ||
const name = id.getText(); | ||
if (this.exports.has(name)) { | ||
return true; | ||
} | ||
this.pushStatement(createExport(id, { start, end: start })); | ||
this.exports.add(name); | ||
const isExportDefault = matchesModifier(node, ts.ModifierFlags.ExportDefault); | ||
const name = isExportDefault ? "default" : id.getText(); | ||
if (this.exports.has(name)) { | ||
return true; | ||
} | ||
return false; | ||
this.pushStatement((isExportDefault ? createDefaultExport : createExport)(id, loc)); | ||
this.exports.add(name); | ||
return true; | ||
} | ||
createDeclaration(id, range) { | ||
const scope = new DeclarationScope({ id, range, transformer: this }); | ||
this.pushStatement(scope.declaration); | ||
const name = id.getText(); | ||
// rollup has problems with functions that are defined twice. For overrides, | ||
// we can just reuse the already declared function, since overrides are | ||
// supposed to be declared close to each other… | ||
// if they are not close to each other, we do have a problem -_- | ||
let scope = this.declarations.get(name); | ||
if (!scope) { | ||
scope = new DeclarationScope({ id, range, transformer: this }); | ||
this.pushStatement(scope.declaration); | ||
this.declarations.set(name, scope); | ||
} | ||
scope.declaration.end = range.end; | ||
return scope; | ||
@@ -833,3 +873,3 @@ } | ||
const scope = this.createDeclaration(node.name, node); | ||
scope.removeModifier(node); | ||
scope.fixModifiers(node); | ||
scope.pushIdentifierReference(node.name); | ||
@@ -841,3 +881,3 @@ scope.convertNamespace(node); | ||
const scope = this.createDeclaration(node.name, node); | ||
scope.removeModifier(node); | ||
scope.fixModifiers(node); | ||
scope.pushIdentifierReference(node.name); | ||
@@ -852,3 +892,3 @@ } | ||
const scope = this.createDeclaration(node.name, node); | ||
scope.removeModifier(node); | ||
scope.fixModifiers(node); | ||
scope.pushIdentifierReference(node.name); | ||
@@ -864,6 +904,3 @@ scope.convertParametersAndType(node); | ||
const scope = this.createDeclaration(node.name, node); | ||
scope.removeModifier(node); | ||
if (ts.isInterfaceDeclaration(node)) { | ||
scope.removeModifier(node, ts.SyntaxKind.DefaultKeyword); | ||
} | ||
scope.fixModifiers(node); | ||
const typeVariables = scope.convertTypeParameters(node.typeParameters); | ||
@@ -877,3 +914,3 @@ scope.convertHeritageClauses(node); | ||
const scope = this.createDeclaration(node.name, node); | ||
scope.removeModifier(node); | ||
scope.fixModifiers(node); | ||
const typeVariables = scope.convertTypeParameters(node.typeParameters); | ||
@@ -896,3 +933,3 @@ scope.convertTypeNode(node.type); | ||
const scope = this.createDeclaration(decl.name, node); | ||
scope.removeModifier(node); | ||
scope.fixModifiers(node); | ||
scope.convertTypeNode(decl.type); | ||
@@ -1016,11 +1053,5 @@ } | ||
const { ast, fixups } = transformer.transform(); | ||
// NOTE(swatinem): | ||
// hm, typescript generates `export default` without a declare, | ||
// but rollup moves the `export default` to a different place, which leaves | ||
// the function declaration without a `declare`. | ||
// Well luckily both words have the same length, haha :-D | ||
let code = input.getFullText(); | ||
code = code.replace(/(export\s+)default(\s+(function|class))/m, "$1declare$2"); | ||
for (const fixup of fixups) { | ||
code = code.slice(0, fixup.range.start) + fixup.identifier + code.slice(fixup.range.end); | ||
code = code.slice(0, fixup.range.start) + fixup.replaceWith + code.slice(fixup.range.end); | ||
} | ||
@@ -1027,0 +1058,0 @@ return { code, ast }; |
{ | ||
"name": "rollup-plugin-dts", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "An experiment to generate .d.ts rollup files", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87322
2229