Skip to content

Function and class properties #13648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 16, 2017
Merged

Function and class properties #13648

merged 10 commits into from
Feb 16, 2017

Conversation

mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Jan 24, 2017

This PR adds support for detecting property assignments to functions in .js files, for example:

function Minimatch (pattern, options) {
}
Minimatch.defaults = function (def) {
}
module.exports = Minimatch;

The property assignment is tracked like prototype assignments.

The change also adds support for handling static and prototype properties on class expressions.

Also for anonymous function and class expression, that are defined in a variable declaration, e.g. var x = function() {..} use the name of the variable (e.g. x) for error reporting purposes.

Copy link
Contributor

@yuit yuit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add tests for this "Also for anonymous function and class expression, that are defined in a variable declaration, e.g. var x = function() {..} use the name of the variable (e.g. x) for error reporting purposes." I didn't see it updated in tests (aside from symbol baselines)

@@ -2230,6 +2238,36 @@ namespace ts {
declareSymbol(funcSymbol.members, funcSymbol, leftSideOfAssignment, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
}

function bindPropertyAssignment(node: BinaryExpression) {
// We saw a node of the form 'x.y = z'. Declare a 'member' y on x if x was a function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be JSDoc comment

////}
////C.prototype.[|z|] = 1;
////var t = new C(12);
////t./**/[|z|] = 11;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be support for "gotoDefinition"?

@@ -1402,10 +1402,10 @@ namespace ts {
* Returns true if the node is a variable declaration whose initializer is a function expression.
* This function does not test if the node is in a JavaScript file or not.
*/
export function isDeclarationOfFunctionExpression(s: Symbol) {
export function isDeclarationOfFunctionOrClassExpression(s: Symbol) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is not updated on line 1402

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

leftSideOfAssignment.parent = node;
target.parent = leftSideOfAssignment;

let funcSymbol = container.locals.get(target.text);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this symbol is not necessary only for function declaration but class expression as well? if so, could you rename the variable?


// Set up the members collection if it doesn't exist already
if (!funcSymbol.exports) {
funcSymbol.exports = createMap<Symbol>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you are using exports ? if there is more use to exports than "module exports" as the comment suggest (types.ts Line 2695) could you fix up the comment ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it because this is the static side of the class, not the instance side? Exports is a confusing name in this case, then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is where we keep the static properties on classes. members has the instance properties.

@@ -1417,6 +1421,7 @@ namespace ts {
}
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need this extra whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope


// Set up the members collection if it doesn't exist already
if (!funcSymbol.exports) {
funcSymbol.exports = createMap<Symbol>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it because this is the static side of the class, not the instance side? Exports is a confusing name in this case, then.

@@ -2216,8 +2220,12 @@ namespace ts {
constructorFunction.parent = classPrototype;
classPrototype.parent = leftSideOfAssignment;

const funcSymbol = container.locals.get(constructorFunction.text);
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function || isDeclarationOfFunctionExpression(funcSymbol))) {
let funcSymbol = container.locals.get(constructorFunction.text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindPrototypePropertyAssignment and bindPropertyAssignment share lots of code now, so I think they should either merge or delegate to a third function that does most of the work.

@mhegazy
Copy link
Contributor Author

mhegazy commented Feb 16, 2017

@sandersn any comments?

@mhegazy mhegazy merged commit 6c58938 into master Feb 16, 2017
@mhegazy mhegazy deleted the functionAndClassProperties branch February 16, 2017 20:27
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants