Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 521627 - Provide function templates to create an async function (#…
  • Loading branch information
mrennie committed Aug 30, 2017
1 parent f543705 commit c5c5e88
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
Expand Up @@ -577,6 +577,28 @@ define([
return null;
},

/**
* @description Returns if the given node is within an async function declaration or expression
* @param {?} node The AST node
* @since 16.0
* @returns {boolean} Returns true if the node or one of its direct parents is an async function. False otherwise
*/
inAsync: function inAsync(node) {
if(node) {
if(node.async) {
return true;
}
if(Array.isArray(node.parents)) {
for(var i = 0, len = node.parents.length; i < len; i++) {
if(node.parents[i].async) {
return true;
}
}
}
}
return false;
},

/**
* @description Computes the kind of context to complete in
* @param {Object} ast The backing AST to visit
Expand All @@ -587,24 +609,26 @@ define([
findCompletionKind: function findCompletionKind(ast, offset) {
var node = this.findNode(offset, ast, {parents:true});
if(node) {
var isasync = this.inAsync(node);
var k = {kind: 'top', isasync: isasync};
if(node.type === 'Literal') {
switch(typeof node.value) {
case 'boolean':
case 'number': {
if(offset > node.range[0] && offset <= node.range[1]) {
return {kind: 'unknown'};
k.kind = 'unknown';
}
break;
}
case 'string': {
if(offset > node.range[0] && offset < node.range[1]) {
return {kind: 'string'};
k.kind = 'string';
}
break;
}
case 'object': {
if(node.regex && offset > node.range[0] && offset <= node.range[1]) {
return {kind: 'regex'};
k.kind = 'regex';
}
break;
}
Expand All @@ -614,36 +638,39 @@ define([
var prent = node.parents.pop();
switch(prent.type) {
case 'MemberExpression':
return { kind : 'member'}; //$NON-NLS-1$
k.kind = 'member';
break;
case 'Program':
case 'BlockStatement':
break;
case 'VariableDeclarator':
if(!prent.init || offset < prent.init.range[0]) {
return {kind: 'unknown'};
if(!prent.init || prent.init.name === '✖' || offset < prent.init.range[0]) {
k.kind = 'assign';
}
break;
case 'FunctionDeclaration':
case 'FunctionExpression':
if(offset < prent.body.range[0]) {
return {kind: 'unknown'};
k.kind = 'unknown';
}
break;
case 'Property':
k.kind = 'unknown';
if(offset-1 >= prent.value.range[0] && offset-1 <= prent.value.range[1]) {
return { kind : 'prop'}; //$NON-NLS-1$
k.kind = 'prop';
}
return {kind: 'unknown'};
break;
case 'SwitchStatement':
return {kind: 'swtch'}; //$NON-NLS-1$
k.kind = 'swtch';
}
}
}
node = Finder.findComment(offset, ast);
if(node) {
return {kind: 'doc', node: node}; //$NON-NLS-1$
k.kind = 'doc';
k.node = node;
}
return {kind:'top'}; //$NON-NLS-1$
return k;
},
/**
* @description Returns the templates that apply to the given completion kind
Expand Down
Expand Up @@ -228,8 +228,8 @@ define([
return null;
});
};

/**
/**
* @description Fetch the children of the named child folder of the current project context
* @function
* @param {String} childName The short name of the project child to get
Expand Down Expand Up @@ -489,7 +489,7 @@ define([
return project.getESlintOptions().then(function(options) {
processEslintOptions(project, options);
if(typeof project.map.env.ecmaVersion !== 'number') {
project.map.env.ecmaVersion = 6;
project.map.env.ecmaVersion = 7;
}
return project.getFile(project.NODE_MODULES).then(function(file) {
if(file && typeof file.contents === "string") {
Expand Down Expand Up @@ -563,12 +563,15 @@ define([
}
}
if(typeof vals.ecmaVersion === 'number') {
if(vals.ecmaVersion > 4 && vals.ecmaVersion < 8) {
if(vals.ecmaVersion > 4 && vals.ecmaVersion <= 9) {
project.map.env.envs.es6 = vals.ecmaVersion >= 6;
project.map.env.envs.es7 = vals.ecmaVersion >= 7;
project.map.env.envs.es8 = vals.ecmaVersion >= 8;
project.map.env.ecmaVersion = vals.ecmaVersion;
} else {
project.map.env.ecmaVersion = 6;
project.map.env.ecmaVersion = 7;
project.map.env.envs.es6 = true;
project.map.env.envs.es7 = true;
}
}
if(vals.sourceType === 'modules') {
Expand Down
Expand Up @@ -479,6 +479,9 @@ define({
'yieldSimple': 'Create a new yield statement.',
'yieldSimpleReturn': 'Create a new yield statement that returns the optional value passed to the generator\'s next() method.',
'yieldDelegate': 'Create a new yield* statement that is used to delegate to another generator.',
'asyncFunction': 'The async function declaration defines an asynchronous function, which returns an AsyncFunction object.',
'asyncFunctionExpression': 'he async function keyword can be used to define async functions inside expressions.',
'awaitExpression': 'The await operator is used to wait for a Promise. It can only be used inside an async function.',
'ifSimple': 'Create a new if statement.',
'ifElseSimple': 'Create a new if..else statement.',
'forArray': 'Create a new for loop that iterates over an array.',
Expand Down
Expand Up @@ -39,6 +39,55 @@ define([
* @private
*/
var templates = [
{
prefix: "async", //$NON-NLS-1$
name: "async function", //$NON-NLS-1$
nodes: {top:true, member:false, prop:false},
template: "async function ${name}(${param}) {\n"+
" ${cursor}\n"+
"}",
url: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function",
doc: Messages.asyncFunction,
ecma: 9
},
{
prefix: "async", //$NON-NLS-1$
name: "async function expression", //$NON-NLS-1$
nodes: {top:true, member:false, prop:false},
template: "var ${name} = async function ${name}(${param}) {\n"+
" ${cursor}\n"+
"};",
url: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/async_function",
doc: Messages.asyncFunctionExpression,
ecma: 9
},
{
prefix: "await", //$NON-NLS-1$
name: "await statement", //$NON-NLS-1$
nodes: {top:true, assign: true, async:true, member:false, prop:false},
template: "await ${statement};", //$NON-NLS-1$
url: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await",
doc: Messages.awaitExpression,
ecma: 9
},
{
prefix: "await", //$NON-NLS-1$
name: "await return statement", //$NON-NLS-1$
nodes: {top:false, async:true, member:false, prop:false},
template: "return await ${statement};", //$NON-NLS-1$
url: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await",
doc: Messages.awaitExpression,
ecma: 9
},
{
prefix: "await", //$NON-NLS-1$
name: "await variable statement", //$NON-NLS-1$
nodes: {top:true, async:true, member:false, prop:false},
template: "var ${variable} = await ${statement};", //$NON-NLS-1$
url: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await",
doc: Messages.awaitExpression,
ecma: 9
},
{
prefix: "if", //$NON-NLS-1$
name: "if statement", //$NON-NLS-1$
Expand Down Expand Up @@ -463,6 +512,9 @@ define([
if(kind && kind.kind) {
var tmpls = Finder.findTemplatesForKind(templates, kind.kind, cachedQuery.ecma ? cachedQuery.ecma : 6);
tmpls.forEach(function(template) {
if(!kind.isasync && template.nodes.async === true) {
return;
}
gather(template.name, null, 0, function(c) {
c.template = template.template;
c.doc = template.doc;
Expand Down

0 comments on commit c5c5e88

Please sign in to comment.