@markuplint/pretenders 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +138 -0
- package/bin/pretenders.mjs +3 -0
- package/lib/cli.d.ts +1 -0
- package/lib/cli.js +31 -0
- package/lib/create-scanner.d.ts +3 -0
- package/lib/create-scanner.js +11 -0
- package/lib/dependency-mapper.d.ts +10 -0
- package/lib/dependency-mapper.js +86 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +1 -0
- package/lib/input.d.ts +1 -0
- package/lib/input.js +13 -0
- package/lib/jsx/create-identify.d.ts +3 -0
- package/lib/jsx/create-identify.js +30 -0
- package/lib/jsx/finder.d.ts +2 -0
- package/lib/jsx/finder.js +17 -0
- package/lib/jsx/get-attributes.d.ts +3 -0
- package/lib/jsx/get-attributes.js +44 -0
- package/lib/jsx/get-children.d.ts +3 -0
- package/lib/jsx/get-children.js +10 -0
- package/lib/jsx/index.d.ts +3 -0
- package/lib/jsx/index.js +276 -0
- package/lib/jsx/types.d.ts +10 -0
- package/lib/jsx/types.js +1 -0
- package/lib/out.d.ts +2 -0
- package/lib/out.js +9 -0
- package/lib/pretender-director.d.ts +6 -0
- package/lib/pretender-director.js +22 -0
- package/lib/types.d.ts +15 -0
- package/lib/types.js +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-2024 Yusuke Hirao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# @markuplint/pretenders
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@markuplint/pretenders)
|
|
4
|
+
[](https://travis-ci.org/markuplint/markuplint)
|
|
5
|
+
[](https://coveralls.io/github/markuplint/markuplint?branch=main)
|
|
6
|
+
|
|
7
|
+
This module features both an API and a CLI that generate **[Pretenders](https://markuplint.dev/docs/guides/besides-html#pretenders) data** from the loaded components
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
$ npx @markuplint/pretenders "./src/**/*.jsx" --out "./pretenders.json"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The module analyzes components defined in files using a parser, currently supporting JSX (both `*.jsx` and `*.tsx` formats). It searches for functions or function objects that return elements and maps their function names or the variable names holding these function objects. For example, if a function object named `Foo` returns a `<div>`, the component `Foo` is considered as pretending to be a `div`. In the CLI, it exports the mapped data as a JSON file. By loading this JSON file into the Pretenders feature, the module evaluates the `Foo` component as equivalent to a `div`.
|
|
16
|
+
|
|
17
|
+
```jsx
|
|
18
|
+
const Foo = () => <div />;
|
|
19
|
+
|
|
20
|
+
function Bar() {
|
|
21
|
+
return <span />;
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
[
|
|
27
|
+
{
|
|
28
|
+
"selector": "Foo",
|
|
29
|
+
"as": "div"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"selector": "Bar",
|
|
33
|
+
"as": "span"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The module is **experimental**. It uses the TypeScript compiler to identify functions or function objects in JSX files where the return values are components or HTML elements. Currently, it only performs a simplistic mapping based on function and variable names without considering dependencies between files. **Consequently, it does not handle name duplications across files or variable scopes;** components with duplicate names overwrite existing data during processing.
|
|
39
|
+
|
|
40
|
+
In addition to definitions based on function and variable names, the module also infers HTML elements from properties, as exemplified by `styled-components`, and infers dependencies from arguments.
|
|
41
|
+
|
|
42
|
+
```jsx
|
|
43
|
+
const Foo = styled.div`
|
|
44
|
+
color: red;
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const Bar = styled(Foo)`
|
|
48
|
+
background-color: blue;
|
|
49
|
+
`;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
[
|
|
54
|
+
{
|
|
55
|
+
"selector": "Foo",
|
|
56
|
+
"as": "div"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"selector": "Bar",
|
|
60
|
+
"as": "div"
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `jsxScanner(files, options)`
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { jsxScanner } from '@markuplint/pretenders';
|
|
71
|
+
|
|
72
|
+
const pretenders = jsxScanner(['./src/**/*.jsx'], {
|
|
73
|
+
cwd: process.cwd(),
|
|
74
|
+
asFragment: [/(?:^|\.)provider$/i],
|
|
75
|
+
ignoreComponentNames: [],
|
|
76
|
+
taggedStylingComponent: [
|
|
77
|
+
// PropertyAccessExpression: styled.button`css-prop: value;`
|
|
78
|
+
/^styled\.(?<tagName>[a-z][\da-z]*)$/i,
|
|
79
|
+
// CallExpression: styled(Button)`css-prop: value;`
|
|
80
|
+
/^styled\s*\(\s*(?<tagName>[a-z][\da-z]*)\s*\)$/i,
|
|
81
|
+
],
|
|
82
|
+
extendingWrapper: [],
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### `files`
|
|
87
|
+
|
|
88
|
+
Type: `string[]`
|
|
89
|
+
|
|
90
|
+
An array of file paths to scan.
|
|
91
|
+
|
|
92
|
+
##### `options.cwd`
|
|
93
|
+
|
|
94
|
+
Type: `string`
|
|
95
|
+
|
|
96
|
+
The current working directory.
|
|
97
|
+
|
|
98
|
+
##### `options.asFragment`
|
|
99
|
+
|
|
100
|
+
Type: `RegExp[]`
|
|
101
|
+
|
|
102
|
+
A list of regular expressions to match components that should be treated as fragments.
|
|
103
|
+
|
|
104
|
+
##### `options.ignoreComponentNames`
|
|
105
|
+
|
|
106
|
+
Type: `string[]`
|
|
107
|
+
|
|
108
|
+
A list of component names to ignore.
|
|
109
|
+
|
|
110
|
+
##### `options.taggedStylingComponent`
|
|
111
|
+
|
|
112
|
+
Type: `RegExp[]`
|
|
113
|
+
|
|
114
|
+
A list of regular expressions to match components that are styled.
|
|
115
|
+
|
|
116
|
+
##### `options.extendingWrapper`
|
|
117
|
+
|
|
118
|
+
Type: `RegExp[]` | `{ identifier: RegExp, numberOfArgument: number }[]`
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
jsxScanner(['./src/**/*.jsx'], {
|
|
122
|
+
extendingWrapper: [
|
|
123
|
+
{
|
|
124
|
+
identifier: /^namespace\.primary$/i,
|
|
125
|
+
numberOfArgument: 1,
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```jsx
|
|
132
|
+
const Foo = <div />;
|
|
133
|
+
const Bar = namespace.primary(true, Foo);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
A list of regular expressions to match components that are extended.
|
|
137
|
+
`identifier` is a regular expression to match the component name.
|
|
138
|
+
`numberOfArgument` is the number of arguments to pass to the component.
|
package/lib/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import meow from 'meow';
|
|
3
|
+
import { getFileList } from './input.js';
|
|
4
|
+
import { jsxScanner } from './jsx/index.js';
|
|
5
|
+
import { out } from './out.js';
|
|
6
|
+
const commands = meow({
|
|
7
|
+
importMeta: import.meta,
|
|
8
|
+
flags: {
|
|
9
|
+
out: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
isRequired: true,
|
|
12
|
+
shortFlag: 'O',
|
|
13
|
+
},
|
|
14
|
+
ignore: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (commands.input.length === 0) {
|
|
20
|
+
commands.showHelp(1);
|
|
21
|
+
}
|
|
22
|
+
async function main() {
|
|
23
|
+
const files = await getFileList(commands.input);
|
|
24
|
+
const jsxFiles = files.filter(filePath => /\.[jt]sx?$/.test(filePath));
|
|
25
|
+
const pretenders = await jsxScanner(jsxFiles, {
|
|
26
|
+
ignoreComponentNames: commands.flags.ignore?.split(',').map(s => s.trim()),
|
|
27
|
+
});
|
|
28
|
+
const outFilePath = path.resolve(process.cwd(), commands.flags.out);
|
|
29
|
+
await out(outFilePath, pretenders);
|
|
30
|
+
}
|
|
31
|
+
await main();
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { PretenderScannerScanMethod } from './types.js';
|
|
2
|
+
import type { PretenderScanOptions } from '@markuplint/ml-config';
|
|
3
|
+
export declare function createScanner<O extends PretenderScanOptions = PretenderScanOptions>(method: PretenderScannerScanMethod<O>): (files: readonly string[], options?: O) => Promise<import("@markuplint/ml-config").Pretender[]>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
export function createScanner(method) {
|
|
3
|
+
return (files, options) => {
|
|
4
|
+
for (const file of files) {
|
|
5
|
+
if (!path.isAbsolute(file)) {
|
|
6
|
+
throw new ReferenceError(`A path is not an absolute path: ${file}`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return method(files, options);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Identifier, Identity } from './types.js';
|
|
2
|
+
import type { Pretender } from '@markuplint/ml-config';
|
|
3
|
+
type PretenderDirectorMap = Map<Identifier, [identity: Identity, filePath?: string]>;
|
|
4
|
+
export declare class PretenderDirector {
|
|
5
|
+
#private;
|
|
6
|
+
add(identifier: Identifier, identity: Identity, filePath: string, line: number, col: number): void;
|
|
7
|
+
getPretenders(): Pretender[];
|
|
8
|
+
}
|
|
9
|
+
export declare function dependencyMapper(map: Readonly<PretenderDirectorMap>): Pretender[];
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _PretenderDirector_map;
|
|
7
|
+
export class PretenderDirector {
|
|
8
|
+
constructor() {
|
|
9
|
+
_PretenderDirector_map.set(this, new Map());
|
|
10
|
+
}
|
|
11
|
+
add(identifier, identity, filePath, line, col) {
|
|
12
|
+
if (__classPrivateFieldGet(this, _PretenderDirector_map, "f").has(identifier)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
__classPrivateFieldGet(this, _PretenderDirector_map, "f").set(identifier, [identity, `${filePath}:${line}:${col}`]);
|
|
16
|
+
}
|
|
17
|
+
getPretenders() {
|
|
18
|
+
return dependencyMapper(__classPrivateFieldGet(this, _PretenderDirector_map, "f"));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
_PretenderDirector_map = new WeakMap();
|
|
22
|
+
export function dependencyMapper(map) {
|
|
23
|
+
const linkedPretenders = [];
|
|
24
|
+
const collection = [...map.entries()];
|
|
25
|
+
for (const [identifier, [_identity, _filePath]] of collection) {
|
|
26
|
+
let identity = _identity;
|
|
27
|
+
let filePath = _filePath;
|
|
28
|
+
let elName = getElName(identity);
|
|
29
|
+
const via = [];
|
|
30
|
+
// eslint-disable-next-line no-constant-condition
|
|
31
|
+
while (true) {
|
|
32
|
+
const mappedPretender = map.get(elName);
|
|
33
|
+
if (!mappedPretender) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
identity = mappedPretender[0];
|
|
37
|
+
filePath = mappedPretender[1];
|
|
38
|
+
if (elName === identifier) {
|
|
39
|
+
via.push('...[Recursive]');
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
via.push(elName);
|
|
43
|
+
elName = getElName(identity);
|
|
44
|
+
}
|
|
45
|
+
const pretender = {
|
|
46
|
+
selector: identifier,
|
|
47
|
+
as: identity,
|
|
48
|
+
};
|
|
49
|
+
if (filePath) {
|
|
50
|
+
// @ts-ignore initialize readonly property
|
|
51
|
+
pretender.filePath = filePath;
|
|
52
|
+
}
|
|
53
|
+
if (via.length > 0) {
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
pretender._via = via;
|
|
56
|
+
}
|
|
57
|
+
linkedPretenders.push(pretender);
|
|
58
|
+
}
|
|
59
|
+
return linkedPretenders.sort(propSort('selector'));
|
|
60
|
+
}
|
|
61
|
+
function getElName(identity) {
|
|
62
|
+
if (typeof identity === 'string') {
|
|
63
|
+
return identity;
|
|
64
|
+
}
|
|
65
|
+
return identity.element;
|
|
66
|
+
}
|
|
67
|
+
function propSort(propName) {
|
|
68
|
+
return (a, b) => {
|
|
69
|
+
const nameA = toLowerCase(a[propName]);
|
|
70
|
+
const nameB = toLowerCase(b[propName]);
|
|
71
|
+
if (nameA < nameB) {
|
|
72
|
+
return -1;
|
|
73
|
+
}
|
|
74
|
+
if (nameA > nameB) {
|
|
75
|
+
return 1;
|
|
76
|
+
}
|
|
77
|
+
return 0;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function toLowerCase(value) {
|
|
81
|
+
if (typeof value === 'string') {
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
return value.toLowerCase();
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsxScanner } from './jsx/index.js';
|
package/lib/input.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getFileList(input: readonly string[]): Promise<string[]>;
|
package/lib/input.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { glob } from 'glob';
|
|
3
|
+
export async function getFileList(input) {
|
|
4
|
+
const result = await Promise.all(input
|
|
5
|
+
.map(filePath => {
|
|
6
|
+
if (path.isAbsolute(filePath)) {
|
|
7
|
+
return filePath;
|
|
8
|
+
}
|
|
9
|
+
return path.resolve(process.cwd(), filePath);
|
|
10
|
+
})
|
|
11
|
+
.map(filePath => glob(filePath)));
|
|
12
|
+
return result.flat();
|
|
13
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function createIndentity(tagName, attrs, slots) {
|
|
2
|
+
if (attrs.length === 0) {
|
|
3
|
+
return tagName;
|
|
4
|
+
}
|
|
5
|
+
const availableAttrs = attrs.filter(attr => attr.nodeType !== 'spread');
|
|
6
|
+
const hasSpread = attrs.some(attr => attr.nodeType === 'spread');
|
|
7
|
+
const pretenderAttrs = availableAttrs.map(attr => {
|
|
8
|
+
const pretenderAttr = {
|
|
9
|
+
name: attr.name,
|
|
10
|
+
};
|
|
11
|
+
if (attr.nodeType === 'static' && attr.value) {
|
|
12
|
+
// @ts-ignore initialize readonly property
|
|
13
|
+
pretenderAttr.value = attr.value;
|
|
14
|
+
}
|
|
15
|
+
return pretenderAttr;
|
|
16
|
+
});
|
|
17
|
+
const identify = {
|
|
18
|
+
element: tagName,
|
|
19
|
+
slots,
|
|
20
|
+
};
|
|
21
|
+
if (pretenderAttrs.length > 0) {
|
|
22
|
+
// @ts-ignore initialize readonly property
|
|
23
|
+
identify.attrs = pretenderAttrs;
|
|
24
|
+
}
|
|
25
|
+
if (hasSpread) {
|
|
26
|
+
// @ts-ignore initialize readonly property
|
|
27
|
+
identify.inheritAttrs = true;
|
|
28
|
+
}
|
|
29
|
+
return identify;
|
|
30
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
/* eslint-disable import/no-named-as-default-member */
|
|
3
|
+
const { forEachChild } = ts;
|
|
4
|
+
/* eslint-enable import/no-named-as-default-member */
|
|
5
|
+
export function finder(
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
7
|
+
sourceFile) {
|
|
8
|
+
return function find(
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
10
|
+
node, is, visit) {
|
|
11
|
+
if (is(node)) {
|
|
12
|
+
visit(node, sourceFile);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
forEachChild(node, node => find(node, is, visit));
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { finder } from './finder.js';
|
|
3
|
+
/* eslint-disable import/no-named-as-default-member */
|
|
4
|
+
const { isJsxAttribute, isJsxExpression, isJsxSpreadAttribute, isStringLiteral } = ts;
|
|
5
|
+
/* eslint-enable import/no-named-as-default-member */
|
|
6
|
+
export function getAttributes(
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
8
|
+
el,
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
10
|
+
sourceFile) {
|
|
11
|
+
const find = finder(sourceFile);
|
|
12
|
+
const attrs = [];
|
|
13
|
+
find(el.attributes, isJsxAttribute, attr => {
|
|
14
|
+
find(attr, isStringLiteral, str => {
|
|
15
|
+
attrs.push({
|
|
16
|
+
nodeType: 'static',
|
|
17
|
+
name: attr.name.getText(sourceFile),
|
|
18
|
+
value: str.text,
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
find(attr, isJsxExpression, exp => {
|
|
22
|
+
attrs.push({
|
|
23
|
+
nodeType: 'dynamic',
|
|
24
|
+
name: attr.name.getText(sourceFile),
|
|
25
|
+
value: exp.getText(sourceFile),
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
if (attr.getChildCount(sourceFile) === 1) {
|
|
29
|
+
attrs.push({
|
|
30
|
+
nodeType: 'boolean',
|
|
31
|
+
name: attr.name.getText(sourceFile),
|
|
32
|
+
value: '',
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
find(el.attributes, isJsxSpreadAttribute, attr => {
|
|
37
|
+
attrs.push({
|
|
38
|
+
nodeType: 'spread',
|
|
39
|
+
name: attr.name?.getText(sourceFile) ?? 'N/A',
|
|
40
|
+
value: 'N/A',
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
return attrs;
|
|
44
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// import { finder } from './finder.js';
|
|
2
|
+
export function getChildren(
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
4
|
+
el,
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
6
|
+
sourceFile) {
|
|
7
|
+
const children = [];
|
|
8
|
+
// const find = finder(sourceFile);
|
|
9
|
+
return children;
|
|
10
|
+
}
|
package/lib/jsx/index.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { getPosition } from '@markuplint/parser-utils/location';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { createScanner } from '../create-scanner.js';
|
|
5
|
+
import { PretenderDirector } from '../pretender-director.js';
|
|
6
|
+
import { createIndentity } from './create-identify.js';
|
|
7
|
+
import { finder } from './finder.js';
|
|
8
|
+
import { getAttributes } from './get-attributes.js';
|
|
9
|
+
import { getChildren } from './get-children.js';
|
|
10
|
+
/* eslint-disable import/no-named-as-default-member */
|
|
11
|
+
const { createProgram, forEachChild, isArrowFunction, isCallExpression, isFunctionDeclaration, isJsxFragment, isJsxOpeningElement, isJsxSelfClosingElement, isReturnStatement, isTaggedTemplateExpression, isVariableDeclaration, JsxEmit, } = ts;
|
|
12
|
+
/* eslint-enable import/no-named-as-default-member */
|
|
13
|
+
const defaultOptions = {
|
|
14
|
+
cwd: process.cwd(),
|
|
15
|
+
asFragment: [/(?:^|\.)provider$/i],
|
|
16
|
+
ignoreComponentNames: [],
|
|
17
|
+
taggedStylingComponent: [
|
|
18
|
+
// PropertyAccessExpression: styled.button`css-prop: value;`
|
|
19
|
+
/^styled\.(?<tagName>[a-z][\da-z]*)$/i,
|
|
20
|
+
// CallExpression: styled(Button)`css-prop: value;`
|
|
21
|
+
/^styled\s*\(\s*(?<tagName>[a-z][\da-z]*)\s*\)$/i,
|
|
22
|
+
],
|
|
23
|
+
extendingWrapper: [],
|
|
24
|
+
};
|
|
25
|
+
export const jsxScanner = createScanner((files, options = defaultOptions) => {
|
|
26
|
+
const { cwd = defaultOptions.cwd, ignoreComponentNames = defaultOptions.ignoreComponentNames, asFragment = defaultOptions.asFragment, taggedStylingComponent = defaultOptions.taggedStylingComponent, extendingWrapper = defaultOptions.extendingWrapper, } = options;
|
|
27
|
+
const director = new PretenderDirector();
|
|
28
|
+
const program = createProgram(files, {
|
|
29
|
+
jsx: JsxEmit.ReactJSX,
|
|
30
|
+
allowJs: true,
|
|
31
|
+
});
|
|
32
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
33
|
+
if (!sourceFile.isDeclarationFile) {
|
|
34
|
+
forEachChild(sourceFile, node => visit(node, sourceFile));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function visit(
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
39
|
+
root,
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
41
|
+
sourceFile) {
|
|
42
|
+
const find = finder(sourceFile);
|
|
43
|
+
/**
|
|
44
|
+
* ```
|
|
45
|
+
* const Component = ...
|
|
46
|
+
* ------^
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
find(root, isVariableDeclaration, define);
|
|
50
|
+
/**
|
|
51
|
+
* ```
|
|
52
|
+
* function Component (...) {...}
|
|
53
|
+
* ---------^
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
find(root, isFunctionDeclaration, define);
|
|
57
|
+
}
|
|
58
|
+
function define(
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
60
|
+
dec,
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
62
|
+
sourceFile) {
|
|
63
|
+
if (!dec.name) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const variableName = dec.name.getText(sourceFile);
|
|
67
|
+
if (ignoreComponentNames.includes(variableName)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if ('jsDoc' in dec && dec.jsDoc) {
|
|
71
|
+
const jsDoc = dec.jsDoc;
|
|
72
|
+
const pretendsTag = jsDoc
|
|
73
|
+
.flatMap(doc => doc.tags ?? [])
|
|
74
|
+
.find(doc => doc.tagName.text.toLowerCase() === 'pretends');
|
|
75
|
+
if (pretendsTag && pretendsTag.comment === 'null') {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const { line, column } = getPosition(sourceFile.text, dec.name.pos);
|
|
80
|
+
/**
|
|
81
|
+
* ```
|
|
82
|
+
* const Component = ...
|
|
83
|
+
* ------------------^
|
|
84
|
+
*
|
|
85
|
+
* function Component (...) {...}
|
|
86
|
+
* --------------------------^
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
findBody(dec, variableName, sourceFile, line, column);
|
|
90
|
+
}
|
|
91
|
+
function findBody(
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
93
|
+
root, name,
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
95
|
+
sourceFile, line, col) {
|
|
96
|
+
const filePath = path.relative(cwd, sourceFile.fileName);
|
|
97
|
+
const find = finder(sourceFile);
|
|
98
|
+
find(root, isReturnStatement, node => {
|
|
99
|
+
/**
|
|
100
|
+
* ```
|
|
101
|
+
* return <Foo></Foo>
|
|
102
|
+
* -------^
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
find(node, isJsxOpeningElement, foundElement);
|
|
106
|
+
/**
|
|
107
|
+
* ```
|
|
108
|
+
* return <Foo />
|
|
109
|
+
* -------^
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
find(node, isJsxSelfClosingElement, foundElement);
|
|
113
|
+
/**
|
|
114
|
+
* ```
|
|
115
|
+
* return <></>
|
|
116
|
+
* -------^
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
find(node, isJsxFragment, foundFragment);
|
|
120
|
+
});
|
|
121
|
+
find(root, isArrowFunction, fn => {
|
|
122
|
+
/**
|
|
123
|
+
* ```
|
|
124
|
+
* (...) => <Foo></Foo>
|
|
125
|
+
* ---------^
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
find(fn, isJsxOpeningElement, foundElement);
|
|
129
|
+
/**
|
|
130
|
+
* ```
|
|
131
|
+
* (...) => <Foo />
|
|
132
|
+
* ---------^
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
find(fn, isJsxSelfClosingElement, foundElement);
|
|
136
|
+
/**
|
|
137
|
+
* ```
|
|
138
|
+
* (...) => <></>
|
|
139
|
+
* ---------^
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
find(fn, isJsxFragment, foundFragment);
|
|
143
|
+
});
|
|
144
|
+
find(root, isTaggedTemplateExpression, tagged => {
|
|
145
|
+
const tag = tagged.tag.getText(sourceFile);
|
|
146
|
+
/**
|
|
147
|
+
* ```
|
|
148
|
+
* styled.button`
|
|
149
|
+
* -------^
|
|
150
|
+
* margin: ${margin};
|
|
151
|
+
* padding: ${padding};
|
|
152
|
+
* `
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
for (const _pattern of taggedStylingComponent) {
|
|
156
|
+
const pattern = typeof _pattern === 'string' ? toRegexp(_pattern) : _pattern;
|
|
157
|
+
const tagName = pattern.exec(tag)?.groups?.tagName;
|
|
158
|
+
if (!tagName) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
director.add(name, {
|
|
162
|
+
element: tagName,
|
|
163
|
+
slots: true,
|
|
164
|
+
inheritAttrs: true,
|
|
165
|
+
}, filePath, line, col);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
find(root, isCallExpression, method => {
|
|
169
|
+
const caller = method.expression.getText(sourceFile);
|
|
170
|
+
/**
|
|
171
|
+
* ```
|
|
172
|
+
* functionCaller(Button)
|
|
173
|
+
* ---------------^
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* Options: `{ numberOfArgument: 2 }`
|
|
177
|
+
* ```
|
|
178
|
+
* namespace.functionCaller(true, Button)
|
|
179
|
+
* -------------------------------^
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
for (const _pattern of extendingWrapper) {
|
|
183
|
+
let pattern;
|
|
184
|
+
let numberOfArgument = 1;
|
|
185
|
+
if (typeof _pattern === 'string') {
|
|
186
|
+
pattern = toRegexp(_pattern);
|
|
187
|
+
}
|
|
188
|
+
else if ('identifier' in _pattern) {
|
|
189
|
+
pattern =
|
|
190
|
+
typeof _pattern.identifier === 'string'
|
|
191
|
+
? toRegexp(_pattern.identifier)
|
|
192
|
+
: _pattern.identifier;
|
|
193
|
+
numberOfArgument = _pattern.numberOfArgument;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
pattern = _pattern;
|
|
197
|
+
}
|
|
198
|
+
if (!pattern.test(caller)) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const arg = method.arguments[numberOfArgument - 1];
|
|
202
|
+
const tagName = arg?.getText(sourceFile);
|
|
203
|
+
if (!tagName) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
director.add(name, {
|
|
207
|
+
element: tagName,
|
|
208
|
+
slots: true,
|
|
209
|
+
inheritAttrs: true,
|
|
210
|
+
}, filePath, line, col);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
function foundFragment(
|
|
214
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
215
|
+
fragment) {
|
|
216
|
+
/**
|
|
217
|
+
* ```
|
|
218
|
+
* <>
|
|
219
|
+
* <Foo></Foo>
|
|
220
|
+
* --^
|
|
221
|
+
* </>
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
find(fragment, isJsxOpeningElement, foundElement);
|
|
225
|
+
/**
|
|
226
|
+
* ```
|
|
227
|
+
* <>
|
|
228
|
+
* <Foo />
|
|
229
|
+
* --^
|
|
230
|
+
* </>
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
find(fragment, isJsxSelfClosingElement, foundElement);
|
|
234
|
+
}
|
|
235
|
+
function foundElement(
|
|
236
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
237
|
+
el) {
|
|
238
|
+
const tagName = el.tagName.getText(sourceFile);
|
|
239
|
+
for (const frag of asFragment) {
|
|
240
|
+
const pattern = typeof frag === 'string' ? toRegexp(frag) : frag;
|
|
241
|
+
/**
|
|
242
|
+
* ```
|
|
243
|
+
* <Provider>
|
|
244
|
+
* <Foo></Foo>
|
|
245
|
+
* --^
|
|
246
|
+
* </Provider>
|
|
247
|
+
*
|
|
248
|
+
* <Namespace.Provider>
|
|
249
|
+
* <Foo></Foo>
|
|
250
|
+
* --^
|
|
251
|
+
* </Namespace.Provider>
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
if (pattern.test(tagName)) {
|
|
255
|
+
for (const child of el.getChildren(sourceFile)) {
|
|
256
|
+
find(child, isJsxOpeningElement, foundElement);
|
|
257
|
+
find(child, isJsxSelfClosingElement, foundElement);
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const attrs = getAttributes(el, sourceFile);
|
|
263
|
+
const children = getChildren(el, sourceFile);
|
|
264
|
+
const identity = createIndentity(tagName, attrs, children);
|
|
265
|
+
director.add(name, identity, filePath, line, col);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return Promise.resolve(director.getPretenders());
|
|
269
|
+
});
|
|
270
|
+
function toRegexp(pattern) {
|
|
271
|
+
const matched = pattern.match(/^\/(.+)\/([gi]*)$/i);
|
|
272
|
+
if (matched?.[1]) {
|
|
273
|
+
return new RegExp(matched[1], matched[2]);
|
|
274
|
+
}
|
|
275
|
+
return new RegExp(pattern);
|
|
276
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PretenderScanOptions } from '@markuplint/ml-config';
|
|
2
|
+
export interface PretenderScanJSXOptions extends PretenderScanOptions {
|
|
3
|
+
asFragment?: readonly (Readonly<RegExp> | string)[];
|
|
4
|
+
taggedStylingComponent?: readonly (Readonly<RegExp> | string)[];
|
|
5
|
+
extendingWrapper?: readonly (string | Readonly<RegExp> | ExtendingWrapperCallerOptions)[];
|
|
6
|
+
}
|
|
7
|
+
export type ExtendingWrapperCallerOptions = {
|
|
8
|
+
readonly identifier: string | Readonly<RegExp>;
|
|
9
|
+
readonly numberOfArgument: number;
|
|
10
|
+
};
|
package/lib/jsx/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/out.d.ts
ADDED
package/lib/out.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { writeFile } from 'node:fs/promises';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
export async function out(filePath, data) {
|
|
5
|
+
await writeFile(filePath, JSON.stringify({
|
|
6
|
+
version: require('../package.json').version,
|
|
7
|
+
data,
|
|
8
|
+
}, null, 2), { encoding: 'utf8' });
|
|
9
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Identifier, Identity } from './types.js';
|
|
2
|
+
export declare class PretenderDirector {
|
|
3
|
+
#private;
|
|
4
|
+
add(identifier: Identifier, identity: Identity, filePath: string, line: number, col: number): void;
|
|
5
|
+
getPretenders(): import("packages/@markuplint/ml-config/lib/types.js").Pretender[];
|
|
6
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _PretenderDirector_map;
|
|
7
|
+
import { dependencyMapper } from './dependency-mapper.js';
|
|
8
|
+
export class PretenderDirector {
|
|
9
|
+
constructor() {
|
|
10
|
+
_PretenderDirector_map.set(this, new Map());
|
|
11
|
+
}
|
|
12
|
+
add(identifier, identity, filePath, line, col) {
|
|
13
|
+
if (__classPrivateFieldGet(this, _PretenderDirector_map, "f").has(identifier)) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
__classPrivateFieldGet(this, _PretenderDirector_map, "f").set(identifier, [identity, `${filePath}:${line}:${col}`]);
|
|
17
|
+
}
|
|
18
|
+
getPretenders() {
|
|
19
|
+
return dependencyMapper(__classPrivateFieldGet(this, _PretenderDirector_map, "f"));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
_PretenderDirector_map = new WeakMap();
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PretenderScanOptions, Pretender } from '@markuplint/ml-config';
|
|
2
|
+
export type PretenderScannerScanMethod<O extends PretenderScanOptions = PretenderScanOptions> =
|
|
3
|
+
/**
|
|
4
|
+
* @param files Absolute file paths. If it includes a relative path, throw an error.
|
|
5
|
+
* @param options
|
|
6
|
+
*/
|
|
7
|
+
(files: readonly string[], options?: Readonly<O>) => Promise<Pretender[]>;
|
|
8
|
+
export type Identifier = Pretender['selector'];
|
|
9
|
+
export type Identity = Pretender['as'];
|
|
10
|
+
export type Attr = {
|
|
11
|
+
readonly nodeType: 'static' | 'boolean' | 'dynamic' | 'spread';
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly value: string;
|
|
14
|
+
readonly type?: string;
|
|
15
|
+
};
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@markuplint/pretenders",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "It loads components and then creates the pretenders data from them.",
|
|
5
|
+
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
|
+
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"private": false,
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./lib/index.js",
|
|
13
|
+
"types": "./lib/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"pretenders": "./bin/pretenders.mjs"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"typedoc": {
|
|
23
|
+
"entryPoint": "./src/index.ts"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"clean": "tsc --build --clean"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@markuplint/ml-config": "4.5.1",
|
|
31
|
+
"@markuplint/parser-utils": "4.5.1",
|
|
32
|
+
"glob": "10.3.12",
|
|
33
|
+
"meow": "13.2.0",
|
|
34
|
+
"typescript": "5.4.5"
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "b029c86a6b3a9ea8189d2e5535e3023aaea753fd"
|
|
37
|
+
}
|