rollup-plugin-react-scoped-css 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.github/workflows/pull-request.yml +19 -0
  2. package/.github/workflows/release.yml +30 -0
  3. package/.releaserc +12 -0
  4. package/README.md +25 -2
  5. package/dist/ast-iterator.d.ts +1 -0
  6. package/dist/ast-iterator.js +114 -0
  7. package/dist/ast-transformer.d.ts +1 -0
  8. package/dist/ast-transformer.js +50 -0
  9. package/dist/ast.utils.d.ts +4 -0
  10. package/dist/ast.utils.js +46 -108
  11. package/dist/index.js +12 -12
  12. package/dist/parsers/classic-parser.d.ts +3 -2
  13. package/dist/parsers/classic-parser.js +76 -19
  14. package/dist/parsers/jsx-parser.model.d.ts +0 -3
  15. package/dist/parsers/new-parser.d.ts +3 -2
  16. package/dist/parsers/new-parser.js +78 -21
  17. package/package.json +8 -3
  18. package/src/__mocks__/{compiled-jsx-with-attr.js → classic-compiled-jsx-with-attr.js} +0 -0
  19. package/src/__mocks__/{compiled-jsx.js → classic-compiled-jsx.js} +0 -0
  20. package/src/__mocks__/new-compiled-jsx-with-attr.js +101 -0
  21. package/src/__mocks__/new-compiled-jsx.js +64 -0
  22. package/src/__mocks__/sample-code-file.js +4 -0
  23. package/src/ast-iterator.spec.ts +19 -0
  24. package/src/ast-iterator.ts +27 -0
  25. package/src/ast-transformer.spec.ts +20 -0
  26. package/src/ast-transformer.ts +24 -0
  27. package/src/ast.utils.spec.ts +36 -12
  28. package/src/ast.utils.ts +32 -95
  29. package/src/parsers/classic-parser.ts +58 -0
  30. package/src/parsers/jsx-parser.model.ts +10 -0
  31. package/src/parsers/new-parser.ts +58 -0
  32. package/tsconfig.json +2 -1
  33. package/dist/parsers/base-parser.d.ts +0 -15
  34. package/dist/parsers/base-parser.js +0 -109
  35. package/dist/parsers/jsx-parser.interface.d.ts +0 -6
  36. package/dist/parsers/jsx-parser.interface.js +0 -2
