[Bug] SyntaxError antlr4 does not provide an export named 'DecisionState' when use typescript esm import
First of all thank you for your great open source work
- set tsconfig.json https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax to
true - compile https://github.com/antlr/grammars-v4/blob/master/java/java/JavaParser.g4 to
JavaParser.ts
// Generated from ./src/grammar/java/JavaParser.g4 by ANTLR 4.13.2
// noinspection ES6UnusedImports,JSUnusedGlobalSymbols,JSUnusedLocalSymbols
import {
ATN,
ATNDeserializer,
DecisionState, /// error
DFA,
FailedPredicateException,
NoViableAltException,
ParserATNSimulator,
ParserRuleContext,
PredictionContextCache,
RecognitionException,
RuleContext,
TerminalNode,
Token,
TokenStream, // error
} from 'antlr4';
import JavaParserListener from './JavaParserListener.js';
// for running tests with parameters, TODO: discuss strategy for typed parameters in CI
// eslint-disable-next-line no-unused-vars
// type int = number;
import JavaParserBase from './JavaParserBase.js';
here is my solution
import {
ATN,
ATNDeserializer,
- DecisionState,
+ type DecisionState,
DFA,
FailedPredicateException,
NoViableAltException,
ParserATNSimulator,
ParserRuleContext,
PredictionContextCache,
RecognitionException,
RuleContext,
TerminalNode,
Token,
- TokenStream,
+ type TokenStream,
} from 'antlr4';
or just set verbatimModuleSyntax to false
Is it possible to determine, when converting JavaParser.g4 to TypeScript, that when a type identifier is used solely as a type marker, its import statement can be changed to a type-only import statement?
NB: There is no port of the java/java/ grammar yet. I wrote the ports for CSharp and Java only. See the desc.xml.
Actually, for the antlr4 JavaScript module, its actual exports are fully known. It is only necessary to determine whether the identifier generated for export is in this list of string constants, and then update these variables to type-only imports. There is no need to actually analyze whether they are used as types or expressions during usage.
// just run it in your browser devtools
console.log(Object.keys(await import('https://cdn.jsdelivr.net/npm/antlr4')))
/*
[
"ATN",
"ATNDeserializer",
"BailErrorStrategy",
"CharStream",
"CharStreams",
"CommonToken",
"CommonTokenStream",
"DFA",
"DefaultErrorStrategy",
"DiagnosticErrorListener",
"ErrorListener",
"FailedPredicateException",
"InputMismatchException",
"InputStream",
"Interval",
"IntervalSet",
"LL1Analyzer",
"Lexer",
"LexerATNSimulator",
"NoViableAltException",
"ParseTreeListener",
"ParseTreeVisitor",
"ParseTreeWalker",
"Parser",
"ParserATNSimulator",
"ParserRuleContext",
"PredictionContextCache",
"PredictionMode",
"RecognitionException",
"RuleContext",
"RuleNode",
"TerminalNode",
"Token",
"TokenStreamRewriter",
"arrayToString",
"default"
]
*/