123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- 'use strict';
- var acorn = require('acorn');
- require('../')(acorn);
- var estraverse = require('estraverse');
- var xtend = require('xtend');
- var assert = require('assert');
- function find (type, ast, skip) {
- skip = skip || 0;
- var skipped = 0;
- var found;
- estraverse.traverse(ast, {
- enter: (node) => {
- if (found) {
- return estraverse.VisitorOption.Skip;
- }
- if (node.type == type) {
- if (skipped === skip) {
- found = node;
- return estraverse.VisitorOption.Skip;
- }
- skipped++;
- }
- }
- });
- if (!found) {
- throw new Error('did not find AwaitExpression (skipped ' + skipped + '/' + skip + ')');
- }
- return found;
- }
- function extendOptions(pluginOptions, acornOptions) {
- return xtend({
- sourceType: 'module',
- ecmaVersion: 8,
- locations: true,
- ranges: true,
- plugins: {asyncawait: pluginOptions || pluginOptions !== false}
- }, acornOptions);
- }
- function parse(code, pluginOptions, acornOptions) {
- if (Array.isArray(code)) {
- code = code.join('\n');
- }
- var options = extendOptions(pluginOptions, acornOptions);
- return acorn.parse(code, options);
- }
- describe('async', () => {
- describe ('function declaration', () => {
- var node;
- describe('-', () => {
- beforeEach(() => {
- node = find(
- 'FunctionDeclaration',
- parse([
- 'async function foo() {',
- ' x = await bar()',
- '}'
- ])
- );
- });
- it('marks the node as async', () =>
- assert(node.async)
- );
- it('finds correct start position', () =>
- assert.strictEqual(node.start, 0)
- );
- it('finds correct end position', () =>
- assert.strictEqual(node.end, 42)
- );
- it('finds correct start line/column', () =>
- assert.deepEqual(node.loc.start, {
- line: 1,
- column: 0
- })
- );
- it('finds correct end line/column', () =>
- assert.deepEqual(node.loc.end, {
- line: 3,
- column: 1
- })
- );
- });
- var assertFindsIdentifierExpressionStatement = (ast) => {
- node = find('ExpressionStatement', ast);
- assert.strictEqual(node.expression.type, 'Identifier');
- assert.strictEqual(node.expression.name, 'async');
- assert.deepEqual(node.expression.loc, {
- start: {
- line: 1,
- column: 0
- },
- end: {
- line: 1,
- column: 5
- }
- });
- };
- describe('linefeed after async (simple)', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'async \t\t ',
- 'function foo() {',
- '}'
- ]);
- });
- it('finds Identifier ExpressionStatement', () => {
- assertFindsIdentifierExpressionStatement(ast);
- });
- it('does not mark FunctionDeclaration as async', () => {
- node = find('FunctionDeclaration', ast);
- assert(!node.async, 'Expected node.async to be false');
- });
- });
- describe('linefeed after async (single line comment)', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'async // flag enables async completion',
- 'function foo() {',
- '}'
- ]);
- });
- it('finds Identifier ExpressionStatement', () => {
- assertFindsIdentifierExpressionStatement(ast);
- });
- it('does not mark FunctionDeclaration as async', () => {
- node = find('FunctionDeclaration', ast);
- assert(!node.async, 'Expected node.async to be false');
- });
- });
- describe('linefeed after async (multiline comment) function', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'async /* flag enables async completion',
- ' of the callback */function foo() {',
- '}'
- ]);
- });
- it('finds Identifier ExpressionStatement', () => {
- assertFindsIdentifierExpressionStatement(ast);
- });
- it('does not mark FunctionDeclaration as async', () => {
- node = find('FunctionDeclaration', ast);
- assert(!node.async, 'Expected node.async to be false');
- });
- });
- });
- describe ('function expression', () => {
- var node, code;
- describe('-', () => {
- beforeEach(() => {
- code = [
- 'foo = async function () {',
- ' x = await bar()',
- '}'
- ];
- node = find(
- 'FunctionExpression',
- parse(code)
- );
- });
- it('marks the node as async', () =>
- assert(node.async)
- );
- it('finds correct start position', () =>
- assert.strictEqual(node.start, 6)
- );
- it('finds correct end position', () =>
- assert.strictEqual(node.end, code.join('\n').length)
- );
- it('finds correct start line/column', () =>
- assert.deepEqual(node.loc.start, {
- line: 1,
- column: 6
- })
- );
- it('finds correct end line/column', () =>
- assert.deepEqual(node.loc.end, {
- line: 3,
- column: 1
- })
- );
- });
- var assertFindsIdentifierAssignmentExpressionRHS = (ast) => {
- node = find('AssignmentExpression', ast);
- assert.strictEqual(node.right.type, 'Identifier');
- assert.strictEqual(node.right.name, 'async');
- assert.deepEqual(node.right.loc, {
- start: {
- line: 1,
- column: 6
- },
- end: {
- line: 1,
- column: 11
- }
- });
- };
- describe('linefeed after async (simple)', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'foo = async \t\t ',
- ', function() {',
- '}'
- ]);
- });
- it('finds Identifier ExpressionStatement', () => {
- assertFindsIdentifierAssignmentExpressionRHS(ast);
- });
- it('does not mark FunctionExpression as async', () => {
- node = find('FunctionExpression', ast);
- assert(!node.async, 'Expected node.async to be false');
- });
- });
- describe('linefeed after async (single line comment)', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'foo = async // flag enables async completion',
- ', function() {',
- '}'
- ]);
- });
- it('finds Identifier ExpressionStatement', () => {
- assertFindsIdentifierAssignmentExpressionRHS(ast);
- });
- it('does not mark FunctionExpression as async', () => {
- node = find('FunctionExpression', ast);
- assert(!node.async, 'Expected node.async to be false');
- });
- });
- describe('linefeed after async (multiline comment), function', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'foo = async /* flag enables async completion',
- ' of the callback */, function() {',
- '}'
- ]);
- });
- it('finds Identifier ExpressionStatement', () => {
- assertFindsIdentifierAssignmentExpressionRHS(ast);
- });
- it('does not mark FunctionExpression as async', () => {
- node = find('FunctionExpression', ast);
- assert(!node.async, 'Expected node.async to be false');
- });
- });
- });
- describe ('enhanced object literal', () => {
- var node, code;
- describe('-', () => {
- beforeEach(() => {
- code = [
- 'var x = {',
- ' async foo() {}',
- '};'
- ];
- node = find(
- // TODO: Is it really supposed to mark the Property async? Why not the FunctionExpression?
- 'Property',
- parse(code)
- );
- });
- it('marks the node value as async', () =>
- assert(node.value.async)
- );
- it('does not mark the node as async', () =>
- assert(!node.async)
- );
- it('finds correct start position', () =>
- assert.strictEqual(node.start, 12)
- );
- it('finds correct end position', () =>
- assert.strictEqual(node.end, code[0].length + code[1].length + 1) // + 1 is due to newline char
- );
- it('finds correct start line/column', () =>
- assert.deepEqual(node.loc.start, {
- line: 2,
- column: 2
- })
- );
- it('finds correct end line/column', () =>
- assert.deepEqual(node.loc.end, {
- line: 2,
- column: 16
- })
- );
- });
- describe('linefeed after async (simple)', () => {
- it('fails to parse', () => {
- assert.throws(() => parse([
- 'var x = {',
- ' async \t\t ',
- ' foo() {}',
- '};'
- ]));
- });
- });
- describe('linefeed after async (single line comment)', () => {
- it('fails to parse', () => {
- assert.throws(() => parse([
- 'var x = {',
- ' async // flag enables async completion',
- ' foo() {}',
- '};'
- ]));
- });
- });
- describe('linefeed after async (multiline comment) illegal decl', () => {
- it('finds Identifier ExpressionStatement', () => {
- assert.throws(() => parse([
- 'var x = {',
- ' async /* flag enables async completion',
- ' of the callback */ foo() {}',
- '};'
- ]));
- });
- });
- });
- describe ('ArrowFunctionExpression', () => {
- var node, code;
- describe('-', () => {
- beforeEach(() => {
- code = 'var x = async () => {}';
- node = find(
- 'ArrowFunctionExpression',
- parse(code)
- );
- });
- it('marks the node as async', () =>
- assert(node.async)
- );
- it('finds correct start position', () =>
- assert.strictEqual(node.start, 8)
- );
- it('finds correct end position', () =>
- assert.strictEqual(node.end, code.length)
- );
- it('finds correct start line/column', () =>
- assert.deepEqual(node.loc.start, {
- line: 1,
- column: 8
- })
- );
- it('finds correct end line/column', () =>
- assert.deepEqual(node.loc.end, {
- line: 1,
- column: code.length
- })
- );
- });
- describe('linefeed after async (simple)', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'var x = async \t\t ',
- '()'
- ]);
- });
- it('fails to parse if linefeed preceeds arrow arguments', () => {
- assert.throws(() => parse([
- 'var x = async \t\t ',
- '() => {}'
- ]));
- });
- it('finds CallExpression with "async" Identifier callee', () => {
- node = find('CallExpression', ast);
- assert.strictEqual(node.callee.type, 'Identifier');
- assert.strictEqual(node.callee.name, 'async');
- assert.deepEqual(node.callee.loc, {
- start: {
- line: 1,
- column: 8
- },
- end: {
- line: 1,
- column: 13
- }
- });
- });
- });
- describe('linefeed after async (single line comment)', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'var x = async // flag enables async completion',
- '()'
- ]);
- });
- it('fails to parse if linefeed preceeds arrow arguments', () => {
- assert.throws(() => parse([
- 'var x = async \t\t ',
- '() => {}'
- ]));
- });
- it('finds CallExpression with "async" Identifier callee', () => {
- node = find('CallExpression', ast);
- assert.strictEqual(node.callee.type, 'Identifier');
- assert.strictEqual(node.callee.name, 'async');
- assert.deepEqual(node.callee.loc, {
- start: {
- line: 1,
- column: 8
- },
- end: {
- line: 1,
- column: 13
- }
- });
- });
- });
- describe('linefeed after async (multiline comment) arrow decl', () => {
- var ast;
- beforeEach(() => {
- ast = parse([
- 'var x = async /* flag enables async completion',
- ' of the callback */()'
- ]);
- });
- it('fails to parse if linefeed preceeds arrow arguments', () => {
- assert.throws(() => parse([
- 'var x = async /* flag enables async completion',
- ' of the callback */() => {}'
- ]));
- });
- it('finds CallExpression with "async" Identifier callee', () => {
- node = find('CallExpression', ast);
- assert.strictEqual(node.callee.type, 'Identifier');
- assert.strictEqual(node.callee.name, 'async');
- assert.deepEqual(node.callee.loc, {
- start: {
- line: 1,
- column: 8
- },
- end: {
- line: 1,
- column: 13
- }
- });
- });
- });
- });
- });
- describe('await', () => {
- describe('-', () => {
- var node;
- beforeEach(() => {
- node = find(
- 'AwaitExpression',
- parse([
- 'async function foo() {',
- ' x = await bar()',
- '}'
- ])
- );
- });
- it('finds correct start position', () =>
- assert.strictEqual(node.start, 29)
- );
- it('finds correct end position', () =>
- assert.strictEqual(node.end, 40)
- );
- it('finds correct start line/column', () =>
- assert.deepEqual(node.loc.start, {
- line: 2,
- column: 6
- })
- );
- it('finds correct end line/column', () =>
- assert.deepEqual(node.loc.end, {
- line: 2,
- column: 17
- })
- );
- });
- describe('outside a function (awaitAnywhere)', () => {
- var node;
- beforeEach(() => {
- node = find(
- 'AwaitExpression',
- parse(
- 'x = await bar()',
- {awaitAnywhere:true}
- )
- );
- });
- it('finds correct start position', () =>
- assert.strictEqual(node.start, 4)
- );
- it('finds correct start line/column', () =>
- assert.deepEqual(node.loc.start, {
- line: 1,
- column: 4
- })
- );
- it('finds correct end position', () =>
- assert.strictEqual(node.end, 15)
- );
- it('finds correct end line/column', () =>
- assert.deepEqual(node.loc.end, {
- line: 1,
- column: 15
- })
- );
- });
- });
|