-
Notifications
You must be signed in to change notification settings - Fork 74
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
Improve typings: ESM, AcornJsxParser
class and tokTypes
const
#130
Changes from 14 commits
7b59516
dda96cc
baa0a61
8526524
def9edd
2383cca
e4eca02
d97bd96
620551b
835ea91
5459984
af6247d
d80ceef
1c2fa2c
4cfaf9f
41e8fcd
71b7e61
b976601
fd68170
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,12 +1,65 @@ | ||||||||||||||||||||||
import { Parser } from 'acorn' | ||||||||||||||||||||||
import * as acorn from 'acorn'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
declare const jsx: (options?: jsx.Options) => (BaseParser: typeof Parser) => typeof Parser; | ||||||||||||||||||||||
declare const jsx: { | ||||||||||||||||||||||
tokTypes: typeof acorn.tokTypes; | ||||||||||||||||||||||
(options?: jsx.Options): (BaseParser: typeof acorn.Parser) => jsx.AcornJsxParser | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
declare namespace jsx { | ||||||||||||||||||||||
interface Options { | ||||||||||||||||||||||
|
||||||||||||||||||||||
type tokTypes = typeof acorn.tokTypes; | ||||||||||||||||||||||
|
||||||||||||||||||||||
export interface TokTypes extends tokTypes { | ||||||||||||||||||||||
jsxName: acorn.TokenType, | ||||||||||||||||||||||
jsxText: acorn.TokenType, | ||||||||||||||||||||||
jsxTagEnd: acorn.TokenType, | ||||||||||||||||||||||
jsxTagStart: acorn.TokenType | ||||||||||||||||||||||
} | ||||||||||||||||||||||
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. I suggest to do the following:
Suggested change
There’s no need to export the 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. The reason I added a type alias was because I get an "expression expected" error if directly adding it as in your suggestion. See microsoft/TypeScript#14757 on why TypeScript isn't supporting this. Should I move the type alias out of the namespace so it is not exported then? And if so, would you prefer for CamelCase that I change it to 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. Also, and I guess this might have been inadvertent in making the commit suggestion, we don't want to delete the properties in the interface. 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.
I've gone ahead and added a commit to do both of these things. Let me know if this is ok. |
||||||||||||||||||||||
|
||||||||||||||||||||||
export interface Options { | ||||||||||||||||||||||
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. Types in a namespace are always exported, so the It’s not wrong, but personally I prefer to remove them. 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. Ok. Applied. TS unfortunately isn't very helpful in their docs in helping give a better understanding of ambient namespaces like this; it seems to only speak there of browser global usage. I guess this may be because modules are moving to become the preferred way, and the fact that we still need to understand the old way when in use in CJS was lost along the way. |
||||||||||||||||||||||
allowNamespacedObjects?: boolean; | ||||||||||||||||||||||
allowNamespaces?: boolean; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export type TokContexts = { | ||||||||||||||||||||||
brettz9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
tc_oTag: acorn.TokContext, | ||||||||||||||||||||||
tc_cTag: acorn.TokContext, | ||||||||||||||||||||||
tc_expr: acorn.TokContext | ||||||||||||||||||||||
}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
type P = typeof acorn.Parser; | ||||||||||||||||||||||
|
||||||||||||||||||||||
// We pick (statics) from acorn rather than plain extending to avoid complaint | ||||||||||||||||||||||
// about base constructors needing the same return type (i.e., we return | ||||||||||||||||||||||
// `IAcornJsxParser` here) | ||||||||||||||||||||||
export interface AcornJsxParser extends Pick<P, keyof P> { | ||||||||||||||||||||||
brettz9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
readonly acornJsx: { | ||||||||||||||||||||||
tokTypes: TokTypes; | ||||||||||||||||||||||
tokContexts: TokContexts | ||||||||||||||||||||||
}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
new (options: acorn.Options, input: string, startPos?: number): IAcornJsxParser; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export interface IAcornJsxParser extends acorn.Parser { | ||||||||||||||||||||||
brettz9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
jsx_readToken(): string; | ||||||||||||||||||||||
jsx_readNewLine(normalizeCRLF: boolean): void; | ||||||||||||||||||||||
jsx_readString(quote: number): void; | ||||||||||||||||||||||
jsx_readEntity(): string; | ||||||||||||||||||||||
jsx_readWord(): void; | ||||||||||||||||||||||
jsx_parseIdentifier(): acorn.Node; | ||||||||||||||||||||||
jsx_parseNamespacedName(): acorn.Node; | ||||||||||||||||||||||
jsx_parseElementName(): acorn.Node | string; | ||||||||||||||||||||||
jsx_parseAttributeValue(): acorn.Node; | ||||||||||||||||||||||
jsx_parseEmptyExpression(): acorn.Node; | ||||||||||||||||||||||
jsx_parseExpressionContainer(): acorn.Node; | ||||||||||||||||||||||
jsx_parseAttribute(): acorn.Node; | ||||||||||||||||||||||
jsx_parseOpeningElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node; | ||||||||||||||||||||||
jsx_parseClosingElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node; | ||||||||||||||||||||||
jsx_parseElementAt(startPos: number, startLoc?: acorn.SourceLocation): acorn.Node; | ||||||||||||||||||||||
jsx_parseText(): acorn.Node; | ||||||||||||||||||||||
jsx_parseElement(): acorn.Node; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export = jsx; |
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.
If this is written as part of the namespace, it can be imported like this when transpiling to
commonjs
:Whether or not this is better, depends on the library’s intend and is subjective. I personally think that’s slightly better in this case, but I don’t have a very strong opinion on this.
Shouldn’t the type be like this?
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.
Regarding use of named exports, Node.js docs state:
So I'm wondering if that would be safe.
However, you are right that I should be using the jsx tokTypes instead of the Acorn one here. But it seems then that trying to fix this brings up two problems:
jsx
in this case will be understood as the exportedconst
rather than the namespace which we want. Renaming the namespace tojsxNS
, fixes the error but then the namespace isn't exported.interface TokTypes
to the top level without being redundant (i.e., I can have one copy at the top level which is not exported and one copy within the namespace which, as you say, is auto-exported).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.
I solved this I think by reexporting the non-exported interface with an exported type alias. Let me know if you think this is all suitable.