Skip to content

Commit b852ddc

Browse files
author
danrubel
committed
add parser parseAsync flag
add new ASYNC_NOT_SUPPORTED error code update ParseTask to support enableAsync add AnalysisOptions.enableAsync flag partially addresses #25373 [email protected] Review URL: https://codereview.chromium.org/1570183002 .
1 parent 8eb030b commit b852ddc

File tree

9 files changed

+125
-0
lines changed

9 files changed

+125
-0
lines changed

pkg/analyzer/lib/src/context/context.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class AnalysisContextImpl implements InternalAnalysisContext {
253253
this._options.enableStrictCallChecks !=
254254
options.enableStrictCallChecks ||
255255
this._options.enableGenericMethods != options.enableGenericMethods ||
256+
this._options.enableAsync != options.enableAsync ||
256257
this._options.enableSuperMixins != options.enableSuperMixins;
257258
int cacheSize = options.cacheSize;
258259
if (this._options.cacheSize != cacheSize) {
@@ -266,6 +267,7 @@ class AnalysisContextImpl implements InternalAnalysisContext {
266267
this._options.enableGenericMethods = options.enableGenericMethods;
267268
this._options.enableAssertMessage = options.enableAssertMessage;
268269
this._options.enableStrictCallChecks = options.enableStrictCallChecks;
270+
this._options.enableAsync = options.enableAsync;
269271
this._options.enableSuperMixins = options.enableSuperMixins;
270272
this._options.hint = options.hint;
271273
this._options.incremental = options.incremental;

pkg/analyzer/lib/src/generated/engine.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,11 @@ abstract class AnalysisOptions {
10461046
*/
10471047
bool get enableAssertMessage;
10481048

1049+
/**
1050+
* Return `true` to if analysis is to enable async support.
1051+
*/
1052+
bool get enableAsync;
1053+
10491054
/**
10501055
* Return `true` to enable interface libraries (DEP 40).
10511056
*/
@@ -1154,6 +1159,11 @@ class AnalysisOptionsImpl implements AnalysisOptions {
11541159
*/
11551160
bool enableAssertMessage = false;
11561161

1162+
/**
1163+
* A flag indicating whether analysis is to enable async support.
1164+
*/
1165+
bool enableAsync = true;
1166+
11571167
/**
11581168
* A flag indicating whether interface libraries are to be supported (DEP 40).
11591169
*/
@@ -1249,6 +1259,7 @@ class AnalysisOptionsImpl implements AnalysisOptions {
12491259
cacheSize = options.cacheSize;
12501260
dart2jsHint = options.dart2jsHint;
12511261
enableAssertMessage = options.enableAssertMessage;
1262+
enableAsync = options.enableAsync;
12521263
enableStrictCallChecks = options.enableStrictCallChecks;
12531264
enableGenericMethods = options.enableGenericMethods;
12541265
enableSuperMixins = options.enableSuperMixins;

pkg/analyzer/lib/src/generated/parser.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,11 @@ class Parser {
21072107
*/
21082108
int _errorListenerLock = 0;
21092109

2110+
/**
2111+
* A flag indicating whether the parser is to parse the async support.
2112+
*/
2113+
bool _parseAsync = true;
2114+
21102115
/**
21112116
* A flag indicating whether parser is to parse function bodies.
21122117
*/
@@ -2187,6 +2192,13 @@ class Parser {
21872192
return _tokenMatchesIdentifier(next);
21882193
}
21892194

2195+
/**
2196+
* Set whether the parser is to parse the async support.
2197+
*/
2198+
void set parseAsync(bool parseAsync) {
2199+
this._parseAsync = parseAsync;
2200+
}
2201+
21902202
/**
21912203
* Set whether parser is to parse function bodies.
21922204
*/
@@ -6087,13 +6099,19 @@ class Parser {
60876099
Token star = null;
60886100
if (_matchesString(ASYNC)) {
60896101
keyword = getAndAdvance();
6102+
if (!_parseAsync) {
6103+
_reportErrorForToken(ParserErrorCode.ASYNC_NOT_SUPPORTED, keyword);
6104+
}
60906105
if (_matches(TokenType.STAR)) {
60916106
star = getAndAdvance();
60926107
_inGenerator = true;
60936108
}
60946109
_inAsync = true;
60956110
} else if (_matchesString(SYNC)) {
60966111
keyword = getAndAdvance();
6112+
if (!_parseAsync) {
6113+
_reportErrorForToken(ParserErrorCode.ASYNC_NOT_SUPPORTED, keyword);
6114+
}
60976115
if (_matches(TokenType.STAR)) {
60986116
star = getAndAdvance();
60996117
_inGenerator = true;
@@ -9360,6 +9378,13 @@ class ParserErrorCode extends ErrorCode {
93609378
const ParserErrorCode('ASYNC_KEYWORD_USED_AS_IDENTIFIER',
93619379
"The keywords 'async', 'await', and 'yield' may not be used as identifiers in an asynchronous or generator function.");
93629380

9381+
/**
9382+
* Some environments, such as Fletch, do not support async.
9383+
*/
9384+
static const CompileTimeErrorCode ASYNC_NOT_SUPPORTED =
9385+
const CompileTimeErrorCode('ASYNC_NOT_SUPPORTED',
9386+
"Async and sync are not supported in this environment.");
9387+
93639388
static const ParserErrorCode BREAK_OUTSIDE_OF_LOOP = const ParserErrorCode(
93649389
'BREAK_OUTSIDE_OF_LOOP',
93659390
"A break statement cannot be used outside of a loop or switch statement");

pkg/analyzer/lib/src/generated/utilities_general.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ library analyzer.src.generated.utilities_general;
77
import 'dart:collection';
88
import 'dart:developer' show UserTag;
99

10+
/**
11+
* Test if the given [value] is `false` or the string "false" (case-insensitive).
12+
*/
13+
bool isFalse(Object value) =>
14+
value is bool ? !value : toLowerCase(value) == 'false';
15+
1016
/**
1117
* Test if the given [value] is `true` or the string "true" (case-insensitive).
1218
*/

pkg/analyzer/lib/src/task/dart.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,6 +3550,7 @@ class ParseDartTask extends SourceBasedAnalysisTask {
35503550
RecordingErrorListener errorListener = new RecordingErrorListener();
35513551
Parser parser = new Parser(source, errorListener);
35523552
AnalysisOptions options = context.analysisOptions;
3553+
parser.parseAsync = options.enableAsync;
35533554
parser.parseFunctionBodies = options.analyzeFunctionBodiesPredicate(source);
35543555
parser.parseGenericMethods = options.enableGenericMethods;
35553556
parser.parseGenericMethodComments = options.strongMode;

pkg/analyzer/lib/src/task/options.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void configureContextOptions(
3939
/// `analyzer` analysis options constants.
4040
class AnalyzerOptions {
4141
static const String analyzer = 'analyzer';
42+
static const String enableAsync = 'enableAsync';
4243
static const String enableGenericMethods = 'enableGenericMethods';
4344
static const String enableSuperMixins = 'enableSuperMixins';
4445
static const String errors = 'errors';
@@ -71,6 +72,7 @@ class AnalyzerOptions {
7172

7273
/// Supported `analyzer` language configuration options.
7374
static const List<String> languageOptions = const [
75+
enableAsync,
7476
enableGenericMethods,
7577
enableSuperMixins
7678
];
@@ -425,6 +427,14 @@ class _OptionsProcessor {
425427

426428
void setLanguageOption(
427429
AnalysisContext context, Object feature, Object value) {
430+
if (feature == AnalyzerOptions.enableAsync) {
431+
if (isFalse(value)) {
432+
AnalysisOptionsImpl options =
433+
new AnalysisOptionsImpl.from(context.analysisOptions);
434+
options.enableAsync = false;
435+
context.analysisOptions = options;
436+
}
437+
}
428438
if (feature == AnalyzerOptions.enableSuperMixins) {
429439
if (isTrue(value)) {
430440
AnalysisOptionsImpl options =

pkg/analyzer/test/generated/parser_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,18 @@ class ErrorParserTest extends ParserTestCase {
937937
[ParserErrorCode.DUPLICATE_LABEL_IN_SWITCH_STATEMENT]);
938938
}
939939

940+
void test_enableAsync_false_1() {
941+
parseAsync = false;
942+
parse4("parseFunctionDeclarationStatement",
943+
"foo() async {}", [ParserErrorCode.ASYNC_NOT_SUPPORTED]);
944+
}
945+
946+
void test_enableAsync_false_2() {
947+
parseAsync = false;
948+
parse4("parseFunctionDeclarationStatement",
949+
"foo() sync* {}", [ParserErrorCode.ASYNC_NOT_SUPPORTED]);
950+
}
951+
940952
void test_emptyEnumBody() {
941953
parse3("parseEnumDeclaration", <Object>[emptyCommentAndMetadata()],
942954
"enum E {}", [ParserErrorCode.EMPTY_ENUM_BODY]);
@@ -2646,6 +2658,11 @@ class ParserTestCase extends EngineTestCase {
26462658
*/
26472659
static bool parseFunctionBodies = true;
26482660

2661+
/**
2662+
* A flag indicating whether parser is to parse async.
2663+
*/
2664+
bool parseAsync = true;
2665+
26492666
/**
26502667
* A flag indicating whether conditional directives support should be enabled
26512668
* for a specific test.
@@ -2713,6 +2730,7 @@ class ParserTestCase extends EngineTestCase {
27132730
// Parse the source.
27142731
//
27152732
Parser parser = createParser(listener);
2733+
parser.parseAsync = parseAsync;
27162734
parser.parseConditionalDirectives = enableConditionalDirectives;
27172735
parser.parseGenericMethods = enableGenericMethods;
27182736
parser.parseGenericMethodComments = enableGenericMethodComments;

pkg/analyzer/test/src/task/dart_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,41 @@ class B {}''');
29932993
expect(outputs[UNITS], hasLength(1));
29942994
}
29952995

2996+
test_perform_enableAsync_false() {
2997+
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
2998+
options.enableAsync = false;
2999+
prepareAnalysisContext(options);
3000+
_performParseTask(r'''
3001+
import 'dart:async';
3002+
class B {void foo() async {}}''');
3003+
expect(outputs, hasLength(9));
3004+
expect(outputs[EXPLICITLY_IMPORTED_LIBRARIES], hasLength(1));
3005+
expect(outputs[EXPORTED_LIBRARIES], hasLength(0));
3006+
_assertHasCore(outputs[IMPORTED_LIBRARIES], 2);
3007+
expect(outputs[INCLUDED_PARTS], hasLength(0));
3008+
expect(outputs[LIBRARY_SPECIFIC_UNITS], hasLength(1));
3009+
expect(outputs[PARSE_ERRORS], hasLength(1));
3010+
expect(outputs[PARSED_UNIT], isNotNull);
3011+
expect(outputs[SOURCE_KIND], SourceKind.LIBRARY);
3012+
expect(outputs[UNITS], hasLength(1));
3013+
}
3014+
3015+
test_perform_enableAsync_true() {
3016+
_performParseTask(r'''
3017+
import 'dart:async';
3018+
class B {void foo() async {}}''');
3019+
expect(outputs, hasLength(9));
3020+
expect(outputs[EXPLICITLY_IMPORTED_LIBRARIES], hasLength(1));
3021+
expect(outputs[EXPORTED_LIBRARIES], hasLength(0));
3022+
_assertHasCore(outputs[IMPORTED_LIBRARIES], 2);
3023+
expect(outputs[INCLUDED_PARTS], hasLength(0));
3024+
expect(outputs[LIBRARY_SPECIFIC_UNITS], hasLength(1));
3025+
expect(outputs[PARSE_ERRORS], hasLength(0));
3026+
expect(outputs[PARSED_UNIT], isNotNull);
3027+
expect(outputs[SOURCE_KIND], SourceKind.LIBRARY);
3028+
expect(outputs[UNITS], hasLength(1));
3029+
}
3030+
29963031
test_perform_computeSourceKind_noDirectives_hasContainingLibrary() {
29973032
// Parse "lib.dart" to let the context know that "test.dart" is included.
29983033
computeResult(

pkg/analyzer/test/src/task/options_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ analyzer:
5050
expect(analysisOptions.strongMode, false);
5151
}
5252

53+
test_configure_enableAsync() {
54+
configureContext('''
55+
analyzer:
56+
language:
57+
''');
58+
expect(analysisOptions.enableAsync, true);
59+
}
60+
61+
test_configure_enableAsync_false() {
62+
configureContext('''
63+
analyzer:
64+
language:
65+
enableAsync: false
66+
''');
67+
expect(analysisOptions.enableAsync, false);
68+
}
69+
5370
test_configure_enableGenericMethods() {
5471
expect(analysisOptions.enableGenericMethods, false);
5572
configureContext('''

0 commit comments

Comments
 (0)