Search in sources :

Example 1 with ExportDeclarationTree

use of com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree in project closure-compiler by google.

the class Parser method parseExportDeclaration.

/*
  ExportDeclaration :
    export * FromClause ;
    export ExportClause [NoReference] FromClause ;
    export ExportClause ;
    export VariableStatement
    export Declaration[Default]
    export default AssignmentExpression ;
  ExportClause [NoReference] :
    { }
    { ExportsList [?NoReference] }
    { ExportsList [?NoReference] , }
  ExportsList [NoReference] :
    ExportSpecifier [?NoReference]
    ExportsList [?NoReference] , ExportSpecifier [?NoReference]
  ExportSpecifier [NoReference] :
    [~NoReference] IdentifierReference
    [~NoReference] IdentifierReference as IdentifierName
    [+NoReference] IdentifierName
    [+NoReference] IdentifierName as IdentifierName
   */
private ParseTree parseExportDeclaration(boolean isAmbient) {
    SourcePosition start = getTreeStartLocation();
    boolean isDefault = false;
    boolean isExportAll = false;
    boolean isExportSpecifier = false;
    boolean needsSemiColon = true;
    eat(TokenType.EXPORT);
    ParseTree export = null;
    ImmutableList<ParseTree> exportSpecifierList = null;
    switch(peekType()) {
        case STAR:
            isExportAll = true;
            nextToken();
            break;
        case FUNCTION:
            // TODO(bradfordcsmith): handle async functions here
            export = isAmbient ? parseAmbientFunctionDeclaration() : parseFunctionDeclaration();
            needsSemiColon = isAmbient;
            break;
        case CLASS:
            export = parseClassDeclaration(isAmbient);
            needsSemiColon = false;
            break;
        case INTERFACE:
            export = parseInterfaceDeclaration();
            needsSemiColon = false;
            break;
        case ENUM:
            export = parseEnumDeclaration();
            needsSemiColon = false;
            break;
        case MODULE:
        case NAMESPACE:
            export = parseNamespaceDeclaration(isAmbient);
            needsSemiColon = false;
            break;
        case DECLARE:
            export = parseAmbientDeclaration();
            needsSemiColon = false;
            break;
        case DEFAULT:
            isDefault = true;
            nextToken();
            export = parseExpression();
            needsSemiColon = false;
            break;
        case OPEN_CURLY:
            isExportSpecifier = true;
            exportSpecifierList = parseExportSpecifierSet();
            break;
        case TYPE:
            export = parseTypeAlias();
            break;
        case VAR:
        case LET:
        case CONST:
        default:
            // unreachable, parse as a var decl to get a parse error.
            export = isAmbient ? parseAmbientVariableDeclarationList() : parseVariableDeclarationList();
            break;
    }
    LiteralToken moduleSpecifier = null;
    if (isExportAll || (isExportSpecifier && peekPredefinedString(PredefinedName.FROM))) {
        eatPredefinedString(PredefinedName.FROM);
        moduleSpecifier = (LiteralToken) eat(TokenType.STRING);
    } else if (isExportSpecifier) {
        for (ParseTree tree : exportSpecifierList) {
            IdentifierToken importedName = tree.asExportSpecifier().importedName;
            if (isKeyword(importedName.value)) {
                reportError(importedName, "cannot use keyword '%s' here.", importedName.value);
            }
        }
    }
    if (needsSemiColon || peekImplicitSemiColon()) {
        eatPossibleImplicitSemiColon();
    }
    return new ExportDeclarationTree(getTreeLocation(start), isDefault, isExportAll, export, exportSpecifierList, moduleSpecifier);
}
Also used : ExportDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 2 with ExportDeclarationTree

use of com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree in project closure-compiler by google.

the class Parser method parseExportDeclaration.

/*
  ExportDeclaration :
    export * FromClause ;
    export ExportClause [NoReference] FromClause ;
    export ExportClause ;
    export VariableStatement
    export Declaration[Default]
    export default AssignmentExpression ;
  ExportClause [NoReference] :
    { }
    { ExportsList [?NoReference] }
    { ExportsList [?NoReference] , }
  ExportsList [NoReference] :
    ExportSpecifier [?NoReference]
    ExportsList [?NoReference] , ExportSpecifier [?NoReference]
  ExportSpecifier [NoReference] :
    [~NoReference] IdentifierReference
    [~NoReference] IdentifierReference as IdentifierName
    [+NoReference] IdentifierName
    [+NoReference] IdentifierName as IdentifierName
   */
private ParseTree parseExportDeclaration() {
    SourcePosition start = getTreeStartLocation();
    boolean isDefault = false;
    boolean isExportAll = false;
    boolean isExportSpecifier = false;
    boolean needsSemiColon = true;
    eat(TokenType.EXPORT);
    ParseTree export = null;
    ImmutableList<ParseTree> exportSpecifierList = null;
    switch(peekType()) {
        case STAR:
            isExportAll = true;
            nextToken();
            break;
        case IDENTIFIER:
            export = parseAsyncFunctionDeclaration();
            break;
        case FUNCTION:
            export = parseFunctionDeclaration();
            needsSemiColon = false;
            break;
        case CLASS:
            export = parseClassDeclaration();
            needsSemiColon = false;
            break;
        case DEFAULT:
            isDefault = true;
            nextToken();
            export = parseExpression();
            needsSemiColon = false;
            break;
        case OPEN_CURLY:
            isExportSpecifier = true;
            exportSpecifierList = parseExportSpecifierSet();
            break;
        case VAR:
        case LET:
        case CONST:
        default:
            // unreachable, parse as a var decl to get a parse error.
            export = parseVariableDeclarationList();
            break;
    }
    LiteralToken moduleSpecifier = null;
    if (isExportAll || (isExportSpecifier && peekPredefinedString(PredefinedName.FROM))) {
        eatPredefinedString(PredefinedName.FROM);
        moduleSpecifier = (LiteralToken) eat(TokenType.STRING);
    } else if (isExportSpecifier) {
        for (ParseTree tree : exportSpecifierList) {
            IdentifierToken importedName = tree.asExportSpecifier().importedName;
            if (isKeyword(importedName.value)) {
                reportError(importedName, "cannot use keyword '%s' here.", importedName.value);
            }
        }
    }
    if (needsSemiColon || peekImplicitSemiColon()) {
        eatPossiblyImplicitSemiColon();
    }
    return new ExportDeclarationTree(getTreeLocation(start), isDefault, isExportAll, export, exportSpecifierList, moduleSpecifier);
}
Also used : ExportDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

ExportDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree)2 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)2 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)2