Skip to content

Commit

Permalink
Enable the import/no-cycle ESLint plugin rule
Browse files Browse the repository at this point in the history
Having cyclical imports is obviously not a good idea, and this ESLint plugin rule can help detect those; please see https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md
  • Loading branch information
Snuffleupagus committed Jun 4, 2023
1 parent 605d9f4 commit cf3a35e
Show file tree
Hide file tree
Showing 22 changed files with 328 additions and 213 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -37,6 +37,7 @@
"import/extensions": ["error", "always", { "ignorePackages": true, }],
"import/first": "error",
"import/named": "error",
"import/no-cycle": "error",
"import/no-empty-named-blocks": "error",
"import/no-mutable-exports": "error",
"import/no-self-import": "error",
Expand Down
6 changes: 2 additions & 4 deletions src/core/xfa/bind.js
Expand Up @@ -36,12 +36,10 @@ import {
$removeChild,
$setValue,
$text,
XFAAttribute,
XFAObjectArray,
XmlObject,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { BindItems, Field, Items, SetProperty, Text } from "./template.js";
import { createDataNode, searchNode } from "./som.js";
import { XFAAttribute, XFAObjectArray, XmlObject } from "./xfa_object.js";
import { NamespaceIds } from "./namespaces.js";
import { warn } from "../../shared/util.js";

Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/builder.js
Expand Up @@ -23,12 +23,12 @@ import {
$onChild,
$resolvePrototypes,
$root,
XFAObject,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { NamespaceSetUp } from "./setup.js";
import { Template } from "./template.js";
import { UnknownNamespace } from "./unknown.js";
import { warn } from "../../shared/util.js";
import { XFAObject } from "./xfa_object.js";

class Root extends XFAObject {
constructor(ids) {
Expand Down
3 changes: 1 addition & 2 deletions src/core/xfa/config.js
Expand Up @@ -14,9 +14,8 @@
*/

import { $buildXFAObject, NamespaceIds } from "./namespaces.js";
import { $content, $finalize } from "./symbol_utils.js";
import {
$content,
$finalize,
ContentObject,
IntegerObject,
Option01,
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/data.js
Expand Up @@ -20,7 +20,7 @@ import {
$setValue,
$toString,
$uid,
} from "./xfa_object.js";
} from "./symbol_utils.js";

class DataHandler {
constructor(root, data) {
Expand Down
5 changes: 2 additions & 3 deletions src/core/xfa/datasets.js
Expand Up @@ -19,10 +19,9 @@ import {
$namespaceId,
$nodeName,
$onChild,
XFAObject,
XmlObject,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { $buildXFAObject, NamespaceIds } from "./namespaces.js";
import { XFAObject, XmlObject } from "./xfa_object.js";

const DATASETS_NS_ID = NamespaceIds.datasets.id;

Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/factory.js
Expand Up @@ -20,7 +20,7 @@ import {
$text,
$toHTML,
$toPages,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { Binder } from "./bind.js";
import { DataHandler } from "./data.js";
import { FontFinder } from "./fonts.js";
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/fonts.js
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { $globalData } from "./xfa_object.js";
import { $globalData } from "./symbol_utils.js";
import { stripQuotes } from "./utils.js";
import { warn } from "../../shared/util.js";

Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/html_utils.js
Expand Up @@ -24,12 +24,12 @@ import {
$pushGlyphs,
$text,
$toStyle,
XFAObject,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { createValidAbsoluteUrl, warn } from "../../shared/util.js";
import { getMeasurement, stripQuotes } from "./utils.js";
import { selectFont } from "./fonts.js";
import { TextMeasure } from "./text.js";
import { XFAObject } from "./xfa_object.js";

function measureToString(m) {
if (typeof m === "string") {
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/layout.js
Expand Up @@ -20,7 +20,7 @@ import {
$getTemplateRoot,
$isSplittable,
$isThereMoreWidth,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { measureToString } from "./html_utils.js";

// Subform and ExclGroup have a layout so they share these functions.
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/parser.js
Expand Up @@ -24,7 +24,7 @@ import {
$onChild,
$onText,
$setId,
} from "./xfa_object.js";
} from "./symbol_utils.js";
import { XMLParserBase, XMLParserErrorCode } from "../xml_parser.js";
import { Builder } from "./builder.js";
import { warn } from "../../shared/util.js";
Expand Down
37 changes: 8 additions & 29 deletions src/core/xfa/som.js
Expand Up @@ -14,17 +14,11 @@
*/

import {
$appendChild,
$getChildren,
$getChildrenByClass,
$getChildrenByName,
$getParent,
$namespaceId,
XFAObject,
XFAObjectArray,
XmlObject,
} from "./xfa_object.js";
import { NamespaceIds } from "./namespaces.js";
} from "./symbol_utils.js";
import { warn } from "../../shared/util.js";

const namePattern = /^[^.[]+/;
Expand Down Expand Up @@ -58,7 +52,6 @@ const shortcuts = new Map([
]);

const somCache = new WeakMap();
const NS_DATASETS = NamespaceIds.datasets.id;

function parseIndex(index) {
index = index.trim();
Expand Down Expand Up @@ -193,7 +186,7 @@ function searchNode(
const { name, cacheName, operator, index } = parsed[i];
const nodes = [];
for (const node of root) {
if (!(node instanceof XFAObject)) {
if (!node.isXFAObject) {
continue;
}

Expand All @@ -218,7 +211,7 @@ function searchNode(
break;
case operators.dotHash:
children = node[$getChildrenByClass](name);
if (children instanceof XFAObjectArray) {
if (children.isXFAObjectArray) {
children = children.children;
} else {
children = [children];
Expand Down Expand Up @@ -265,20 +258,6 @@ function searchNode(
return root;
}

function createNodes(root, path) {
let node = null;
for (const { name, index } of path) {
for (let i = 0, ii = !isFinite(index) ? 0 : index; i <= ii; i++) {
const nsId = root[$namespaceId] === NS_DATASETS ? -1 : root[$namespaceId];
node = new XmlObject(nsId, name);
root[$appendChild](node);
}

root = node;
}
return node;
}

function createDataNode(root, container, expr) {
const parsed = parseExpression(expr);
if (!parsed) {
Expand All @@ -302,7 +281,7 @@ function createDataNode(root, container, expr) {
const { name, operator, index } = parsed[i];
if (!isFinite(index)) {
parsed[i].index = 0;
return createNodes(root, parsed.slice(i));
return root.createNodes(parsed.slice(i));
}

let children;
Expand All @@ -315,7 +294,7 @@ function createDataNode(root, container, expr) {
break;
case operators.dotHash:
children = root[$getChildrenByClass](name);
if (children instanceof XFAObjectArray) {
if (children.isXFAObjectArray) {
children = children.children;
} else {
children = [children];
Expand All @@ -326,19 +305,19 @@ function createDataNode(root, container, expr) {
}

if (children.length === 0) {
return createNodes(root, parsed.slice(i));
return root.createNodes(parsed.slice(i));
}

if (index < children.length) {
const child = children[index];
if (!(child instanceof XFAObject)) {
if (!child.isXFAObject) {
warn(`XFA - Cannot create a node.`);
return null;
}
root = child;
} else {
parsed[i].index = index - children.length;
return createNodes(root, parsed.slice(i));
return root.createNodes(parsed.slice(i));
}
}
return null;
Expand Down
156 changes: 156 additions & 0 deletions src/core/xfa/symbol_utils.js
@@ -0,0 +1,156 @@
/* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// We use these symbols to avoid name conflict between tags
// and properties/methods names.
const $acceptWhitespace = Symbol();
const $addHTML = Symbol();
const $appendChild = Symbol();
const $childrenToHTML = Symbol();
const $clean = Symbol();
const $cleanPage = Symbol();
const $cleanup = Symbol();
const $clone = Symbol();
const $consumed = Symbol();
const $content = Symbol("content");
const $data = Symbol("data");
const $dump = Symbol();
const $extra = Symbol("extra");
const $finalize = Symbol();
const $flushHTML = Symbol();
const $getAttributeIt = Symbol();
const $getAttributes = Symbol();
const $getAvailableSpace = Symbol();
const $getChildrenByClass = Symbol();
const $getChildrenByName = Symbol();
const $getChildrenByNameIt = Symbol();
const $getDataValue = Symbol();
const $getExtra = Symbol();
const $getRealChildrenByNameIt = Symbol();
const $getChildren = Symbol();
const $getContainedChildren = Symbol();
const $getNextPage = Symbol();
const $getSubformParent = Symbol();
const $getParent = Symbol();
const $getTemplateRoot = Symbol();
const $globalData = Symbol();
const $hasSettableValue = Symbol();
const $ids = Symbol();
const $indexOf = Symbol();
const $insertAt = Symbol();
const $isCDATAXml = Symbol();
const $isBindable = Symbol();
const $isDataValue = Symbol();
const $isDescendent = Symbol();
const $isNsAgnostic = Symbol();
const $isSplittable = Symbol();
const $isThereMoreWidth = Symbol();
const $isTransparent = Symbol();
const $isUsable = Symbol();
const $lastAttribute = Symbol();
const $namespaceId = Symbol("namespaceId");
const $nodeName = Symbol("nodeName");
const $nsAttributes = Symbol();
const $onChild = Symbol();
const $onChildCheck = Symbol();
const $onText = Symbol();
const $pushGlyphs = Symbol();
const $popPara = Symbol();
const $pushPara = Symbol();
const $removeChild = Symbol();
const $root = Symbol("root");
const $resolvePrototypes = Symbol();
const $searchNode = Symbol();
const $setId = Symbol();
const $setSetAttributes = Symbol();
const $setValue = Symbol();
const $tabIndex = Symbol();
const $text = Symbol();
const $toPages = Symbol();
const $toHTML = Symbol();
const $toString = Symbol();
const $toStyle = Symbol();
const $uid = Symbol("uid");

export {
$acceptWhitespace,
$addHTML,
$appendChild,
$childrenToHTML,
$clean,
$cleanPage,
$cleanup,
$clone,
$consumed,
$content,
$data,
$dump,
$extra,
$finalize,
$flushHTML,
$getAttributeIt,
$getAttributes,
$getAvailableSpace,
$getChildren,
$getChildrenByClass,
$getChildrenByName,
$getChildrenByNameIt,
$getContainedChildren,
$getDataValue,
$getExtra,
$getNextPage,
$getParent,
$getRealChildrenByNameIt,
$getSubformParent,
$getTemplateRoot,
$globalData,
$hasSettableValue,
$ids,
$indexOf,
$insertAt,
$isBindable,
$isCDATAXml,
$isDataValue,
$isDescendent,
$isNsAgnostic,
$isSplittable,
$isThereMoreWidth,
$isTransparent,
$isUsable,
$lastAttribute,
$namespaceId,
$nodeName,
$nsAttributes,
$onChild,
$onChildCheck,
$onText,
$popPara,
$pushGlyphs,
$pushPara,
$removeChild,
$resolvePrototypes,
$root,
$searchNode,
$setId,
$setSetAttributes,
$setValue,
$tabIndex,
$text,
$toHTML,
$toPages,
$toString,
$toStyle,
$uid,
};

0 comments on commit cf3a35e

Please sign in to comment.