pgsql-parser 16.0.0 โ 17.1.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.
- package/README.md +70 -33
- package/esm/index.js +1 -3
- package/esm/utils/index.js +90 -0
- package/index.d.ts +1 -3
- package/index.js +4 -19
- package/package.json +17 -9
- package/utils/index.d.ts +4 -0
- package/utils/index.js +97 -0
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
<a href="https://www.npmjs.com/package/pgsql-parser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Fparser%2Fpackage.json"/></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
The real PostgreSQL parser for Node.js
|
|
17
|
+
The real PostgreSQL parser for Node.js, `pgsql-parser` provides symmetric parsing and deparsing of SQL statements using the actual [PostgreSQL parser](https://github.com/pganalyze/libpg_query). It allows you to parse SQL queries into AST and modify or reconstruct SQL queries from the AST.
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -22,30 +22,38 @@ The real PostgreSQL parser for Node.js. Built with the actual [PostgreSQL parser
|
|
|
22
22
|
npm install pgsql-parser
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
## Features
|
|
25
|
+
## Key Features
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
* ๐ ๏ธ **AST Manipulation:** - Easily modify parts of a SQL statement through the AST.
|
|
27
|
+
- **True PostgreSQL Parsing:** Utilizes the real PostgreSQL source code for accurate parsing.
|
|
28
|
+
- **Symmetric Parsing and Deparsing:** Convert SQL to AST and back, enabling query manipulation.
|
|
29
|
+
- **AST Manipulation:** Easily modify parts of a SQL statement through the AST.
|
|
30
|
+
- **WebAssembly Powered:** Cross-platform compatibility without native dependencies.
|
|
32
31
|
|
|
33
32
|
## API
|
|
34
33
|
|
|
35
34
|
The package exports both async and sync methods. Async methods handle initialization automatically, while sync methods require explicit initialization.
|
|
36
35
|
|
|
37
|
-
โ ๏ธ
|
|
36
|
+
โ ๏ธ We recommend using `@pgsql/deparser` instead of `deparse` from `pgsql-parser`. The deparser package is more complete, supports sub-expressions, and doesn't require the WebAssembly module, making it lighter and more flexible for most use cases. It will soon be deprecated, in a minor version bump.
|
|
38
37
|
|
|
39
38
|
### Async Methods (Recommended)
|
|
40
39
|
|
|
41
40
|
```typescript
|
|
42
|
-
import { parse, deparse } from 'pgsql-parser';
|
|
41
|
+
import { parse, deparse, parseFunction } from 'pgsql-parser';
|
|
43
42
|
|
|
44
43
|
// Parse SQL to AST
|
|
45
44
|
const stmts = await parse('SELECT * FROM test_table');
|
|
46
45
|
|
|
47
46
|
// Deparse AST back to SQL
|
|
48
47
|
const sql = await deparse(stmts);
|
|
48
|
+
|
|
49
|
+
// Parse PL/pgSQL functions
|
|
50
|
+
const funcAst = await parseFunction(`
|
|
51
|
+
CREATE FUNCTION get_count() RETURNS integer AS $$
|
|
52
|
+
BEGIN
|
|
53
|
+
RETURN (SELECT COUNT(*) FROM users);
|
|
54
|
+
END;
|
|
55
|
+
$$ LANGUAGE plpgsql;
|
|
56
|
+
`);
|
|
49
57
|
```
|
|
50
58
|
|
|
51
59
|
### Sync Methods
|
|
@@ -89,21 +97,20 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
|
|
|
89
97
|
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/launchql/pgsql-parser/tree/main/packages/utils) to create an AST for `deparse`:
|
|
90
98
|
|
|
91
99
|
```ts
|
|
92
|
-
import
|
|
93
|
-
import { RangeVar, SelectStmt } from '@pgsql/types';
|
|
100
|
+
import ast from '@pgsql/utils';
|
|
94
101
|
import { deparse } from 'pgsql-deparser';
|
|
95
102
|
|
|
96
103
|
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
|
|
97
|
-
const stmt
|
|
104
|
+
const stmt = ast.selectStmt({
|
|
98
105
|
targetList: [
|
|
99
|
-
|
|
100
|
-
val:
|
|
101
|
-
fields: [
|
|
106
|
+
ast.resTarget({
|
|
107
|
+
val: ast.columnRef({
|
|
108
|
+
fields: [ast.aStar()]
|
|
102
109
|
})
|
|
103
110
|
})
|
|
104
111
|
],
|
|
105
112
|
fromClause: [
|
|
106
|
-
|
|
113
|
+
ast.rangeVar({
|
|
107
114
|
relname: 'some_table',
|
|
108
115
|
inh: true,
|
|
109
116
|
relpersistence: 'p'
|
|
@@ -113,36 +120,66 @@ const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
|
|
|
113
120
|
op: 'SETOP_NONE'
|
|
114
121
|
});
|
|
115
122
|
|
|
116
|
-
// Modify the AST if needed
|
|
117
|
-
|
|
123
|
+
// Modify the AST if needed
|
|
124
|
+
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
118
125
|
|
|
119
126
|
// Deparse the modified AST back to a SQL string
|
|
120
|
-
console.log(
|
|
127
|
+
console.log(deparse(stmts));
|
|
121
128
|
|
|
122
129
|
// Output: SELECT * FROM another_table
|
|
123
130
|
```
|
|
124
131
|
|
|
125
|
-
##
|
|
132
|
+
## CLI
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
npm install -g pgsql-parser
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### usage
|
|
139
|
+
|
|
140
|
+
```sh
|
|
141
|
+
pgsql-parser <sqlfile>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Versions
|
|
145
|
+
|
|
146
|
+
As of PG 13, PG majors versions maintained will have a matching dedicated major npm version. Only the latest Postgres stable release receives active updates.
|
|
126
147
|
|
|
127
|
-
|
|
148
|
+
Our latest is built with `17-latest` branch from libpg_query
|
|
149
|
+
|
|
150
|
+
| PostgreSQL Major Version | libpg_query | Status | npm tag |
|
|
151
|
+
|--------------------------|-------------|---------------------|---------|
|
|
152
|
+
| 17 | 17-latest | Active Development | `latest` |
|
|
153
|
+
| 16 | (n/a) | Not supported |
|
|
154
|
+
| 15 | (n/a) | Not supported |
|
|
155
|
+
| 14 | (n/a) | Not supported |
|
|
156
|
+
| 13 | 13-latest | Only Critical Fixes | `13.16.0` |
|
|
157
|
+
| 12 | (n/a) | Not supported |
|
|
158
|
+
| 11 | (n/a) | Not supported |
|
|
159
|
+
| 10 | 10-latest | Not supported | `@1.3.1` ([tree](https://github.com/launchql/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
|
|
128
160
|
|
|
129
|
-
* **[Dan Lynch](https://github.com/pyramation)** โ official maintainer since 2018 and architect of the current implementation
|
|
130
|
-
* **[Lukas Fittl](https://github.com/lfittl)** for [libpg_query](https://github.com/pganalyze/libpg_query) โ the core PostgreSQL parser that powers this project
|
|
131
|
-
* **[Greg Richardson](https://github.com/gregnr)** for AST guidance and pushing the transition to WASM and multiple PG runtimes for better interoperability
|
|
132
|
-
* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings
|
|
133
|
-
* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser
|
|
134
161
|
|
|
135
162
|
## Related
|
|
136
163
|
|
|
137
|
-
* [pgsql-parser](https://
|
|
138
|
-
* [pgsql-deparser](https://
|
|
139
|
-
* [
|
|
140
|
-
* [@pgsql/
|
|
141
|
-
* [@pgsql/
|
|
142
|
-
* [@pgsql/utils](https://
|
|
143
|
-
* [pg-proto-parser](https://
|
|
164
|
+
* [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
|
|
165
|
+
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
|
|
166
|
+
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
|
|
167
|
+
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
|
|
168
|
+
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
|
|
169
|
+
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.
|
|
170
|
+
* [pg-proto-parser](https://github.com/launchql/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
144
171
|
* [libpg-query](https://github.com/launchql/libpg-query-node): The real PostgreSQL parser exposed for Node.js, used primarily in `pgsql-parser` for parsing and deparsing SQL queries.
|
|
145
172
|
|
|
173
|
+
## Credits
|
|
174
|
+
|
|
175
|
+
Thanks [@lfittl](https://github.com/lfittl) for building the core `libpg_query` suite:
|
|
176
|
+
|
|
177
|
+
* [libpg_query](https://github.com/pganalyze/libpg_query)
|
|
178
|
+
* [pg_query](https://github.com/lfittl/pg_query)
|
|
179
|
+
* [pg_query.go](https://github.com/lfittl/pg_query.go)
|
|
180
|
+
|
|
181
|
+
Thanks to [@zhm](https://github.com/zhm) for the [OG Parser](https://github.com/zhm/pg-query-parser/blob/master/LICENSE.md) that started it all.
|
|
182
|
+
|
|
146
183
|
## Disclaimer
|
|
147
184
|
|
|
148
185
|
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
package/esm/index.js
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
export { parse as parse, parseSync as parseSync, loadModule as loadModule } from 'libpg-query';
|
|
2
|
-
export { deparse, deparseSync, } from 'pgsql-deparser';
|
|
3
|
-
export * from '@pgsql/types';
|
|
1
|
+
export { parse as parse, parseSync as parseSync, parsePlPgSQL as parseFunction, deparseSync as deparseSync, deparse as deparse, loadModule as loadModule } from 'libpg-query';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
export const cleanLines = (sql) => {
|
|
3
|
+
return sql
|
|
4
|
+
.split('\n')
|
|
5
|
+
.map((l) => l.trim())
|
|
6
|
+
.filter((a) => a)
|
|
7
|
+
.join('\n');
|
|
8
|
+
};
|
|
9
|
+
export const transform = (obj, props) => {
|
|
10
|
+
let copy = null;
|
|
11
|
+
// Handle the 3 simple types, and null or undefined
|
|
12
|
+
if (obj == null || typeof obj !== 'object') {
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
15
|
+
// Handle Date
|
|
16
|
+
if (obj instanceof Date) {
|
|
17
|
+
copy = new Date();
|
|
18
|
+
copy.setTime(obj.getTime());
|
|
19
|
+
return copy;
|
|
20
|
+
}
|
|
21
|
+
// Handle Array
|
|
22
|
+
if (obj instanceof Array) {
|
|
23
|
+
copy = [];
|
|
24
|
+
for (let i = 0, len = obj.length; i < len; i++) {
|
|
25
|
+
copy[i] = transform(obj[i], props);
|
|
26
|
+
}
|
|
27
|
+
return copy;
|
|
28
|
+
}
|
|
29
|
+
// Handle Object
|
|
30
|
+
if (obj instanceof Object || typeof obj === 'object') {
|
|
31
|
+
copy = {};
|
|
32
|
+
for (const attr in obj) {
|
|
33
|
+
if (obj.hasOwnProperty(attr)) {
|
|
34
|
+
if (props.hasOwnProperty(attr)) {
|
|
35
|
+
if (typeof props[attr] === 'function') {
|
|
36
|
+
copy[attr] = props[attr](obj[attr]);
|
|
37
|
+
}
|
|
38
|
+
else if (props[attr].hasOwnProperty(obj[attr])) {
|
|
39
|
+
copy[attr] = props[attr][obj[attr]];
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
copy[attr] = transform(obj[attr], props);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
copy[attr] = transform(obj[attr], props);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
copy[attr] = transform(obj[attr], props);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return copy;
|
|
54
|
+
}
|
|
55
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
56
|
+
};
|
|
57
|
+
const noop = () => undefined;
|
|
58
|
+
export const cleanTree = (tree) => {
|
|
59
|
+
return transform(tree, {
|
|
60
|
+
stmt_len: noop,
|
|
61
|
+
stmt_location: noop,
|
|
62
|
+
location: noop,
|
|
63
|
+
DefElem: (obj) => {
|
|
64
|
+
if (obj.defname === 'as') {
|
|
65
|
+
if (Array.isArray(obj.arg) && obj.arg.length) {
|
|
66
|
+
// function
|
|
67
|
+
obj.arg[0].String.str = obj.arg[0].String.str.trim();
|
|
68
|
+
}
|
|
69
|
+
else if (obj.arg.List && obj.arg.List.items) {
|
|
70
|
+
// function
|
|
71
|
+
obj.arg.List.items[0].String.str = obj.arg.List.items[0].String.str.trim();
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// do stmt
|
|
75
|
+
obj.arg.String.str = obj.arg.String.str.trim();
|
|
76
|
+
}
|
|
77
|
+
return cleanTree(obj);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return cleanTree(obj);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
export const cleanTreeWithStmt = (tree) => {
|
|
86
|
+
return transform(tree, {
|
|
87
|
+
stmt_location: noop,
|
|
88
|
+
location: noop
|
|
89
|
+
});
|
|
90
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
export { parse as parse, parseSync as parseSync, loadModule as loadModule } from 'libpg-query';
|
|
2
|
-
export { deparse, deparseSync, } from 'pgsql-deparser';
|
|
3
|
-
export * from '@pgsql/types';
|
|
1
|
+
export { parse as parse, parseSync as parseSync, parsePlPgSQL as parseFunction, deparseSync as deparseSync, deparse as deparse, loadModule as loadModule } from 'libpg-query';
|
package/index.js
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
3
|
+
exports.loadModule = exports.deparse = exports.deparseSync = exports.parseFunction = exports.parseSync = exports.parse = void 0;
|
|
18
4
|
var libpg_query_1 = require("libpg-query");
|
|
19
5
|
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return libpg_query_1.parse; } });
|
|
20
6
|
Object.defineProperty(exports, "parseSync", { enumerable: true, get: function () { return libpg_query_1.parseSync; } });
|
|
7
|
+
Object.defineProperty(exports, "parseFunction", { enumerable: true, get: function () { return libpg_query_1.parsePlPgSQL; } });
|
|
8
|
+
Object.defineProperty(exports, "deparseSync", { enumerable: true, get: function () { return libpg_query_1.deparseSync; } });
|
|
9
|
+
Object.defineProperty(exports, "deparse", { enumerable: true, get: function () { return libpg_query_1.deparse; } });
|
|
21
10
|
Object.defineProperty(exports, "loadModule", { enumerable: true, get: function () { return libpg_query_1.loadModule; } });
|
|
22
|
-
var pgsql_deparser_1 = require("pgsql-deparser");
|
|
23
|
-
Object.defineProperty(exports, "deparse", { enumerable: true, get: function () { return pgsql_deparser_1.deparse; } });
|
|
24
|
-
Object.defineProperty(exports, "deparseSync", { enumerable: true, get: function () { return pgsql_deparser_1.deparseSync; } });
|
|
25
|
-
__exportStar(require("@pgsql/types"), exports);
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pgsql-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "17.1.0",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "The real PostgreSQL query parser",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
8
|
"types": "index.d.ts",
|
|
9
9
|
"homepage": "https://github.com/launchql/pgsql-parser",
|
|
10
|
-
"license": "
|
|
10
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public",
|
|
13
13
|
"directory": "dist"
|
|
@@ -19,11 +19,18 @@
|
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/launchql/pgsql-parser/issues"
|
|
21
21
|
},
|
|
22
|
+
"bin": {
|
|
23
|
+
"pgsql-parser": "main/cli.js"
|
|
24
|
+
},
|
|
22
25
|
"scripts": {
|
|
23
|
-
"copy": "copyfiles -f
|
|
26
|
+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
24
27
|
"clean": "rimraf dist",
|
|
28
|
+
"prepare": "npm run build",
|
|
25
29
|
"build": "npm run clean && tsc && tsc -p tsconfig.esm.json && npm run copy",
|
|
26
|
-
"
|
|
30
|
+
"build:dev": "npm run clean && tsc --declarationMap && tsc -p tsconfig.esm.json && npm run copy",
|
|
31
|
+
"lint": "eslint . --fix",
|
|
32
|
+
"test": "jest",
|
|
33
|
+
"test:watch": "jest --watch"
|
|
27
34
|
},
|
|
28
35
|
"keywords": [
|
|
29
36
|
"sql",
|
|
@@ -35,8 +42,9 @@
|
|
|
35
42
|
"database"
|
|
36
43
|
],
|
|
37
44
|
"dependencies": {
|
|
38
|
-
"@pgsql/types": "
|
|
39
|
-
"libpg-query": "
|
|
40
|
-
"
|
|
41
|
-
}
|
|
42
|
-
|
|
45
|
+
"@pgsql/types": "^17.1.0",
|
|
46
|
+
"libpg-query": "17.3.3",
|
|
47
|
+
"minimist": "^1.2.6"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "40ee618df37a501169847056a50558c453cd30a6"
|
|
50
|
+
}
|
package/utils/index.d.ts
ADDED
package/utils/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.cleanTreeWithStmt = exports.cleanTree = exports.transform = exports.cleanLines = void 0;
|
|
5
|
+
const cleanLines = (sql) => {
|
|
6
|
+
return sql
|
|
7
|
+
.split('\n')
|
|
8
|
+
.map((l) => l.trim())
|
|
9
|
+
.filter((a) => a)
|
|
10
|
+
.join('\n');
|
|
11
|
+
};
|
|
12
|
+
exports.cleanLines = cleanLines;
|
|
13
|
+
const transform = (obj, props) => {
|
|
14
|
+
let copy = null;
|
|
15
|
+
// Handle the 3 simple types, and null or undefined
|
|
16
|
+
if (obj == null || typeof obj !== 'object') {
|
|
17
|
+
return obj;
|
|
18
|
+
}
|
|
19
|
+
// Handle Date
|
|
20
|
+
if (obj instanceof Date) {
|
|
21
|
+
copy = new Date();
|
|
22
|
+
copy.setTime(obj.getTime());
|
|
23
|
+
return copy;
|
|
24
|
+
}
|
|
25
|
+
// Handle Array
|
|
26
|
+
if (obj instanceof Array) {
|
|
27
|
+
copy = [];
|
|
28
|
+
for (let i = 0, len = obj.length; i < len; i++) {
|
|
29
|
+
copy[i] = (0, exports.transform)(obj[i], props);
|
|
30
|
+
}
|
|
31
|
+
return copy;
|
|
32
|
+
}
|
|
33
|
+
// Handle Object
|
|
34
|
+
if (obj instanceof Object || typeof obj === 'object') {
|
|
35
|
+
copy = {};
|
|
36
|
+
for (const attr in obj) {
|
|
37
|
+
if (obj.hasOwnProperty(attr)) {
|
|
38
|
+
if (props.hasOwnProperty(attr)) {
|
|
39
|
+
if (typeof props[attr] === 'function') {
|
|
40
|
+
copy[attr] = props[attr](obj[attr]);
|
|
41
|
+
}
|
|
42
|
+
else if (props[attr].hasOwnProperty(obj[attr])) {
|
|
43
|
+
copy[attr] = props[attr][obj[attr]];
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
copy[attr] = (0, exports.transform)(obj[attr], props);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
copy[attr] = (0, exports.transform)(obj[attr], props);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
copy[attr] = (0, exports.transform)(obj[attr], props);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return copy;
|
|
58
|
+
}
|
|
59
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
60
|
+
};
|
|
61
|
+
exports.transform = transform;
|
|
62
|
+
const noop = () => undefined;
|
|
63
|
+
const cleanTree = (tree) => {
|
|
64
|
+
return (0, exports.transform)(tree, {
|
|
65
|
+
stmt_len: noop,
|
|
66
|
+
stmt_location: noop,
|
|
67
|
+
location: noop,
|
|
68
|
+
DefElem: (obj) => {
|
|
69
|
+
if (obj.defname === 'as') {
|
|
70
|
+
if (Array.isArray(obj.arg) && obj.arg.length) {
|
|
71
|
+
// function
|
|
72
|
+
obj.arg[0].String.str = obj.arg[0].String.str.trim();
|
|
73
|
+
}
|
|
74
|
+
else if (obj.arg.List && obj.arg.List.items) {
|
|
75
|
+
// function
|
|
76
|
+
obj.arg.List.items[0].String.str = obj.arg.List.items[0].String.str.trim();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// do stmt
|
|
80
|
+
obj.arg.String.str = obj.arg.String.str.trim();
|
|
81
|
+
}
|
|
82
|
+
return (0, exports.cleanTree)(obj);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return (0, exports.cleanTree)(obj);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
exports.cleanTree = cleanTree;
|
|
91
|
+
const cleanTreeWithStmt = (tree) => {
|
|
92
|
+
return (0, exports.transform)(tree, {
|
|
93
|
+
stmt_location: noop,
|
|
94
|
+
location: noop
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
exports.cleanTreeWithStmt = cleanTreeWithStmt;
|