-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(ast-spec): tighter types and documentation for declaration/* #9211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4407bad
391172f
1ffdb16
53aa274
7feb479
19374ea
091cc9e
fc730f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import type { AST_NODE_TYPES } from '../../ast-node-types'; | ||
import type { BaseNode } from '../../base/BaseNode'; | ||
import type { DefaultExportDeclarations } from '../../unions/ExportDeclaration'; | ||
import type { ExportKind } from '../ExportAndImportKind'; | ||
|
||
export interface ExportDefaultDeclaration extends BaseNode { | ||
type: AST_NODE_TYPES.ExportDefaultDeclaration; | ||
|
@@ -10,7 +9,7 @@ export interface ExportDefaultDeclaration extends BaseNode { | |
*/ | ||
declaration: DefaultExportDeclarations; | ||
/** | ||
* The kind of the export. | ||
* The kind of the export. Always `value` for default exports. | ||
*/ | ||
exportKind: ExportKind; | ||
exportKind: 'value'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tightened: |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ interface ExportNamedDeclarationBase extends BaseNode { | |
/** | ||
* The attributes declared for the export. | ||
* ``` | ||
* export { foo } from 'mod' assert { type: 'json' }; | ||
* export { foo } from 'mod' with { type: 'json' }; | ||
* ``` | ||
* This will be an empty array if `source` is `null` | ||
*/ | ||
|
@@ -51,6 +51,13 @@ interface ExportNamedDeclarationBase extends BaseNode { | |
specifiers: ExportSpecifier[]; | ||
} | ||
|
||
/** | ||
* Exporting names from the current module. | ||
* ``` | ||
* export {}; | ||
* export { a, b }; | ||
* ``` | ||
*/ | ||
export interface ExportNamedDeclarationWithoutSourceWithMultiple | ||
extends ExportNamedDeclarationBase { | ||
/** | ||
|
@@ -64,9 +71,14 @@ export interface ExportNamedDeclarationWithoutSourceWithMultiple | |
attributes: ImportAttribute[]; | ||
declaration: null; | ||
source: null; | ||
specifiers: ExportSpecifier[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removals here are not breaking because these properties are already declared on the base |
||
} | ||
|
||
/** | ||
* Exporting a single named declaration. | ||
* ``` | ||
* export const x = 1; | ||
* ``` | ||
*/ | ||
export interface ExportNamedDeclarationWithoutSourceWithSingle | ||
extends ExportNamedDeclarationBase { | ||
/** | ||
|
@@ -80,24 +92,22 @@ export interface ExportNamedDeclarationWithoutSourceWithSingle | |
attributes: ImportAttribute[]; | ||
declaration: NamedExportDeclarations; | ||
source: null; | ||
// This will always be an empty array. | ||
/** | ||
* This will always be an empty array. | ||
*/ | ||
specifiers: ExportSpecifier[]; | ||
} | ||
|
||
/** | ||
* Export names from another module. | ||
* ``` | ||
* export { a, b } from 'mod'; | ||
* ``` | ||
*/ | ||
export interface ExportNamedDeclarationWithSource | ||
extends ExportNamedDeclarationBase { | ||
/** | ||
* This will always be an empty array. | ||
* @deprecated Replaced with {@link `attributes`}. | ||
*/ | ||
assertions: ImportAttribute[]; | ||
/** | ||
* This will always be an empty array. | ||
*/ | ||
attributes: ImportAttribute[]; | ||
declaration: null; | ||
source: StringLiteral; | ||
specifiers: ExportSpecifier[]; | ||
} | ||
|
||
export type ExportNamedDeclaration = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; | |
import type { BaseNode } from '../../base/BaseNode'; | ||
import type { Identifier } from '../../expression/Identifier/spec'; | ||
import type { TSExternalModuleReference } from '../../special/TSExternalModuleReference/spec'; | ||
import type { EntityName } from '../../unions/EntityName'; | ||
import type { TSQualifiedName } from '../../type/TSQualifiedName/spec'; | ||
import type { ImportKind } from '../ExportAndImportKind'; | ||
|
||
export interface TSImportEqualsDeclaration extends BaseNode { | ||
interface TSImportEqualsDeclarationBase extends BaseNode { | ||
type: AST_NODE_TYPES.TSImportEqualsDeclaration; | ||
/** | ||
* The locally imported name | ||
* The locally imported name. | ||
*/ | ||
id: Identifier; | ||
/** | ||
|
@@ -19,7 +19,45 @@ export interface TSImportEqualsDeclaration extends BaseNode { | |
* import F3 = require('mod'); | ||
* ``` | ||
*/ | ||
moduleReference: EntityName | TSExternalModuleReference; | ||
// TODO(#1852) - breaking change remove this as it is invalid | ||
moduleReference: Identifier | TSExternalModuleReference | TSQualifiedName; | ||
/** | ||
* The kind of the import. Always `'value'` unless `moduleReference` is a | ||
* `TSExternalModuleReference`. | ||
*/ | ||
importKind: ImportKind; | ||
} | ||
|
||
export interface TSImportEqualsNamespaceDeclaration | ||
extends TSImportEqualsDeclarationBase { | ||
/** | ||
* The value being aliased. | ||
* ``` | ||
* import F1 = A; | ||
* import F2 = A.B.C; | ||
* ``` | ||
*/ | ||
moduleReference: Identifier | TSQualifiedName; | ||
/** | ||
* The kind of the import. | ||
*/ | ||
importKind: 'value'; | ||
} | ||
|
||
export interface TSImportEqualsRequireDeclaration | ||
extends TSImportEqualsDeclarationBase { | ||
/** | ||
* The value being aliased. | ||
* ``` | ||
* import F3 = require('mod'); | ||
* ``` | ||
*/ | ||
moduleReference: TSExternalModuleReference; | ||
/** | ||
* The kind of the import. | ||
*/ | ||
importKind: ImportKind; | ||
} | ||
|
||
export type TSImportEqualsDeclaration = | ||
| TSImportEqualsNamespaceDeclaration | ||
| TSImportEqualsRequireDeclaration; | ||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to union |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,12 @@ interface TSModuleDeclarationBase extends BaseNode { | |
/** | ||
* The body of the module. | ||
* This can only be `undefined` for the code `declare module 'mod';` | ||
* This will be a `TSModuleDeclaration` if the name is "nested" (`Foo.Bar`). | ||
*/ | ||
body?: TSModuleBlock; | ||
/** | ||
* Whether this is a global declaration | ||
* ``` | ||
* declare global {} | ||
* ``` | ||
* | ||
* @deprecated Use {@link kind} instead | ||
*/ | ||
// TODO - remove this in the next major (we have `.kind` now) | ||
global: boolean; | ||
|
@@ -50,64 +48,81 @@ interface TSModuleDeclarationBase extends BaseNode { | |
* module 'foo' {} | ||
* ^^^^^^ | ||
* | ||
* declare global {} | ||
* ^^^^^^ | ||
* global {} | ||
* ^^^^^^ | ||
* ``` | ||
*/ | ||
kind: TSModuleDeclarationKind; | ||
} | ||
|
||
export interface TSModuleDeclarationNamespace extends TSModuleDeclarationBase { | ||
kind: 'namespace'; | ||
// namespaces cannot have literal IDs | ||
id: Identifier | TSQualifiedName; | ||
// namespaces must always have a body | ||
body: TSModuleBlock; | ||
} | ||
|
||
export interface TSModuleDeclarationGlobal extends TSModuleDeclarationBase { | ||
kind: 'global'; | ||
// cannot have a nested namespace for global module augmentation | ||
// cannot have `declare global;` | ||
body: TSModuleBlock; | ||
// this will always be an Identifier with name `global` | ||
/** | ||
* This will always be an Identifier with name `global` | ||
*/ | ||
id: Identifier; | ||
body: TSModuleBlock; | ||
} | ||
|
||
interface TSModuleDeclarationModuleBase extends TSModuleDeclarationBase { | ||
kind: 'module'; | ||
} | ||
|
||
export type TSModuleDeclarationModule = | ||
| TSModuleDeclarationModuleWithIdentifierId | ||
| TSModuleDeclarationModuleWithStringId; | ||
export type TSModuleDeclarationModuleWithStringId = | ||
| TSModuleDeclarationModuleWithStringIdDeclared | ||
| TSModuleDeclarationModuleWithStringIdNotDeclared; | ||
/** | ||
* A string module declaration that is not declared: | ||
* ``` | ||
* module 'foo' {} | ||
* ``` | ||
*/ | ||
export interface TSModuleDeclarationModuleWithStringIdNotDeclared | ||
extends TSModuleDeclarationModuleBase { | ||
kind: 'module'; | ||
id: StringLiteral; | ||
declare: false; | ||
// cannot have nested namespaces with a string ID, must have a body | ||
body: TSModuleBlock; | ||
} | ||
/** | ||
* A string module declaration that is declared: | ||
* ``` | ||
* declare module 'foo' {} | ||
* declare module 'foo'; | ||
* ``` | ||
*/ | ||
export interface TSModuleDeclarationModuleWithStringIdDeclared | ||
extends TSModuleDeclarationModuleBase { | ||
kind: 'module'; | ||
id: StringLiteral; | ||
declare: true; | ||
// cannot have nested namespaces with a string ID, might not have a body | ||
body?: TSModuleBlock; | ||
} | ||
/** | ||
* The legacy module declaration, replaced with namespace declarations. | ||
* ``` | ||
* module A {} | ||
* ``` | ||
*/ | ||
export interface TSModuleDeclarationModuleWithIdentifierId | ||
extends TSModuleDeclarationModuleBase { | ||
kind: 'module'; | ||
id: Identifier; | ||
// modules with an Identifier must always have a body | ||
// TODO: we emit the wrong AST for `module A.B {}` | ||
// https://github.com/typescript-eslint/typescript-eslint/pull/6272 only fixed namespaces | ||
// Maybe not worth fixing since it's legacy | ||
Comment on lines
+114
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth pointing out |
||
body: TSModuleBlock; | ||
} | ||
|
||
export type TSModuleDeclarationModuleWithStringId = | ||
| TSModuleDeclarationModuleWithStringIdDeclared | ||
| TSModuleDeclarationModuleWithStringIdNotDeclared; | ||
export type TSModuleDeclarationModule = | ||
| TSModuleDeclarationModuleWithIdentifierId | ||
| TSModuleDeclarationModuleWithStringId; | ||
export type TSModuleDeclaration = | ||
| TSModuleDeclarationGlobal | ||
| TSModuleDeclarationModule | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TS now allows
export type * from