@coze-editor/extension-json-unnecessary-properties 0.1.0-alpha.0fd19e

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 coze-dev
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.
@@ -0,0 +1,130 @@
1
+ // src/index.ts
2
+ import { FacetCombineStrategy } from "@coze-editor/utils";
3
+ import astField from "@coze-editor/extension-json-ast";
4
+ import {
5
+ Decoration,
6
+ EditorView,
7
+ ViewPlugin
8
+ } from "@codemirror/view";
9
+ import { Facet, Prec } from "@codemirror/state";
10
+
11
+ // src/find.ts
12
+ function findUnnecessaryProperties(ast, checker) {
13
+ const nodes = [];
14
+ visit(ast, (node) => {
15
+ if (node.type === "property") {
16
+ const key = node.keyNode.value;
17
+ if (checker({
18
+ paths: [...getParentKeys(node), key]
19
+ })) {
20
+ nodes.push(node);
21
+ }
22
+ }
23
+ return true;
24
+ });
25
+ const ranges = [];
26
+ nodes.forEach((node) => {
27
+ const nodeRange = {
28
+ from: node.offset,
29
+ to: node.offset + node.length
30
+ };
31
+ if (ranges.some((r) => nodeRange.from >= r.from && nodeRange.to <= r.to)) {
32
+ return;
33
+ }
34
+ ranges.push(nodeRange);
35
+ });
36
+ return ranges;
37
+ }
38
+ function getParentKeys(node) {
39
+ const paths = [];
40
+ let current = node;
41
+ let target = node.parent;
42
+ while (target) {
43
+ if (target.type === "property") {
44
+ paths.unshift(target.keyNode.value);
45
+ } else if (target.type === "array") {
46
+ paths.unshift(target.items.indexOf(current));
47
+ }
48
+ current = target;
49
+ target = target.parent;
50
+ }
51
+ return paths;
52
+ }
53
+ function visit(root, visitor) {
54
+ const doVisit = (node) => {
55
+ let ctn = visitor(node);
56
+ const { children } = node;
57
+ if (Array.isArray(children)) {
58
+ for (let i = 0; i < children.length && ctn; i++) {
59
+ ctn = doVisit(children[i]);
60
+ }
61
+ }
62
+ return ctn;
63
+ };
64
+ doVisit(root);
65
+ }
66
+
67
+ // src/index.ts
68
+ var facet = Facet.define({
69
+ combine: FacetCombineStrategy.Last
70
+ });
71
+ var plugin = ViewPlugin.fromClass(
72
+ class {
73
+ decorations = Decoration.none;
74
+ constructor(view) {
75
+ this.decorations = this._getDecorations(view.state);
76
+ }
77
+ update(update) {
78
+ if (update.docChanged) {
79
+ this.decorations = this._getDecorations(update.state);
80
+ }
81
+ }
82
+ _getDecorations(state) {
83
+ const ast = state.field(astField);
84
+ if (!ast) {
85
+ return Decoration.none;
86
+ }
87
+ const propertyRanges = findUnnecessaryProperties(
88
+ ast,
89
+ state.facet(facet).isUnnecessary ?? (() => false)
90
+ );
91
+ return Decoration.set(
92
+ propertyRanges.map(
93
+ (range) => Decoration.mark({
94
+ class: "json-unnecessary-property"
95
+ }).range(range.from, range.to)
96
+ ),
97
+ true
98
+ );
99
+ }
100
+ },
101
+ {
102
+ provide(plugin2) {
103
+ return EditorView.decorations.of(
104
+ (view) => {
105
+ var _a;
106
+ return ((_a = view.plugin(plugin2)) == null ? void 0 : _a.decorations) ?? Decoration.none;
107
+ }
108
+ );
109
+ }
110
+ }
111
+ );
112
+ var jsonUnnecessaryProperties = (isUnnecessary) => [
113
+ astField,
114
+ facet.of({
115
+ isUnnecessary
116
+ }),
117
+ plugin,
118
+ Prec.low(
119
+ EditorView.theme({
120
+ ".json-unnecessary-property": {
121
+ opacity: "0.5"
122
+ }
123
+ })
124
+ )
125
+ ];
126
+ var index_default = jsonUnnecessaryProperties;
127
+ export {
128
+ index_default as default
129
+ };
130
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/find.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport { FacetCombineStrategy } from '@coze-editor/utils';\nimport astField from '@coze-editor/extension-json-ast';\nimport {\n Decoration,\n type DecorationSet,\n EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from '@codemirror/view';\nimport { type EditorState, Facet, Prec } from '@codemirror/state';\n\nimport { type JSONPropertyChecker } from './types';\nimport { findUnnecessaryProperties } from './find';\n\nconst facet = Facet.define({\n combine: FacetCombineStrategy.Last<{\n isUnnecessary: JSONPropertyChecker;\n }>,\n});\n\nconst plugin = ViewPlugin.fromClass(\n class {\n decorations: DecorationSet = Decoration.none;\n\n constructor(view: EditorView) {\n this.decorations = this._getDecorations(view.state);\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged) {\n this.decorations = this._getDecorations(update.state);\n }\n }\n\n _getDecorations(state: EditorState) {\n const ast = state.field(astField);\n\n if (!ast) {\n return Decoration.none;\n }\n\n const propertyRanges = findUnnecessaryProperties(\n ast,\n state.facet(facet).isUnnecessary ?? (() => false),\n );\n\n return Decoration.set(\n propertyRanges.map(range =>\n Decoration.mark({\n class: 'json-unnecessary-property',\n }).range(range.from, range.to),\n ),\n true,\n );\n }\n },\n {\n provide(plugin) {\n return EditorView.decorations.of(\n view => view.plugin(plugin)?.decorations ?? Decoration.none,\n );\n },\n },\n);\n\nconst jsonUnnecessaryProperties = (isUnnecessary: JSONPropertyChecker) => [\n astField,\n facet.of({\n isUnnecessary,\n }),\n plugin,\n Prec.low(\n EditorView.theme({\n '.json-unnecessary-property': {\n opacity: '0.5',\n },\n }),\n ),\n];\n\nexport default jsonUnnecessaryProperties;\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport type { ASTNode } from '@coze-editor/extension-json-ast';\n\nimport { type JSONPropertyChecker } from './types';\n\ninterface Range {\n from: number;\n to: number;\n}\n\nfunction findUnnecessaryProperties(\n ast: ASTNode,\n checker: JSONPropertyChecker,\n): Range[] {\n const nodes: ASTNode[] = [];\n\n visit(ast, node => {\n if (node.type === 'property') {\n const key = node.keyNode.value;\n\n if (\n checker({\n paths: [...getParentKeys(node), key],\n })\n ) {\n nodes.push(node);\n }\n }\n\n return true;\n });\n\n // remove already included ranges\n // WHY: two nested `opacity: 0.5` will become `opacity: 0.25`\n const ranges: Range[] = [];\n nodes.forEach(node => {\n const nodeRange = {\n from: node.offset,\n to: node.offset + node.length,\n };\n\n if (ranges.some(r => nodeRange.from >= r.from && nodeRange.to <= r.to)) {\n return;\n }\n\n ranges.push(nodeRange);\n });\n\n return ranges;\n}\n\nfunction getParentKeys(node: ASTNode): (string | number)[] {\n const paths: (string | number)[] = [];\n\n let current: ASTNode = node;\n let target: ASTNode | undefined = node.parent;\n\n while (target) {\n if (target.type === 'property') {\n paths.unshift(target.keyNode.value);\n } else if (target.type === 'array') {\n paths.unshift(target.items.indexOf(current));\n }\n\n current = target;\n target = target.parent;\n }\n return paths;\n}\n\nfunction visit(root: ASTNode, visitor: (node: ASTNode) => boolean) {\n const doVisit = (node: ASTNode): boolean => {\n let ctn = visitor(node);\n const { children } = node;\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length && ctn; i++) {\n ctn = doVisit(children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A,MAAM,GAAG;AAAA,IACP;AAAA,EACF,CAAC;AAAA,EACD;AAAA,EACA,KAAK;AAAA,IACH,WAAW,MAAM;AAAA,MACf,8BAA8B;AAAA,QAC5B,SAAS;AAAA,MACX;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEA,IAAO,gBAAQ;","names":["plugin"]}
@@ -0,0 +1,11 @@
1
+ import * as _coze_editor_extension_json_ast from '@coze-editor/extension-json-ast';
2
+ import * as _codemirror_state from '@codemirror/state';
3
+
4
+ type JSONPath = string | number;
5
+ type JSONPropertyChecker = (e: {
6
+ paths: JSONPath[];
7
+ }) => boolean;
8
+
9
+ declare const jsonUnnecessaryProperties: (isUnnecessary: JSONPropertyChecker) => (_codemirror_state.Extension | _codemirror_state.StateField<_coze_editor_extension_json_ast.ASTNode>)[];
10
+
11
+ export { jsonUnnecessaryProperties as default };
@@ -0,0 +1,11 @@
1
+ import * as _coze_editor_extension_json_ast from '@coze-editor/extension-json-ast';
2
+ import * as _codemirror_state from '@codemirror/state';
3
+
4
+ type JSONPath = string | number;
5
+ type JSONPropertyChecker = (e: {
6
+ paths: JSONPath[];
7
+ }) => boolean;
8
+
9
+ declare const jsonUnnecessaryProperties: (isUnnecessary: JSONPropertyChecker) => (_codemirror_state.Extension | _codemirror_state.StateField<_coze_editor_extension_json_ast.ASTNode>)[];
10
+
11
+ export { jsonUnnecessaryProperties as default };
package/dist/index.js ADDED
@@ -0,0 +1,156 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ default: () => index_default
33
+ });
34
+ module.exports = __toCommonJS(index_exports);
35
+ var import_utils = require("@coze-editor/utils");
36
+ var import_extension_json_ast = __toESM(require("@coze-editor/extension-json-ast"));
37
+ var import_view = require("@codemirror/view");
38
+ var import_state = require("@codemirror/state");
39
+
40
+ // src/find.ts
41
+ function findUnnecessaryProperties(ast, checker) {
42
+ const nodes = [];
43
+ visit(ast, (node) => {
44
+ if (node.type === "property") {
45
+ const key = node.keyNode.value;
46
+ if (checker({
47
+ paths: [...getParentKeys(node), key]
48
+ })) {
49
+ nodes.push(node);
50
+ }
51
+ }
52
+ return true;
53
+ });
54
+ const ranges = [];
55
+ nodes.forEach((node) => {
56
+ const nodeRange = {
57
+ from: node.offset,
58
+ to: node.offset + node.length
59
+ };
60
+ if (ranges.some((r) => nodeRange.from >= r.from && nodeRange.to <= r.to)) {
61
+ return;
62
+ }
63
+ ranges.push(nodeRange);
64
+ });
65
+ return ranges;
66
+ }
67
+ function getParentKeys(node) {
68
+ const paths = [];
69
+ let current = node;
70
+ let target = node.parent;
71
+ while (target) {
72
+ if (target.type === "property") {
73
+ paths.unshift(target.keyNode.value);
74
+ } else if (target.type === "array") {
75
+ paths.unshift(target.items.indexOf(current));
76
+ }
77
+ current = target;
78
+ target = target.parent;
79
+ }
80
+ return paths;
81
+ }
82
+ function visit(root, visitor) {
83
+ const doVisit = (node) => {
84
+ let ctn = visitor(node);
85
+ const { children } = node;
86
+ if (Array.isArray(children)) {
87
+ for (let i = 0; i < children.length && ctn; i++) {
88
+ ctn = doVisit(children[i]);
89
+ }
90
+ }
91
+ return ctn;
92
+ };
93
+ doVisit(root);
94
+ }
95
+
96
+ // src/index.ts
97
+ var facet = import_state.Facet.define({
98
+ combine: import_utils.FacetCombineStrategy.Last
99
+ });
100
+ var plugin = import_view.ViewPlugin.fromClass(
101
+ class {
102
+ decorations = import_view.Decoration.none;
103
+ constructor(view) {
104
+ this.decorations = this._getDecorations(view.state);
105
+ }
106
+ update(update) {
107
+ if (update.docChanged) {
108
+ this.decorations = this._getDecorations(update.state);
109
+ }
110
+ }
111
+ _getDecorations(state) {
112
+ const ast = state.field(import_extension_json_ast.default);
113
+ if (!ast) {
114
+ return import_view.Decoration.none;
115
+ }
116
+ const propertyRanges = findUnnecessaryProperties(
117
+ ast,
118
+ state.facet(facet).isUnnecessary ?? (() => false)
119
+ );
120
+ return import_view.Decoration.set(
121
+ propertyRanges.map(
122
+ (range) => import_view.Decoration.mark({
123
+ class: "json-unnecessary-property"
124
+ }).range(range.from, range.to)
125
+ ),
126
+ true
127
+ );
128
+ }
129
+ },
130
+ {
131
+ provide(plugin2) {
132
+ return import_view.EditorView.decorations.of(
133
+ (view) => {
134
+ var _a;
135
+ return ((_a = view.plugin(plugin2)) == null ? void 0 : _a.decorations) ?? import_view.Decoration.none;
136
+ }
137
+ );
138
+ }
139
+ }
140
+ );
141
+ var jsonUnnecessaryProperties = (isUnnecessary) => [
142
+ import_extension_json_ast.default,
143
+ facet.of({
144
+ isUnnecessary
145
+ }),
146
+ plugin,
147
+ import_state.Prec.low(
148
+ import_view.EditorView.theme({
149
+ ".json-unnecessary-property": {
150
+ opacity: "0.5"
151
+ }
152
+ })
153
+ )
154
+ ];
155
+ var index_default = jsonUnnecessaryProperties;
156
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/find.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport { FacetCombineStrategy } from '@coze-editor/utils';\nimport astField from '@coze-editor/extension-json-ast';\nimport {\n Decoration,\n type DecorationSet,\n EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from '@codemirror/view';\nimport { type EditorState, Facet, Prec } from '@codemirror/state';\n\nimport { type JSONPropertyChecker } from './types';\nimport { findUnnecessaryProperties } from './find';\n\nconst facet = Facet.define({\n combine: FacetCombineStrategy.Last<{\n isUnnecessary: JSONPropertyChecker;\n }>,\n});\n\nconst plugin = ViewPlugin.fromClass(\n class {\n decorations: DecorationSet = Decoration.none;\n\n constructor(view: EditorView) {\n this.decorations = this._getDecorations(view.state);\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged) {\n this.decorations = this._getDecorations(update.state);\n }\n }\n\n _getDecorations(state: EditorState) {\n const ast = state.field(astField);\n\n if (!ast) {\n return Decoration.none;\n }\n\n const propertyRanges = findUnnecessaryProperties(\n ast,\n state.facet(facet).isUnnecessary ?? (() => false),\n );\n\n return Decoration.set(\n propertyRanges.map(range =>\n Decoration.mark({\n class: 'json-unnecessary-property',\n }).range(range.from, range.to),\n ),\n true,\n );\n }\n },\n {\n provide(plugin) {\n return EditorView.decorations.of(\n view => view.plugin(plugin)?.decorations ?? Decoration.none,\n );\n },\n },\n);\n\nconst jsonUnnecessaryProperties = (isUnnecessary: JSONPropertyChecker) => [\n astField,\n facet.of({\n isUnnecessary,\n }),\n plugin,\n Prec.low(\n EditorView.theme({\n '.json-unnecessary-property': {\n opacity: '0.5',\n },\n }),\n ),\n];\n\nexport default jsonUnnecessaryProperties;\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport type { ASTNode } from '@coze-editor/extension-json-ast';\n\nimport { type JSONPropertyChecker } from './types';\n\ninterface Range {\n from: number;\n to: number;\n}\n\nfunction findUnnecessaryProperties(\n ast: ASTNode,\n checker: JSONPropertyChecker,\n): Range[] {\n const nodes: ASTNode[] = [];\n\n visit(ast, node => {\n if (node.type === 'property') {\n const key = node.keyNode.value;\n\n if (\n checker({\n paths: [...getParentKeys(node), key],\n })\n ) {\n nodes.push(node);\n }\n }\n\n return true;\n });\n\n // remove already included ranges\n // WHY: two nested `opacity: 0.5` will become `opacity: 0.25`\n const ranges: Range[] = [];\n nodes.forEach(node => {\n const nodeRange = {\n from: node.offset,\n to: node.offset + node.length,\n };\n\n if (ranges.some(r => nodeRange.from >= r.from && nodeRange.to <= r.to)) {\n return;\n }\n\n ranges.push(nodeRange);\n });\n\n return ranges;\n}\n\nfunction getParentKeys(node: ASTNode): (string | number)[] {\n const paths: (string | number)[] = [];\n\n let current: ASTNode = node;\n let target: ASTNode | undefined = node.parent;\n\n while (target) {\n if (target.type === 'property') {\n paths.unshift(target.keyNode.value);\n } else if (target.type === 'array') {\n paths.unshift(target.items.indexOf(current));\n }\n\n current = target;\n target = target.parent;\n }\n return paths;\n}\n\nfunction visit(root: ASTNode, visitor: (node: ASTNode) => boolean) {\n const doVisit = (node: ASTNode): boolean => {\n let ctn = visitor(node);\n const { children } = node;\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length && ctn; i++) {\n ctn = doVisit(children[i]);\n }\n }\n return ctn;\n };\n\n doVisit(root);\n}\n\nexport { findUnnecessaryProperties };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAAqC;AACrC,gCAAqB;AACrB,kBAMO;AACP,mBAA8C;;;ACA9C,SAAS,0BACP,KACA,SACS;AACT,QAAM,QAAmB,CAAC;AAE1B,QAAM,KAAK,UAAQ;AACjB,QAAI,KAAK,SAAS,YAAY;AAC5B,Y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properties",
3
+ "version": "0.1.0-alpha.0fd19e",
4
+ "description": "extension-json-unnecessary-properties",
5
+ "license": "MIT",
6
+ "author": "fengzilong",
7
+ "maintainers": [],
8
+ "sideEffects": [
9
+ "**/*.css",
10
+ "**/*.less",
11
+ "**/*.sass",
12
+ "**/*.scss"
13
+ ],
14
+ "main": "./dist/esm/index.js",
15
+ "module": "./dist/esm/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "lint": "eslint && tsc --noEmit"
23
+ },
24
+ "dependencies": {
25
+ "@coze-editor/extension-json-ast": "0.1.0-alpha.0fd19e",
26
+ "@coze-editor/utils": "0.1.0-alpha.0fd19e"
27
+ },
28
+ "devDependencies": {
29
+ "@codemirror/state": "^6.4.1",
30
+ "@codemirror/view": "^6.26.1",
31
+ "@coze-arch/ts-config": "workspace:*",
32
+ "@coze-editor/eslint-config": "workspace:*",
33
+ "@types/node": "^22",
34
+ "eslint": "9.14.0",
35
+ "tsup": "^8.0.1",
36
+ "typescript": "^5.8.2"
37
+ },
38
+ "peerDependencies": {
39
+ "@codemirror/state": "^6.4.1",
40
+ "@codemirror/view": "^6.26.1"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "registry": "https://registry.npmjs.org"
45
+ },
46
+ "test:main": "./src/index.ts"
47
+ }