From 0e6c3cf9450a123a0b792c283ebbf494e11bdeda Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Fri, 15 Nov 2019 22:50:11 -0600 Subject: [PATCH 1/2] Fix typescript build --- .gitignore | 3 ++- package.json | 12 ++++++++---- src/mutation-summary.ts | 19 ++++++++++--------- tests/test.ts | 4 ++-- tsconfig.json | 16 ++++++++++++++++ typings/extensions.d.ts | 10 ++++++++++ util/tree-mirror.ts | 2 +- 7 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 tsconfig.json create mode 100644 typings/extensions.d.ts diff --git a/.gitignore b/.gitignore index 50ea277..fd05dea 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea *.js.map node_modules -.project \ No newline at end of file +.project +dist diff --git a/package.json b/package.json index 3029ee6..8007974 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,15 @@ { "name": "mutation-summary", - "version": "0.0.0", + "version": "1.0.0", "description": "Makes observing the DOM fast and easy", - "main": "src/mutation-summary.js", + "main": "dist/mutation-summary.js", + "types": "dist/mutation-summary.d.ts", "directories": { "example": "examples", "test": "tests" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "prepublish": "tsc" }, "repository": { "type": "git", @@ -19,7 +20,10 @@ "devDependencies": { "chai": "*", "mocha": "*" - } + }, + "files": [ + "dist" + ] } diff --git a/src/mutation-summary.ts b/src/mutation-summary.ts index ee3a268..31421f5 100644 --- a/src/mutation-summary.ts +++ b/src/mutation-summary.ts @@ -421,7 +421,7 @@ class MutationProjection { this.processChildlistChanges(); - var parentNode = node.parentNode; + var parentNode = node.parentNode as Node; var nodeChange = this.treeChanges.get(node); if (nodeChange && nodeChange.oldParentNode) parentNode = nodeChange.oldParentNode; @@ -487,7 +487,7 @@ class MutationProjection { } getOldPreviousSibling(node:Node):Node { - var parentNode = node.parentNode; + var parentNode = node.parentNode as Node; var nodeChange = this.treeChanges.get(node); if (nodeChange && nodeChange.oldParentNode) parentNode = nodeChange.oldParentNode; @@ -613,7 +613,7 @@ class MutationProjection { return result; } - matchabilityChange(node:Node) { + matchabilityChange(node:Node): Movement { // TODO(rafaelw): Include PI, CDATA? // Only include text nodes. if (this.characterDataOnly) { @@ -697,7 +697,7 @@ class MutationProjection { var oldPrevious = mutation.previousSibling; - function recordOldPrevious(node:Node, previous:Node) { + const recordOldPrevious = (node:Node, previous:Node) => { if (!node || change.oldPrevious.has(node) || change.added.has(node) || @@ -746,7 +746,7 @@ class MutationProjection { this.processChildlistChanges(); - var parentNode = node.parentNode; + var parentNode = node.parentNode as Node; var nodeChange = this.treeChanges.get(node); if (nodeChange && nodeChange.oldParentNode) parentNode = nodeChange.oldParentNode; @@ -825,7 +825,7 @@ class MutationProjection { } } -class Summary { +export class Summary { public added:Node[]; public removed:Node[]; public reparented:Node[]; @@ -940,6 +940,7 @@ class Qualifier { if ('attrValue' in this) return '[' + this.attrName + '=' + escapeQuotes(this.attrValue) + ']'; + //@ts-ignore return '[' + this.attrName + ']'; } } @@ -1443,7 +1444,7 @@ function elementFilterAttributes(selectors:Selector[]):string[] { return Object.keys(attributes); } -interface Query { +export interface Query { element?:string; attribute?:string; all?:boolean; @@ -1453,7 +1454,7 @@ interface Query { elementFilter?:Selector[]; } -interface Options { +export interface Options { callback:(summaries:Summary[]) => any; queries: Query[]; rootNode?:Node; @@ -1461,7 +1462,7 @@ interface Options { observeOwnChanges?:boolean; } -class MutationSummary { +export class MutationSummary { public static NodeMap = NodeMap; // exposed for use in TreeMirror. public static parseElementFilter = Selector.parseSelectors; // exposed for testing. diff --git a/tests/test.ts b/tests/test.ts index 89ee95c..512e562 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -219,7 +219,7 @@ suite('Mutation Summary', function() { }); test('Attribute -- Array proto changed', function() { - Array.prototype.foo = 'bar'; + (Array.prototype as any).foo = 'bar'; var div = document.createElement('div'); testDiv.appendChild(div); @@ -259,7 +259,7 @@ suite('Mutation Summary', function() { div2.removeAttribute('foo'); div2.setAttribute('foo', 'baz2'); assertNothingReported(); - delete Array.prototype.foo; + delete (Array.prototype as any).foo; }); test('Attribute Case Insensitive', function() { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..53b9c2a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "declaration": true, + "outDir": "dist", + "strict": false, + "baseUrl": "./src", + "typeRoots": [ "typings" ], + "esModuleInterop": true, + }, + "include": [ + "src/**/*.ts", + "typings/*.ts" + ] +} diff --git a/typings/extensions.d.ts b/typings/extensions.d.ts new file mode 100644 index 0000000..e9d4e6f --- /dev/null +++ b/typings/extensions.d.ts @@ -0,0 +1,10 @@ +declare global { + var WebKitMutationObserver: { + prototype: MutationObserver; + new(callback: MutationCallback): MutationObserver; + }; + + type WebKitMutationObserver = MutationObserver; +} + +export { }; diff --git a/util/tree-mirror.ts b/util/tree-mirror.ts index ff75412..99b8c95 100644 --- a/util/tree-mirror.ts +++ b/util/tree-mirror.ts @@ -189,7 +189,7 @@ class TreeMirrorClient { var self = this; - var queries = [{ all: true }]; + var queries = [{ all: true }] as Query[]; if (testingQueries) queries = queries.concat(testingQueries); From 40b7c2b723ce40dc258c23767fa535826e202b66 Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Fri, 15 Nov 2019 22:52:13 -0600 Subject: [PATCH 2/2] Add typescript to dev dependencies --- package.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8007974..b092e7a 100644 --- a/package.json +++ b/package.json @@ -19,16 +19,10 @@ "license": "Apache 2.0", "devDependencies": { "chai": "*", - "mocha": "*" + "mocha": "*", + "typescript": "^3.7.2" }, "files": [ "dist" ] } - - - - - - -