Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

New: Create option to enable JSXText node type (fixes #266) #272

Merged
merged 1 commit into from
May 21, 2017
Merged
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
3 changes: 2 additions & 1 deletion lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ module.exports = (ast, extra) => {
parent: null,
ast,
additionalOptions: {
errorOnUnknownASTType: extra.errorOnUnknownASTType || false
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
useJSXTextNode: extra.useJSXTextNode || false
}
});

Expand Down
21 changes: 15 additions & 6 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1613,16 +1613,25 @@ module.exports = function convert(config) {

}

/**
* The JSX AST changed the node type for string literals
* inside a JSX Element from `Literal` to `JSXText`. We
* provide a flag to support both types until `Literal`
* node type is deprecated in ESLint v5.
*/
case SyntaxKind.JsxText: {
Object.assign(result, {
type: AST_NODE_TYPES.Literal,
value: ast.text.slice(node.pos, node.end),
raw: ast.text.slice(node.pos, node.end)
});

const start = node.getFullStart();
const end = node.getEnd();

const type = (additionalOptions.useJSXTextNode)
? AST_NODE_TYPES.JSXText : AST_NODE_TYPES.Literal;

Object.assign(result, {
type,
value: ast.text.slice(start, end),
raw: ast.text.slice(start, end)
});

result.loc = nodeUtils.getLocFor(start, end, ast);
result.range = [start, end];

Expand Down
7 changes: 6 additions & 1 deletion parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function resetExtra() {
tolerant: false,
errors: [],
strict: false,
ecmaFeatures: {}
ecmaFeatures: {},
useJSXTextNode: false
};
}

Expand Down Expand Up @@ -104,6 +105,10 @@ function parse(code, options) {
extra.errorOnUnknownASTType = true;
}

if (typeof options.useJSXTextNode === "boolean" && options.useJSXTextNode) {
extra.useJSXTextNode = true;
}

}

// Even if jsx option is set in typescript compiler, filename still has to
Expand Down
Loading