Repository navigation
Expand file tree
/
Copy pathtransforms.coffee
More file actions
104 lines (90 loc) · 5.48 KB
/
Copy pathtransforms.coffee
File metadata and controls
104 lines (90 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
_ = window?._ ? self?._ ? global?._ ? require 'lodash' # rely on lodash existing, since it busts CodeCombat to browserify it--TODO
S = require('esprima').Syntax
ranges = require './ranges'
statements = [S.EmptyStatement, S.ExpressionStatement, S.BreakStatement, S.ContinueStatement, S.DebuggerStatement, S.DoWhileStatement, S.ForStatement, S.FunctionDeclaration, S.ClassDeclaration, S.IfStatement, S.ReturnStatement, S.SwitchStatement, S.ThrowStatement, S.TryStatement, S.VariableStatement, S.WhileStatement, S.WithStatement, S.VariableDeclaration]
getParents = (node) ->
parents = []
while node.parent
parents.push node = node.parent
parents
getParentsOfTypes = (node, types) ->
_.filter getParents(node), (elem) -> elem.type in types
getFunctionNestingLevel = (node) ->
getParentsOfTypes(node, [S.FunctionExpression]).length
getImmediateParentOfType = (node, type) ->
while node
return node if node.type is type
node = node.parent
########## Before JS_WALA Normalization ##########
# Original node range preservation.
# 1. Make a many-to-one mapping of normalized nodes to original nodes based on the original ranges, which are unique except for the outer Program wrapper.
# 2. When we generate the normalizedCode, we can also create a source map.
# 3. A postNormalizationTransform can then get the original ranges for each node by going through the source map to our normalized mapping to our original node ranges.
# 4. Instrumentation can then include the original ranges and node source in the saved flow state.
module.exports.makeGatherNodeRanges = makeGatherNodeRanges = (nodeRanges, code, codePrefix) -> (node) ->
return unless node.range
#for x in node.range when _.isNaN x
# console.log "got bad range", node.range, "from", node, node.parent
node.originalRange = ranges.offsetsToRange node.range[0], node.range[1], code, codePrefix
if node.source
node.originalSource = node.source()
else
#TODO: compute this via ranges
nodeRanges.push node
# Making
module.exports.makeCheckThisKeywords = makeCheckThisKeywords = (globals, varNames, language, problemContext) ->
return (node) ->
if node.type is S.VariableDeclarator
varNames[node.id.name] = true
else if node.type is S.AssignmentExpression
varNames[node.left.name] = true
else if node.type is S.FunctionDeclaration or node.type is S.FunctionExpression# and node.parent.type isnt S.Program
varNames[node.id.name] = true if node.id?
varNames[param.name] = true for param in node.params
else if node.type is S.CallExpression
# TODO: false negative when user method call precedes function declaration
v = node
while v.type in [S.CallExpression, S.MemberExpression]
v = if v.object? then v.object else v.callee
v = v.name
if v and not varNames[v] and not (v in globals)
return unless problemContext # If we don't know what properties are available, don't create this problem.
# Probably MissingThis, but let's check if we're recursively calling an inner function from itself first.
for p in getParentsOfTypes node, [S.FunctionDeclaration, S.FunctionExpression, S.VariableDeclarator, S.AssignmentExpression]
varNames[p.id.name] = true if p.id?
varNames[p.left.name] = true if p.left?
varNames[param.name] = true for param in p.params if p.params?
return if varNames[v] is true
return if /\$$/.test v # accum$ in CoffeeScript Redux isn't handled properly
return if problemContext?.thisMethods? and v not in problemContext.thisMethods
# TODO: '@' in CoffeeScript isn't really a keyword
message = "Missing `hero` keyword; should be `#{language.heroValueAccess}#{v}`."
hint = "There is no function `#{v}`, but `hero` has a method `#{v}`."
if node.originalRange
range = language.removeWrappedIndent [node.originalRange.start, node.originalRange.end]
problem = @createUserCodeProblem type: 'transpile', reporter: 'aether', kind: 'MissingThis', message: message, hint: hint, range: range # TODO: code/codePrefix?
@addProblem problem
module.exports.makeCheckIncompleteMembers = makeCheckIncompleteMembers = (language, problemContext) ->
return (node) ->
# console.log 'check incomplete members', node, node.source() if node.source().search('this.') isnt -1
if node.type is 'ExpressionStatement'
exp = node.expression
if exp.type is 'MemberExpression'
# Handle missing parentheses, like in: this.moveUp;
if exp.property.name is "IncompleteThisReference"
kind = 'IncompleteThis'
m = "this.what? (Check available spells below.)"
hint = ''
else if exp.object.source() is language.thisValue
kind = 'NoEffect'
m = "#{exp.source()} has no effect."
if problemContext?.thisMethods? and exp.property.name in problemContext.thisMethods
m += " It needs parentheses: #{exp.source()}()"
else if problemContext?.commonThisMethods? and exp.property.name in problemContext.commonThisMethods
m = "#{exp.source()} is not currently available."
else
hint = "Is it a method? Those need parentheses: #{exp.source()}()"
if node.originalRange
range = language.removeWrappedIndent [node.originalRange.start, node.originalRange.end]
problem = @createUserCodeProblem type: 'transpile', reporter: 'aether', message: m, kind: kind, hint: hint, range: range # TODO: code/codePrefix?
@addProblem problem