@@ -0,0 +1,58 @@
1
+ import { JsxParser } from "./jsx-parser.model";
2
+
3
+ export class ClassicJsxParser implements JsxParser {
4
+ isNodeReactFragment(node: any) {
5
+ return node?.arguments?.[0]?.object?.name === "React" &&
6
+ node?.arguments?.[0]?.property?.name === "Fragment"
7
+ }
8
+ isNodeReactElement(node: any) {
9
+ return node?.type === 'CallExpression' &&
10
+ node?.callee?.object?.name === 'React' &&
11
+ node?.callee?.property?.name === 'createElement'
12
+ }
13
+
14
+ public extendNodeWithAttributes(node: any, attr: string) {
15
+ const arg = node?.arguments?.[1]
16
+ return {
17
+ ...node,
18
+ arguments: node?.arguments.map((v: any, i: number) => {
19
+ if (i === 1) {
20
+ if (v.type === 'ObjectExpression') {
21
+ return {
22
+ ...v,
23
+ properties: [
24
+ ...arg.properties,
25
+ this.createAttributeNode(attr)
26
+ ]
27
+ }
28
+ } else {
29
+ return {
30
+ type: 'ObjectExpression',
31
+ properties: [this.createAttributeNode(attr)]
32
+ }
33
+ }
34
+ }
35
+ return v
36
+ })
37
+ }
38
+ }
39
+
40
+ private createAttributeNode(attr: string) {
41
+ return {
42
+ type: 'Property',
43
+ method: false,
44
+ shorthand: false,
45
+ computed: false,
46
+ key: {
47
+ type: 'Identifier',
48
+ name: `"${attr}"`
49
+ },
50
+ value: {
51
+ type: 'Literal',
52
+ value: true,
53
+ raw: "true"
54
+ },
55
+ kind: 'init'
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,10 @@
1
+ export interface JsxParser {
2
+ isNodeReactElement: (node: any) => boolean
3
+ isNodeReactFragment: (node: any) => boolean
4
+ extendNodeWithAttributes: (node: any, attr: string) => any
5
+ }
6
+
7
+ export enum ParserImplementations {
8
+ new = 'new',
9
+ classic = 'classic'
10
+ }
@@ -0,0 +1,58 @@
1
+ import { JsxParser } from "./jsx-parser.model";
2
+
3
+ export class NewJsxParser implements JsxParser {
4
+ isNodeReactFragment(node: any) {
5
+ return node.arguments?.[0]?.type === "Identifier" &&
6
+ ['_Fragment', 'Fragment'].includes(node.arguments?.[0]?.name)
7
+ }
8
+
9
+ isNodeReactElement(node: any) {
10
+ return node?.type === 'CallExpression' &&
11
+ ['_jsxDEV', '_jsx', '_jsxs', 'jsxDev', 'jsx', 'jsxs'].includes(node?.callee?.name)
12
+ }
13
+
14
+ public extendNodeWithAttributes(node: any, attr: string) {
15
+ const arg = node?.arguments?.[1]
16
+ return {
17
+ ...node,
18
+ arguments: node?.arguments.map((v: any, i: number) => {
19
+ if (i === 1) {
20
+ if (v.type === 'ObjectExpression') {
21
+ return {
22
+ ...v,
23
+ properties: [
24
+ ...arg.properties,
25
+ this.createAttributeNode(attr)
26
+ ]
27
+ }
28
+ } else {
29
+ return {
30
+ type: 'ObjectExpression',
31
+ properties: [this.createAttributeNode(attr)]
32
+ }
33
+ }
34
+ }
35
+ return v
36
+ })
37
+ }
38
+ }
39
+
40
+ private createAttributeNode(attr: string) {
41
+ return {
42
+ type: 'Property',
43
+ method: false,
44
+ shorthand: false,
45
+ computed: false,
46
+ key: {
47
+ type: 'Identifier',
48
+ name: `"${attr}"`
49
+ },
50
+ value: {
51
+ type: 'Literal',
52
+ value: true,
53
+ raw: "true"
54
+ },
55
+ kind: 'init'
56
+ }
57
+ }
58
+ }
package/tsconfig.json CHANGED
@@ -6,7 +6,8 @@
6
6
  "declaration": true,
7
7
  "outDir": "dist",
8
8
  "strict": true,
9
- "esModuleInterop": true
9
+ "esModuleInterop": true,
10
+ "downlevelIteration": true
10
11
  },
11
12
  "exclude": [
12
13
  "node_modules",
@@ -1,15 +0,0 @@
1
- import { JsxParser, ParserImplementations } from "./jsx-parser.model";
2
- export declare class BaseParser implements Pick<JsxParser, 'isNode' & 'traverse'> {
3
- isNode(obj: any): boolean;
4
- /**
5
- * Traverses the AST tree and calls callback for each node.
6
- * -
7
- * @param ast The ast tree
8
- * @param callback Callback function to be called for each node
9
- * @returns
10
- */
11
- traverse(ast: any, callback: Function): any;
12
- findImplementation(ast: any): ParserImplementations.classic;
13
- extendNodeWithAttributes(node: any, attr: string): any;
14
- private createAttributeNode;
15
- }
@@ -1,109 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
14
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
15
- to[j] = from[i];
16
- return to;
17
- };
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.BaseParser = void 0;
20
- var jsx_parser_model_1 = require("./jsx-parser.model");
21
- var BaseParser = /** @class */ (function () {
22
- function BaseParser() {
23
- }
24
- BaseParser.prototype.isNode = function (obj) {
25
- var _a, _b, _c;
26
- return typeof obj === 'object' &&
27
- typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.end) === 'number' &&
28
- typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.start) === 'number' &&
29
- typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.type) === 'string';
30
- };
31
- /**
32
- * Traverses the AST tree and calls callback for each node.
33
- * -
34
- * @param ast The ast tree
35
- * @param callback Callback function to be called for each node
36
- * @returns
37
- */
38
- BaseParser.prototype.traverse = function (ast, callback) {
39
- for (var _i = 0, _a = Object.keys(ast); _i < _a.length; _i++) {
40
- var key = _a[_i];
41
- if (['end', 'start', 'type'].includes(key)) {
42
- continue;
43
- }
44
- if (Array.isArray(ast[key])) {
45
- for (var nodeIndex in ast[key]) {
46
- ast[key][nodeIndex] = this.traverse(ast[key][nodeIndex], callback);
47
- }
48
- }
49
- else if (this.isNode(ast[key])) {
50
- ast[key] = this.traverse(ast[key], callback);
51
- }
52
- }
53
- var result = callback(ast);
54
- return typeof result !== 'undefined' ? result : ast;
55
- };
56
- BaseParser.prototype.findImplementation = function (ast) {
57
- var implementation = jsx_parser_model_1.ParserImplementations.classic;
58
- this.traverse(ast, function (node) {
59
- if (node.type !== 'ImportDeclaration') {
60
- return;
61
- }
62
- if (node.source.value === 'react/jsx-runtime') {
63
- implementation = jsx_parser_model_1.ParserImplementations.new;
64
- }
65
- });
66
- return implementation;
67
- };
68
- BaseParser.prototype.extendNodeWithAttributes = function (node, attr) {
69
- var _this = this;
70
- var _a;
71
- var arg = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[1];
72
- return __assign(__assign({}, node), { arguments: node === null || node === void 0 ? void 0 : node.arguments.map(function (v, i) {
73
- if (i === 1) {
74
- if (v.type === 'ObjectExpression') {
75
- return __assign(__assign({}, v), { properties: __spreadArray(__spreadArray([], arg.properties), [
76
- _this.createAttributeNode(attr)
77
- ]) });
78
- }
79
- else {
80
- return {
81
- type: 'ObjectExpression',
82
- properties: [_this.createAttributeNode(attr)]
83
- };
84
- }
85
- }
86
- return v;
87
- }) });
88
- };
89
- BaseParser.prototype.createAttributeNode = function (attr) {
90
- return {
91
- type: 'Property',
92
- method: false,
93
- shorthand: false,
94
- computed: false,
95
- key: {
96
- type: 'Identifier',
97
- name: "\"" + attr + "\""
98
- },
99
- value: {
100
- type: 'Literal',
101
- value: true,
102
- raw: "true"
103
- },
104
- kind: 'init'
105
- };
106
- };
107
- return BaseParser;
108
- }());
109
- exports.BaseParser = BaseParser;
@@ -1,6 +0,0 @@
1
- export interface JsxParser {
2
- isNode: (node: any) => boolean;
3
- isNodeReactElement: (node: any) => boolean;
4
- isNodeReactFragment: (node: any) => boolean;
5
- traverse: (ast: any, callback: (ast: any) => void) => void;
6
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });