Skip to content
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

feat(react-jsx-source): add columnNumber property #11139

Merged
merged 2 commits into from Mar 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions packages/babel-plugin-transform-react-jsx-source/src/index.js
@@ -1,7 +1,7 @@
/**
* This adds {fileName, lineNumber} annotations to React component definitions
* and to jsx tag literals.
* This adds {fileName, lineNumber, columnNumber} annotations to JSX tags.
*
* NOTE: lineNumber and columnNumber are both 1-based.
*
* == JSX Literals ==
*
Expand All @@ -10,7 +10,7 @@
* becomes:
*
* var __jsxFileName = 'this/file.js';
* <sometag __source={{fileName: __jsxFileName, lineNumber: 10}}/>
* <sometag __source={{fileName: __jsxFileName, lineNumber: 10, columnNumber: 1}}/>
*/
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";
Expand All @@ -21,9 +21,13 @@ const FILE_NAME_VAR = "_jsxFileName";
export default declare(api => {
api.assertVersion(7);

function makeTrace(fileNameIdentifier, lineNumber) {
function makeTrace(fileNameIdentifier, lineNumber, column0Based) {
const fileLineLiteral =
lineNumber != null ? t.numericLiteral(lineNumber) : t.nullLiteral();
const fileColumnLiteral =
column0Based != null
? t.numericLiteral(column0Based + 1)
: t.nullLiteral();
const fileNameProperty = t.objectProperty(
t.identifier("fileName"),
fileNameIdentifier,
Expand All @@ -32,7 +36,15 @@ export default declare(api => {
t.identifier("lineNumber"),
fileLineLiteral,
);
return t.objectExpression([fileNameProperty, lineNumberProperty]);
const columnNumberProperty = t.objectProperty(
t.identifier("columnNumber"),
fileColumnLiteral,
);
return t.objectExpression([
fileNameProperty,
lineNumberProperty,
columnNumberProperty,
]);
}

const visitor = {
Expand Down Expand Up @@ -69,7 +81,11 @@ export default declare(api => {
state.fileNameIdentifier = fileNameIdentifier;
}

const trace = makeTrace(state.fileNameIdentifier, location.start.line);
const trace = makeTrace(
state.fileNameIdentifier,
location.start.line,
location.start.column,
);
attributes.push(t.jsxAttribute(id, t.jsxExpressionContainer(trace)));
},
};
Expand Down
Expand Up @@ -7,7 +7,8 @@ var expected = multiline([
'var _jsxFileName = "/fake/path/mock.js";',
'var x = <sometag __source={{',
' fileName: _jsxFileName,',
' lineNumber: 1',
' lineNumber: 1,',
' columnNumber: 9',
'}} />;',
]);

Expand Down
Expand Up @@ -9,7 +9,8 @@ const expected = multiline([
' bar: "baz",',
' __source: {',
' fileName: _jsxFileName,',
' lineNumber: 1',
' lineNumber: 1,',
' columnNumber: 1',
' },',
' __self: this',
'});',
Expand Down
Expand Up @@ -9,7 +9,8 @@ const expected = multiline([
' bar: "baz",',
' __source: {',
' fileName: _jsxFileName,',
' lineNumber: 1',
' lineNumber: 1,',
' columnNumber: 1',
' },',
' __self: this',
'});',
Expand Down