Skip to content

Commit 6ffeec1

Browse files
committed
feat: add style['--source-code-location'] to all JSX Elements
1 parent 708f15b commit 6ffeec1

File tree

15 files changed

+3533
-1
lines changed

15 files changed

+3533
-1
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 2

.github/workflows/publish.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Node.js Package
2+
on:
3+
release:
4+
types: [created]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
# Setup .npmrc file to publish to npm
11+
- uses: actions/setup-node@v3
12+
with:
13+
node-version: "20"
14+
registry-url: "https://registry.npmjs.org"
15+
- run: yarn install
16+
- run: yarn publish
17+
env:
18+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
lib
2+
.DS_Store
3+
14
# Logs
25
logs
36
*.log

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
test
3+
*.log

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# v0.0.1
2+
3+
Add `style['--source-code-location']` to all JSX Elements

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
11
# babel-plugin-transform-react-jsx-source-to-style
2-
Add `style['--source-code-location']` to all JSX Elements.
2+
3+
[![npm](https://img.shields.io/npm/v/babel-plugin-transform-react-jsx-source-to-style)](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source-to-style)
4+
5+
> Add `style['--source-code-location']` to all JSX Elements.
6+
7+
English | [简体中文](./README.zh-CN.md)
8+
9+
## Example
10+
11+
**In**
12+
13+
```
14+
<sometag />
15+
```
16+
17+
**Out**
18+
19+
```
20+
<sometag style={{'--source-code-location': `${__jsxFileName}:${lineNumber}:${columnNumber}`}} />
21+
```
22+
23+
## Install
24+
25+
Using npm:
26+
27+
```sh
28+
npm install --save-dev babel-plugin-transform-react-jsx-source-to-style
29+
```
30+
31+
or using yarn:
32+
33+
```sh
34+
yarn add babel-plugin-transform-react-jsx-source-to-style --dev
35+
```
36+
37+
## Usage
38+
39+
### With a configuration file (Recommended)
40+
41+
```json title="babel.config.json"
42+
{
43+
"plugins": ["babel-plugin-transform-react-jsx-source-to-style"]
44+
}
45+
```
46+
47+
### Via CLI
48+
49+
```sh
50+
babel --plugins babel-plugin-transform-react-jsx-source-to-style script.js
51+
```
52+
53+
### Via Node API
54+
55+
```js
56+
require("@babel/core").transformSync("code", {
57+
plugins: ["babel-plugin-transform-react-jsx-source-to-style"],
58+
});
59+
```

README.zh-CN.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# babel-plugin-transform-react-jsx-source-to-style
2+
3+
[![npm](https://img.shields.io/npm/v/babel-plugin-transform-react-jsx-source-to-style)](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source-to-style)
4+
5+
> 为所有 JSX 元素增加 `style['--source-code-location']`
6+
7+
English | [简体中文](./README.zh-CN.md)
8+
9+
## 示例
10+
11+
**输入**
12+
13+
```
14+
<sometag />
15+
```
16+
17+
**输入**
18+
19+
```
20+
<sometag style={{'--source-code-location': `${__jsxFileName}:${lineNumber}:${columnNumber}`}} />
21+
```
22+
23+
## 安装
24+
25+
使用 npm:
26+
27+
```sh
28+
npm install --save-dev babel-plugin-transform-react-jsx-source-to-style
29+
```
30+
31+
或者使用 yarn:
32+
33+
```sh
34+
yarn add babel-plugin-transform-react-jsx-source-to-style --dev
35+
```
36+
37+
## 使用
38+
39+
### 通过配置文件(推荐)
40+
41+
```json title="babel.config.json"
42+
{
43+
"plugins": ["babel-plugin-transform-react-jsx-source-to-style"]
44+
}
45+
```
46+
47+
### 通过 CLI
48+
49+
```sh
50+
babel --plugins babel-plugin-transform-react-jsx-source-to-style script.js
51+
```
52+
53+
### 通过 Node API
54+
55+
```js
56+
require("@babel/core").transformSync("code", {
57+
plugins: ["babel-plugin-transform-react-jsx-source-to-style"],
58+
});
59+
```

eslint.config.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { includeIgnoreFile } from "@eslint/compat";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import eslint from "@eslint/js";
5+
import prettierConfig from "eslint-config-prettier";
6+
import tseslint from "typescript-eslint";
7+
import globals from "globals";
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
11+
const gitignorePath = path.resolve(__dirname, ".gitignore");
12+
13+
export default tseslint.config(
14+
includeIgnoreFile(gitignorePath),
15+
eslint.configs.recommended,
16+
...tseslint.configs.recommended,
17+
prettierConfig,
18+
{
19+
files: ["**/*.test.js"],
20+
rules: {
21+
"@typescript-eslint/no-require-imports": "off",
22+
},
23+
languageOptions: {
24+
globals: {
25+
...globals.jest,
26+
},
27+
},
28+
},
29+
);

package.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "babel-plugin-transform-react-jsx-source-to-style",
3+
"version": "0.0.0",
4+
"description": "Add style['--source-code-location'] to all JSX Elements.",
5+
"keywords": [
6+
"babel-plugin"
7+
],
8+
"homepage": "https://github.com/zjffun/babel-plugin-transform-react-jsx-source-to-style#readme",
9+
"bugs": {
10+
"url": "https://github.com/zjffun/babel-plugin-transform-react-jsx-source-to-style/issues"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/zjffun/babel-plugin-transform-react-jsx-source-to-style.git"
15+
},
16+
"license": "MIT",
17+
"author": "Jufeng Zhang <[email protected]>",
18+
"type": "commonjs",
19+
"exports": {
20+
".": {
21+
"types": "./lib/index.d.ts",
22+
"default": "./lib/index.js"
23+
},
24+
"./package.json": "./package.json"
25+
},
26+
"main": "./lib/index.js",
27+
"scripts": {
28+
"build": "rimraf lib && tsc",
29+
"dev": "tsc -w",
30+
"eslint-fix": "eslint --fix .",
31+
"prepublishOnly": "npm run test && npm run build",
32+
"prettier-fix": "prettier --write .",
33+
"sort-package-json": "sort-package-json",
34+
"test": "jest",
35+
"update-snapshot": "jest -u"
36+
},
37+
"dependencies": {
38+
"@babel/helper-plugin-utils": "^7.24.7"
39+
},
40+
"devDependencies": {
41+
"@babel/core": "^7.24.7",
42+
"@babel/helper-plugin-test-runner": "^7.24.7",
43+
"@babel/plugin-syntax-jsx": "^7.24.7",
44+
"@babel/preset-react": "^7.24.7",
45+
"@eslint/compat": "^1.1.1",
46+
"eslint": "^9.11.1",
47+
"eslint-config-prettier": "^9.1.0",
48+
"jest": "^29.7.0",
49+
"prettier": "^3.3.3",
50+
"rimraf": "^6.0.1",
51+
"typescript": "^5.6.2",
52+
"typescript-eslint": "^8.7.0"
53+
},
54+
"peerDependencies": {
55+
"@babel/core": "^7.0.0-0"
56+
},
57+
"engines": {
58+
"node": ">=6.9.0"
59+
},
60+
"publishConfig": {
61+
"access": "public"
62+
}
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`basic-sample 1`] = `
4+
"var _jsxFileName = "/fake/path/mock.js";
5+
var x = /*#__PURE__*/React.createElement("sometag", {
6+
style: {
7+
'--source-code-location': _jsxFileName + ':' + 2 + ':' + 9
8+
}
9+
});"
10+
`;
11+
12+
exports[`no-jsx 1`] = `"var x = 42;"`;
13+
14+
exports[`with-style add add string style 1`] = `
15+
"var _jsxFileName = "/fake/path/mock.js";
16+
var x = /*#__PURE__*/React.createElement("sometag", {
17+
style: '--source-code-location:' + (_jsxFileName + ':' + 2 + ':' + 9) + ';' + ""
18+
});
19+
var x = /*#__PURE__*/React.createElement("sometag", {
20+
style: '--source-code-location:' + (_jsxFileName + ':' + 3 + ':' + 9) + ';' + ""
21+
});
22+
var x = /*#__PURE__*/React.createElement("sometag", {
23+
style: '--source-code-location:' + (_jsxFileName + ':' + 4 + ':' + 9) + ';' + 0
24+
});
25+
var x = /*#__PURE__*/React.createElement("sometag", {
26+
style: '--source-code-location:' + (_jsxFileName + ':' + 5 + ':' + 9) + ';' + 1
27+
});
28+
var x = /*#__PURE__*/React.createElement("sometag", {
29+
style: '--source-code-location:' + (_jsxFileName + ':' + 6 + ':' + 9) + ';' + "--source-code-location: test"
30+
});"
31+
`;
32+
33+
exports[`with-style add assign object style 1`] = `
34+
"var _jsxFileName = "/fake/path/mock.js";
35+
var x = /*#__PURE__*/React.createElement("sometag", {
36+
style: Object.assign({
37+
'--source-code-location': _jsxFileName + ':' + 2 + ':' + 9
38+
}, null)
39+
});
40+
var x = /*#__PURE__*/React.createElement("sometag", {
41+
style: Object.assign({
42+
'--source-code-location': _jsxFileName + ':' + 3 + ':' + 9
43+
}, undefined)
44+
});
45+
var x = /*#__PURE__*/React.createElement("sometag", {
46+
style: Object.assign({
47+
'--source-code-location': _jsxFileName + ':' + 4 + ':' + 9
48+
}, {
49+
'--source-code-location': test
50+
})
51+
});
52+
var x = /*#__PURE__*/React.createElement("sometag", {
53+
style: Object.assign({
54+
'--source-code-location': _jsxFileName + ':' + 5 + ':' + 9
55+
}, styleObj)
56+
});"
57+
`;

src/__tests__/index.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const babel = require("@babel/core");
2+
const plugin = require("../../");
3+
4+
const babelConfig = {
5+
filename: "/fake/path/mock.js",
6+
presets: ["@babel/preset-react"],
7+
plugins: [plugin],
8+
};
9+
10+
it("basic-sample", () => {
11+
const example = `
12+
var x = <sometag />;
13+
`;
14+
const { code } = babel.transform(example, babelConfig);
15+
expect(code).toMatchSnapshot();
16+
});
17+
18+
it("no-jsx", () => {
19+
const example = `
20+
var x = 42;
21+
`;
22+
const { code } = babel.transform(example, babelConfig);
23+
expect(code).toMatchSnapshot();
24+
});
25+
26+
it("with-style add assign object style", () => {
27+
const example = `
28+
var x = <sometag style={null} />;
29+
var x = <sometag style={undefined} />;
30+
var x = <sometag style={{'--source-code-location': test}} />;
31+
var x = <sometag style={styleObj} />;
32+
`;
33+
const { code } = babel.transform(example, babelConfig);
34+
expect(code).toMatchSnapshot();
35+
});
36+
37+
it("with-style add add string style", () => {
38+
const example = `
39+
var x = <sometag style="" />;
40+
var x = <sometag style={""} />;
41+
var x = <sometag style={0} />;
42+
var x = <sometag style={1} />;
43+
var x = <sometag style={"--source-code-location: test"} />;
44+
`;
45+
const { code } = babel.transform(example, babelConfig);
46+
expect(code).toMatchSnapshot();
47+
});

0 commit comments

Comments
 (0)