Skip to content

Commit 0fa50bc

Browse files
zavr-1tjgq
authored andcommitted
Add error message for using import() in a type annotation
Closes #3323 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=244704722
1 parent dabb9f9 commit 0fa50bc

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

src/com/google/javascript/jscomp/DiagnosticGroups.java

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public static DiagnosticGroup forName(String name) {
228228
"nonStandardJsDocs",
229229
RhinoErrorReporter.BAD_JSDOC_ANNOTATION,
230230
RhinoErrorReporter.INVALID_PARAM,
231+
RhinoErrorReporter.JSDOC_IMPORT_TYPE_WARNING,
231232
CheckJSDoc.JSDOC_IN_BLOCK_COMMENT);
232233

233234
public static final DiagnosticGroup INVALID_CASTS =

src/com/google/javascript/jscomp/RhinoErrorReporter.java

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class RhinoErrorReporter {
4848
static final DiagnosticType JSDOC_MISSING_TYPE_WARNING =
4949
DiagnosticType.disabled("JSC_JSDOC_MISSING_TYPE_WARNING", "{0}");
5050

51+
// Import is supported by VSCode and is pretty much a standard now.
52+
static final DiagnosticType JSDOC_IMPORT_TYPE_WARNING =
53+
DiagnosticType.disabled("JSC_JSDOC_IMPORT_TYPE_WARNING", "{0}");
54+
5155
static final DiagnosticType TOO_MANY_TEMPLATE_PARAMS =
5256
DiagnosticType.disabled("JSC_TOO_MANY_TEMPLATE_PARAMS", "{0}");
5357

@@ -153,6 +157,11 @@ private RhinoErrorReporter(AbstractCompiler compiler) {
153157
// Unresolved types that aren't forward declared.
154158
.put(Pattern.compile(".*Unknown type.*"), UNRECOGNIZED_TYPE_ERROR)
155159

160+
// Import annotation errors.
161+
.put(
162+
Pattern.compile("^Bad type annotation. Import in typedef.*"),
163+
JSDOC_IMPORT_TYPE_WARNING)
164+
156165
// Type annotation errors.
157166
.put(Pattern.compile("^Bad type annotation.*"), TYPE_PARSE_ERROR)
158167

src/com/google/javascript/jscomp/parsing/JsDocInfoParser.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,11 @@ private Node parseTypeExpressionAnnotation(JsDocToken token) {
19391939
if (typeNode != null) {
19401940
skipEOLs();
19411941
if (!match(JsDocToken.RIGHT_CURLY)) {
1942-
reportTypeSyntaxWarning("msg.jsdoc.missing.rc");
1942+
if (typeNode.isString() && "import".equals(typeNode.getString())) {
1943+
reportTypeSyntaxWarning("msg.jsdoc.import");
1944+
} else {
1945+
reportTypeSyntaxWarning("msg.jsdoc.missing.rc");
1946+
}
19431947
} else {
19441948
next();
19451949
}

src/com/google/javascript/jscomp/resources.json

+1-1
Large diffs are not rendered by default.

src/com/google/javascript/rhino/Messages.properties

+3
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,6 @@ msg.jsdoc.typetransformation.extra.param =\
351351

352352
msg.jsdoc.typetransformation.invalid.inside =\
353353
Invalid expression inside {0}
354+
355+
msg.jsdoc.import =\
356+
Import in typedef is not supported.

test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,13 @@ public void testMalformedThisAnnotation() {
691691
"Bad type annotation. type not recognized due to syntax error." + BAD_TYPE_WIKI_LINK);
692692
}
693693

694+
@Test
695+
public void testParseImportTypeError() {
696+
parse(
697+
"@type {import('http').Stream} */",
698+
"Bad type annotation. Import in typedef is not supported." + BAD_TYPE_WIKI_LINK);
699+
}
700+
694701
@Test
695702
public void testParseFunctionalTypeError1() {
696703
parse(

0 commit comments

Comments
 (0)