From 16bcbc429aaf1fde61395af73dfa2035960db65b Mon Sep 17 00:00:00 2001 From: othree Date: Fri, 21 Sep 2018 18:12:19 +0800 Subject: [PATCH 1/2] Fix #990, Handle export default function/class --- lib/infer.js | 21 ++++++++++++++++++--- plugin/es_modules.js | 6 +++--- test/cases/es_modules/class.js | 4 ++++ test/cases/es_modules/func.js | 1 + test/cases/es_modules/main.js | 13 +++++++++++++ test/cases/es_modules/obj.js | 1 + 6 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 test/cases/es_modules/class.js create mode 100644 test/cases/es_modules/func.js create mode 100644 test/cases/es_modules/obj.js diff --git a/lib/infer.js b/lib/infer.js index 44707443..ce638bac 100644 --- a/lib/infer.js +++ b/lib/infer.js @@ -1153,7 +1153,7 @@ } }, ClassDeclaration: function(node, scope, c) { - addVar(scope, node.id) + if (node.id) addVar(scope, node.id); if (node.superClass) c(node.superClass, scope, "Expression") for (var i = 0; i < node.body.body.length; i++) c(node.body.body[i], scope) @@ -1660,18 +1660,26 @@ infer(node, node.scope || scope, ANull); }, + ObjectExpression: function(node, scope) { + infer(node, node.scope || scope, ANull); + }, + FunctionDeclaration: function(node, scope, c) { var inner = node.scope, fn = inner.fnType; connectParams(node, inner) c(node.body, inner, "Statement"); maybeTagAsInstantiated(node, fn) || maybeTagAsGeneric(fn); - scope.getProp(node.id.name).addType(fn) + if (node.id) scope.getProp(node.id.name).addType(fn); }, Statement: function(node, scope, c) { c(node, node.scope || scope) }, + ExportDefaultDeclaration: function(node, scope, c) { + c(node.declaration, node.scope || scope) + }, + VariableDeclaration: function(node, scope) { for (var i = 0; i < node.declarations.length; ++i) { var decl = node.declarations[i]; @@ -1686,7 +1694,8 @@ }, ClassDeclaration: function(node, scope) { - scope.getProp(node.id.name).addType(inferClass(node, scope, node.id.name)) + if (!node.id) inferClass(node, scope); + else scope.getProp(node.id.name).addType(inferClass(node, scope, node.id.name)) }, ReturnStatement: function(node, scope) { @@ -1893,9 +1902,15 @@ ObjectExpression: function(node) { return node.objType; }, + ClassDeclaration: function(node) { + return node.objType; + }, ClassExpression: function(node) { return node.objType; }, + FunctionDeclaration: function(node) { + return node.scope.fnType; + }, FunctionExpression: function(node) { return node.scope.fnType; }, diff --git a/plugin/es_modules.js b/plugin/es_modules.js index caf67f5c..870538b1 100644 --- a/plugin/es_modules.js +++ b/plugin/es_modules.js @@ -19,9 +19,9 @@ outObj.originNode = file.ast out.addType(outObj) } - var prop = outObj.defProp(prop, originNode) - prop.origin = file.name - type.propagate(prop) + var propObj = outObj.defProp(prop, originNode) + propObj.origin = file.name + type.propagate(propObj) } walk.simple(file.ast, { diff --git a/test/cases/es_modules/class.js b/test/cases/es_modules/class.js new file mode 100644 index 00000000..8af5fca8 --- /dev/null +++ b/test/cases/es_modules/class.js @@ -0,0 +1,4 @@ +export default class { + methodA() { + } +}; \ No newline at end of file diff --git a/test/cases/es_modules/func.js b/test/cases/es_modules/func.js new file mode 100644 index 00000000..fda957e1 --- /dev/null +++ b/test/cases/es_modules/func.js @@ -0,0 +1 @@ +export default function () { return true; } \ No newline at end of file diff --git a/test/cases/es_modules/main.js b/test/cases/es_modules/main.js index 10afd19b..53739d05 100644 --- a/test/cases/es_modules/main.js +++ b/test/cases/es_modules/main.js @@ -19,3 +19,16 @@ import * as reexp from "./reexp" reexp //:: {a: number, b: bool} import "./b" //+ "./blah" + +import C from "./class" + +(new C()). //+ methodA + +import f from "./func" + +f //: fn() -> bool + +import o from "./obj" + +o.propA //: number +o.propB //: string diff --git a/test/cases/es_modules/obj.js b/test/cases/es_modules/obj.js new file mode 100644 index 00000000..91945229 --- /dev/null +++ b/test/cases/es_modules/obj.js @@ -0,0 +1 @@ +export default {propA: 1, propB: "str"} \ No newline at end of file From bf707427064a07a1e71c05a8a6cbba87257667a6 Mon Sep 17 00:00:00 2001 From: othree Date: Thu, 27 Sep 2018 10:21:08 +0800 Subject: [PATCH 2/2] Mark version 0.22.3 --- index.html | 4 ++-- lib/tern.js | 2 +- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 67ffd0ff..54c043a9 100644 --- a/index.html +++ b/index.html @@ -91,10 +91,10 @@

Documentation

Release history

-

13-09-2018: Version 0.22.2

+

27-09-2018: Version 0.22.3

Small update release that fixes crashes -affecting spread operator in object notation and rest operatore in object pattern.

+affecting spread operator in object notation, rest operatore in object pattern and export default anonymous class or function.

28-08-2018: Version 0.22

diff --git a/lib/tern.js b/lib/tern.js index f4d5bbed..4d89cd6b 100644 --- a/lib/tern.js +++ b/lib/tern.js @@ -1149,5 +1149,5 @@ return {files: srv.files.map(function(f){return f.name;})}; } - exports.version = "0.22.2"; + exports.version = "0.22.3"; }); diff --git a/package.json b/package.json index 875a2d17..e86a09d7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "tern", "license": "MIT", - "version": "0.22.2", + "version": "0.22.3", "author": "Marijn Haverbeke ", "description": "A JavaScript code analyzer for deep, cross-editor language support", "main": "lib/tern.js",