typescript-eslint-parser
Advanced tools
Comparing version 0.1.0-alpha.0 to 0.1.0-alpha.1
@@ -34,3 +34,4 @@ /** | ||
var ts = require("typescript"), | ||
assign = require("object-assign"); | ||
assign = require("object-assign"), | ||
unescape = require("lodash.unescape"); | ||
@@ -313,3 +314,12 @@ //------------------------------------------------------------------------------ | ||
case SyntaxKind.JsxText: | ||
return "JSXText"; | ||
case SyntaxKind.StringLiteral: | ||
// A TypeScript-StringLiteral token with a TypeScript-JsxAttribute or TypeScript-JsxElement parent, | ||
// must actually be an ESTree-JSXText token | ||
if (token.parent && (token.parent.kind === SyntaxKind.JsxAttribute || token.parent.kind === SyntaxKind.JsxElement)) { | ||
return "JSXText"; | ||
} | ||
return "String"; | ||
@@ -328,2 +338,19 @@ | ||
// Some JSX tokens have to be determined based on their parent | ||
if (token.parent) { | ||
if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.FirstNode) { | ||
return "JSXIdentifier"; | ||
} | ||
if (token.parent.kind >= SyntaxKind.JsxElement && token.parent.kind <= SyntaxKind.JsxAttribute) { | ||
if (token.kind === SyntaxKind.FirstNode) { | ||
return "JSXMemberExpression"; | ||
} | ||
if (token.kind === SyntaxKind.Identifier) { | ||
return "JSXIdentifier"; | ||
} | ||
} | ||
} | ||
return "Identifier"; | ||
@@ -374,3 +401,2 @@ } | ||
} | ||
token = ts.findNextToken(token, ast); | ||
@@ -431,2 +457,34 @@ } | ||
/** | ||
* Converts a TypeScript JSX node.tagName into an ESTree node.name | ||
* @param {Object} tagName the tagName object from a JSX TSNode | ||
* @param {Object} ast the AST object | ||
* @returns {Object} the converted ESTree name object | ||
*/ | ||
function convertTypeScriptJSXTagNameToESTreeName(tagName) { | ||
var tagNameToken = convertToken(tagName, ast); | ||
if (tagNameToken.type === "JSXMemberExpression") { | ||
var isNestedMemberExpression = (node.tagName.left.kind === SyntaxKind.FirstNode); | ||
// Convert TSNode left and right objects into ESTreeNode object | ||
// and property objects | ||
tagNameToken.object = convertChild(node.tagName.left); | ||
tagNameToken.property = convertChild(node.tagName.right); | ||
// Assign the appropriate types | ||
tagNameToken.object.type = (isNestedMemberExpression) ? "JSXMemberExpression" : "JSXIdentifier"; | ||
tagNameToken.property.type = "JSXIdentifier"; | ||
} else { | ||
tagNameToken.name = tagNameToken.value; | ||
} | ||
delete tagNameToken.value; | ||
return tagNameToken; | ||
} | ||
switch (node.kind) { | ||
@@ -1326,3 +1384,3 @@ case SyntaxKind.SourceFile: | ||
type: "Literal", | ||
value: node.text, | ||
value: unescape(node.text), | ||
raw: ast.text.slice(result.range[0], result.range[1]) | ||
@@ -1355,3 +1413,3 @@ }); | ||
type: "Literal", | ||
value: "true" | ||
value: true | ||
}); | ||
@@ -1363,3 +1421,3 @@ break; | ||
type: "Literal", | ||
value: "false" | ||
value: false | ||
}); | ||
@@ -1371,3 +1429,3 @@ break; | ||
type: "Literal", | ||
value: "null" | ||
value: null | ||
}); | ||
@@ -1381,2 +1439,121 @@ break; | ||
// JSX | ||
case SyntaxKind.JsxElement: | ||
assign(result, { | ||
type: "JSXElement", | ||
openingElement: convertChild(node.openingElement), | ||
closingElement: convertChild(node.closingElement), | ||
children: node.children.map(convertChild) | ||
}); | ||
break; | ||
case SyntaxKind.JsxSelfClosingElement: | ||
// Convert SyntaxKind.JsxSelfClosingElement to SyntaxKind.JsxOpeningElement, | ||
// TypeScript does not seem to have the idea of openingElement when tag is self-closing | ||
node.kind = SyntaxKind.JsxOpeningElement; | ||
assign(result, { | ||
type: "JSXElement", | ||
openingElement: convertChild(node), | ||
closingElement: null, | ||
children: [] | ||
}); | ||
break; | ||
case SyntaxKind.JsxOpeningElement: | ||
var openingTagName = convertTypeScriptJSXTagNameToESTreeName(node.tagName); | ||
assign(result, { | ||
type: "JSXOpeningElement", | ||
selfClosing: !(node.parent && node.parent.closingElement), | ||
name: openingTagName, | ||
attributes: node.attributes.map(convertChild) | ||
}); | ||
break; | ||
case SyntaxKind.JsxClosingElement: | ||
var closingTagName = convertTypeScriptJSXTagNameToESTreeName(node.tagName); | ||
assign(result, { | ||
type: "JSXClosingElement", | ||
name: closingTagName | ||
}); | ||
break; | ||
case SyntaxKind.JsxExpression: | ||
var eloc = ast.getLineAndCharacterOfPosition(result.range[0] + 1); | ||
var expression = (node.expression) ? convertChild(node.expression) : { | ||
type: "JSXEmptyExpression", | ||
loc: { | ||
start: { | ||
line: eloc.line + 1, | ||
column: eloc.character | ||
}, | ||
end: { | ||
line: result.loc.end.line, | ||
column: result.loc.end.column - 1 | ||
} | ||
}, | ||
range: [result.range[0] + 1, result.range[1] - 1] | ||
}; | ||
assign(result, { | ||
type: "JSXExpressionContainer", | ||
expression: expression | ||
}); | ||
break; | ||
case SyntaxKind.JsxAttribute: | ||
var attributeName = convertToken(node.name, ast); | ||
attributeName.name = attributeName.value; | ||
delete attributeName.value; | ||
assign(result, { | ||
type: "JSXAttribute", | ||
name: attributeName, | ||
value: convertChild(node.initializer) | ||
}); | ||
break; | ||
case SyntaxKind.JsxText: | ||
assign(result, { | ||
type: "Literal", | ||
value: ast.text.slice(node.pos, node.end), | ||
raw: ast.text.slice(node.pos, node.end) | ||
}); | ||
result.loc.start.column = node.pos; | ||
result.range[0] = node.pos; | ||
break; | ||
case SyntaxKind.JsxSpreadAttribute: | ||
assign(result, { | ||
type: "JSXSpreadAttribute", | ||
argument: convertChild(node.expression) | ||
}); | ||
break; | ||
case SyntaxKind.FirstNode: | ||
var jsxMemberExpressionObject = convertChild(node.left); | ||
jsxMemberExpressionObject.type = "JSXIdentifier"; | ||
delete jsxMemberExpressionObject.value; | ||
var jsxMemberExpressionProperty = convertChild(node.right); | ||
jsxMemberExpressionProperty.type = "JSXIdentifier"; | ||
delete jsxMemberExpressionObject.value; | ||
assign(result, { | ||
type: "JSXMemberExpression", | ||
object: jsxMemberExpressionObject, | ||
property: jsxMemberExpressionProperty | ||
}); | ||
break; | ||
// TypeScript specific | ||
@@ -1388,3 +1565,3 @@ | ||
default: | ||
console.log(node.kind); | ||
console.log("unsupported node.kind:", node.kind); | ||
result = null; | ||
@@ -1391,0 +1568,0 @@ } |
@@ -7,3 +7,3 @@ { | ||
"main": "parser.js", | ||
"version": "0.1.0-alpha.0", | ||
"version": "0.1.0-alpha.1", | ||
"files": [ | ||
@@ -60,2 +60,3 @@ "lib", | ||
"dependencies": { | ||
"lodash.unescape": "4.0.0", | ||
"object-assign": "^4.0.1" | ||
@@ -62,0 +63,0 @@ }, |
@@ -110,4 +110,11 @@ /** | ||
var FILENAME = "eslint.ts"; | ||
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") { | ||
// pass through jsx option | ||
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx; | ||
} | ||
// Even if jsx option is set in typescript compiler, filename still has to | ||
// contain .tsx file extension | ||
var FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts"; | ||
var compilerHost = { | ||
@@ -114,0 +121,0 @@ fileExists: function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92371
2095
3
+ Addedlodash.unescape@4.0.0
+ Addedlodash.tostring@4.1.4(transitive)
+ Addedlodash.unescape@4.0.0(transitive)