Skip to content

JSX Support #2673

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6266,6 +6266,11 @@ module ts {
}
}

function checkJSXElement(node: JSXElement) {
checkGrammarJSXElement(node);
grammarErrorOnNode(node, Diagnostics.JSX_elements_are_not_currently_supported);
}

// If a symbol is a synthesized symbol with no value declaration, we assume it is a property. Example of this are the synthesized
// '.prototype' property as well as synthesized tuple index properties.
function getDeclarationKindFromSymbol(s: Symbol) {
Expand Down Expand Up @@ -8164,6 +8169,9 @@ module ts {
case SyntaxKind.YieldExpression:
checkYieldExpression(<YieldExpression>node);
return unknownType;
case SyntaxKind.JSXElement:
checkJSXElement(<JSXElement>node);
return unknownType;
}
return unknownType;
}
Expand Down Expand Up @@ -12699,6 +12707,29 @@ module ts {
}
}

function checkGrammarJSXElement(node: JSXElement) {
const seen: Map<boolean> = {};
for (let attr of node.openingElement.attributes) {
if (attr.kind === SyntaxKind.JSXSpreadAttribute) {
continue;
}

let jsxAttr = (<JSXAttribute>attr);
let name = jsxAttr.name;
if (!hasProperty(seen, name.text)) {
seen[name.text] = true;
}
else {
return grammarErrorOnNode(name, Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name);
}

let initializer = jsxAttr.initializer;
if (initializer && initializer.kind === SyntaxKind.JSXExpression && !(<JSXExpression>initializer).expression) {
return grammarErrorOnNode(jsxAttr.initializer, Diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression);
}
}
}

function checkGrammarForInOrForOfStatement(forInOrOfStatement: ForInStatement | ForOfStatement): boolean {
if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) {
return true;
Expand Down
15 changes: 13 additions & 2 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,21 @@ module ts {
}
}
else {
var sysFiles = host.readDirectory(basePath, ".ts");
var sysFiles = host.readDirectory(basePath, ".ts").concat(host.readDirectory(basePath, ".tsx"));
for (var i = 0; i < sysFiles.length; i++) {
var name = sysFiles[i];
if (!fileExtensionIs(name, ".d.ts") || !contains(sysFiles, name.substr(0, name.length - 5) + ".ts")) {
if (fileExtensionIs(name, ".d.ts")) {
let baseName = name.substr(0, name.length - ".d.ts".length);
if (!contains(sysFiles, baseName + ".tsx") && !contains(sysFiles, baseName + ".ts")) {
files.push(name);
}
}
else if (fileExtensionIs(name, ".ts")) {
if (!contains(sysFiles, name + "x")) {
files.push(name)
}
}
else {
files.push(name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,9 @@ module ts {
/**
* List of supported extensions in order of file resolution precedence.
*/
export const supportedExtensions = [".ts", ".d.ts"];
export const supportedExtensions = [".tsx", ".ts", ".d.ts"];

const extensionsToRemove = [".d.ts", ".ts", ".js"];
const extensionsToRemove = [".d.ts", ".ts", ".tsx", ".js"];
export function removeFileExtension(path: string): string {
for (let ext of extensionsToRemove) {
if (fileExtensionIs(path, ext)) {
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,11 @@ module ts {
Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." },
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." },
JSX_attributes_must_only_be_assigned_a_non_empty_expression: { code: 17000, category: DiagnosticCategory.Error, key: "JSX attributes must only be assigned a non-empty 'expression'." },
JSX_elements_cannot_have_multiple_attributes_with_the_same_name: { code: 17001, category: DiagnosticCategory.Error, key: "JSX elements cannot have multiple attributes with the same name." },
Expected_corresponding_JSX_closing_tag_for_0: { code: 17002, category: DiagnosticCategory.Error, key: "Expected corresponding JSX closing tag for '{0}'." },
JSX_attribute_expected: { code: 17003, category: DiagnosticCategory.Error, key: "JSX attribute expected." },
JSX_factory_cannot_have_multiple_assignments: { code: 17004, category: DiagnosticCategory.Error, key: "JSX factory cannot have multiple assignments." },
JSX_elements_are_not_currently_supported: { code: 17005, category: DiagnosticCategory.Error, key: "JSX elements are not currently supported." },
};
}
24 changes: 24 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2172,5 +2172,29 @@
"'class' declarations are only supported directly inside a module or as a top level declaration.": {
"category": "Error",
"code": 9004
},
"JSX attributes must only be assigned a non-empty 'expression'.": {
"category": "Error",
"code": 17000
},
"JSX elements cannot have multiple attributes with the same name.": {
"category": "Error",
"code": 17001
},
"Expected corresponding JSX closing tag for '{0}'.": {
"category": "Error",
"code": 17002
},
"JSX attribute expected.": {
"category": "Error",
"code": 17003
},
"JSX factory cannot have multiple assignments.": {
"category": "Error",
"code": 17004
},
"JSX elements are not currently supported.": {
"category": "Error",
"code" : 17005
}
}
Loading