mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
added isolated declarations flag.
This commit is contained in:
@@ -795,6 +795,14 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
|
||||
description: Diagnostics.Do_not_transform_or_elide_any_imports_or_exports_not_marked_as_type_only_ensuring_they_are_written_in_the_output_file_s_format_based_on_the_module_setting,
|
||||
defaultValueDescription: false,
|
||||
},
|
||||
{
|
||||
name: "isolatedDeclarations",
|
||||
type: "boolean",
|
||||
category: Diagnostics.Interop_Constraints,
|
||||
description: Diagnostics.Ensure_that_each_file_can_have_declaration_emit_generated_without_type_information,
|
||||
transpileOptionValue: true,
|
||||
defaultValueDescription: false,
|
||||
},
|
||||
|
||||
// Strict Type Checks
|
||||
{
|
||||
|
||||
@@ -6067,6 +6067,11 @@
|
||||
"category": "Message",
|
||||
"code": 6718
|
||||
},
|
||||
"Ensure that each file can have declaration emit generated without type information": {
|
||||
"category": "Message",
|
||||
"code": 6719
|
||||
},
|
||||
|
||||
"Default catch clause variables as 'unknown' instead of 'any'.": {
|
||||
"category": "Message",
|
||||
"code": 6803
|
||||
@@ -6581,6 +6586,10 @@
|
||||
"category": "Error",
|
||||
"code": 9006
|
||||
},
|
||||
"Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.": {
|
||||
"category": "Error",
|
||||
"code": 9007
|
||||
},
|
||||
"JSX attributes must only be assigned a non-empty 'expression'.": {
|
||||
"category": "Error",
|
||||
"code": 17000
|
||||
|
||||
@@ -317,8 +317,15 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
const resolver = context.getEmitResolver();
|
||||
const options = context.getCompilerOptions();
|
||||
const { noResolve, stripInternal } = options;
|
||||
const isolatedDeclarations = false;
|
||||
return transformRoot;
|
||||
|
||||
function reportIsolatedDeclarationError(_node: Node) {
|
||||
// context.addDiagnostic(createDiagnosticForNode(
|
||||
// node,
|
||||
// Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit
|
||||
// ));
|
||||
}
|
||||
function recordTypeReferenceDirectivesIfNecessary(typeReferenceDirectives: readonly [specifier: string, mode: ResolutionMode][] | undefined): void {
|
||||
if (!typeReferenceDirectives) {
|
||||
return;
|
||||
@@ -739,6 +746,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
}
|
||||
|
||||
function shouldPrintWithInitializer(node: Node) {
|
||||
if (isolatedDeclarations) return false;
|
||||
return canHaveLiteralInitializer(node) && resolver.isLiteralConstDeclaration(getParseTreeNode(node) as CanHaveLiteralInitializer); // TODO: Make safe
|
||||
}
|
||||
|
||||
@@ -772,6 +780,13 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
// Literal const declarations will have an initializer ensured rather than a type
|
||||
return;
|
||||
}
|
||||
if (isolatedDeclarations) {
|
||||
if (type == undefined) {
|
||||
reportIsolatedDeclarationError(node);
|
||||
return factory.createTypeReferenceNode("invalid");
|
||||
}
|
||||
return visitNode(type, visitDeclarationSubtree);
|
||||
}
|
||||
const shouldUseResolverType = node.kind === SyntaxKind.Parameter &&
|
||||
(resolver.isRequiredInitializedParameter(node) ||
|
||||
resolver.isOptionalUninitializedParameterProperty(node));
|
||||
@@ -1387,7 +1402,13 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
errorNode: input
|
||||
});
|
||||
errorFallbackNode = input;
|
||||
const varDecl = factory.createVariableDeclaration(newId, /*exclamationToken*/ undefined, resolver.createTypeOfExpression(input.expression, input, declarationEmitNodeBuilderFlags, symbolTracker), /*initializer*/ undefined);
|
||||
if (isolatedDeclarations) {
|
||||
reportIsolatedDeclarationError(input);
|
||||
}
|
||||
const type = isolatedDeclarations ?
|
||||
factory.createTypeReferenceNode("invalud") :
|
||||
resolver.createTypeOfExpression(input.expression, input, declarationEmitNodeBuilderFlags, symbolTracker)
|
||||
const varDecl = factory.createVariableDeclaration(newId, /*exclamationToken*/ undefined, type, /*initializer*/ undefined);
|
||||
errorFallbackNode = undefined;
|
||||
const statement = factory.createVariableStatement(needsDeclare ? [factory.createModifier(SyntaxKind.DeclareKeyword)] : [], factory.createVariableDeclarationList([varDecl], NodeFlags.Const));
|
||||
|
||||
@@ -1494,9 +1515,15 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
return undefined; // TODO GH#33569: Handle element access expressions that created late bound names (rather than silently omitting them)
|
||||
}
|
||||
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(p.valueDeclaration);
|
||||
const type = resolver.createTypeOfDeclaration(p.valueDeclaration, fakespace, declarationEmitNodeBuilderFlags, symbolTracker);
|
||||
let type = resolver.createTypeOfDeclaration(p.valueDeclaration, fakespace, declarationEmitNodeBuilderFlags, symbolTracker);
|
||||
getSymbolAccessibilityDiagnostic = oldDiag;
|
||||
const nameStr = unescapeLeadingUnderscores(p.escapedName);
|
||||
|
||||
if(isolatedDeclarations) {
|
||||
reportIsolatedDeclarationError(p.valueDeclaration);
|
||||
type = factory.createTypeReferenceNode("invalid");
|
||||
}
|
||||
|
||||
const isNonContextualKeywordName = isStringANonContextualKeyword(nameStr);
|
||||
const name = isNonContextualKeywordName ? factory.getGeneratedNameForNode(p.valueDeclaration) : factory.createIdentifier(nameStr);
|
||||
if (isNonContextualKeywordName) {
|
||||
@@ -1678,6 +1705,24 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
if (extendsClause && !isEntityNameExpression(extendsClause.expression) && extendsClause.expression.kind !== SyntaxKind.NullKeyword) {
|
||||
// We must add a temporary declaration for the extends clause expression
|
||||
|
||||
// Isolated declarations do not allow inferred type in the extends clause
|
||||
if(isolatedDeclarations) {
|
||||
reportIsolatedDeclarationError(extendsClause);
|
||||
return cleanup(factory.updateClassDeclaration(
|
||||
input,
|
||||
modifiers,
|
||||
input.name,
|
||||
typeParameters,
|
||||
factory.createNodeArray([factory.createHeritageClause(SyntaxKind.ExtendsKeyword,
|
||||
[
|
||||
factory.createExpressionWithTypeArguments(
|
||||
factory.createIdentifier("invalid"),
|
||||
/* typeArguments */undefined
|
||||
)
|
||||
])]),
|
||||
members
|
||||
))
|
||||
}
|
||||
const oldId = input.name ? unescapeLeadingUnderscores(input.name.escapedText) : "default";
|
||||
const newId = factory.createUniqueName(`${oldId}_base`, GeneratedIdentifierFlags.Optimistic);
|
||||
getSymbolAccessibilityDiagnostic = () => ({
|
||||
@@ -1725,7 +1770,7 @@ export function transformDeclarations(context: TransformationContext) {
|
||||
return cleanup(factory.updateEnumDeclaration(input, factory.createNodeArray(ensureModifiers(input)), input.name, factory.createNodeArray(mapDefined(input.members, m => {
|
||||
if (shouldStripInternal(m)) return;
|
||||
// Rewrite enum values to their constants, if available
|
||||
const constValue = resolver.getConstantValue(m);
|
||||
const constValue = isolatedDeclarations? undefined : resolver.getConstantValue(m);
|
||||
return preserveJsDoc(factory.updateEnumMember(m, m.name, constValue !== undefined ? typeof constValue === "string" ? factory.createStringLiteral(constValue) : factory.createNumericLiteral(constValue) : undefined), m);
|
||||
}))));
|
||||
}
|
||||
|
||||
@@ -7071,6 +7071,7 @@ export interface CompilerOptions {
|
||||
inlineSourceMap?: boolean;
|
||||
inlineSources?: boolean;
|
||||
isolatedModules?: boolean;
|
||||
isolatedDeclarations?: boolean;
|
||||
jsx?: JsxEmit;
|
||||
keyofStringsOnly?: boolean;
|
||||
lib?: string[];
|
||||
|
||||
@@ -7083,6 +7083,7 @@ declare namespace ts {
|
||||
inlineSourceMap?: boolean;
|
||||
inlineSources?: boolean;
|
||||
isolatedModules?: boolean;
|
||||
isolatedDeclarations?: boolean;
|
||||
jsx?: JsxEmit;
|
||||
keyofStringsOnly?: boolean;
|
||||
lib?: string[];
|
||||
|
||||
@@ -3140,6 +3140,7 @@ declare namespace ts {
|
||||
inlineSourceMap?: boolean;
|
||||
inlineSources?: boolean;
|
||||
isolatedModules?: boolean;
|
||||
isolatedDeclarations?: boolean;
|
||||
jsx?: JsxEmit;
|
||||
keyofStringsOnly?: boolean;
|
||||
lib?: string[];
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+1
@@ -76,6 +76,7 @@
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||
// "isolatedDeclarations": true, /* Ensure that each file can have declaration emit generated without type information */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"isolatedDeclarations": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user