8000 ts strict - getAttribute (#863) · lowgrind/pyscript@c352b50 · GitHub
[go: up one dir, main page]

Skip to content

Commit c352b50

Browse files
Ted Patrickpre-commit-ci[bot]
Ted Patrick
andauthored
ts strict - getAttribute (pyscript#863)
* ts strict getAttribute * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * mark xfail * fix to string * Remove * use getAttribute for theme Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 58b4df6 commit c352b50

File tree

11 files changed

+131
-80
lines changed

11 files changed

+131
-80
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { guidGenerator, addClasses, removeClasses } from '../utils';
1+
import { getAttribute, guidGenerator, addClasses, removeClasses } from '../utils';
22
import type { Runtime } from '../runtime';
33
import { getLogger } from '../logger';
44

@@ -34,7 +34,7 @@ export class BaseEvalElement extends HTMLElement {
3434
}
3535

3636
setOutputMode(defaultMode = "append") {
37-
const mode = this.hasAttribute('output-mode') ? this.getAttribute('output-mode') : defaultMode;
37+
const mode = getAttribute(this,'output-mode') || defaultMode;
3838

3939
switch (mode) {
4040
case "append":
@@ -77,7 +77,7 @@ export class BaseEvalElement extends HTMLElement {
7777
protected async _register_esm(runtime: Runtime): Promise<void> {
7878
const imports: { [key: string]: unknown } = {};
7979
const nodes = document.querySelectorAll("script[type='importmap']");
80-
const importmaps: Array<any> = [];
80+
const importmaps: any[] = [];
8181
nodes.forEach( node =>
8282
{
8383
let importmap;

pyscriptjs/src/components/pybox.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addClasses } from '../utils';
1+
import { getAttribute, addClasses } from '../utils';
22
import { getLogger } from '../logger';
33

44
const logger = getLogger('py-box');
@@ -7,7 +7,7 @@ export class PyBox extends HTMLElement {
77
shadow: ShadowRoot;
88
wrapper: HTMLElement;
99
theme: string;
10-
widths: Array<string>;
10+
widths: string[];
1111

1212
constructor() {
1313
super();
@@ -47,10 +47,14 @@ export class PyBox extends HTMLElement {
4747
// now we need to set widths
4848
this.widths = [];
4949

50-
if (this.hasAttribute('widths')) {
51-
for (const w of this.getAttribute('widths').split(';')) {
52-
if (w.includes('/')) this.widths.push(w.split('/')[0])
53-
else this.widths.push(w)
50+
const widthsAttr = getAttribute( this, "widths" );
51+
if (widthsAttr) {
52+
for (const w of widthsAttr.split(';')) {
53+
if (w.includes('/')){
54+
this.widths.push(w.split('/')[0])
55+
}else{
56+
this.widths.push(w)
57+
}
5458
}
5559
} else {
5660
this.widths = Array<string>(mainDiv.children.length).fill('1 1 0');

pyscriptjs/src/components/pybutton.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import { BaseEvalElement } from './base';
2-
import { addClasses, htmlDecode } from '../utils';
2+
import { getAttribute, addClasses, htmlDecode } from '../utils';
33
import { getLogger } from '../logger'
44
import type { Runtime } from '../runtime';
55

66
const logger = getLogger('py-button');
77

88
export function make_PyButton(runtime: Runtime) {
99
class PyButton extends BaseEvalElement {
10-
widths: Array<string>;
11-
label: string;
12-
class: Array<string>;
13-
defaultClass: Array<string>;
14-
mount_name: string;
10+
widths: string[] = [];
11+
label: string | undefined = undefined;
12+
class: string[];
13+
defaultClass: string[];
14+
mount_name: string | undefined = undefined;
1515
constructor() {
1616
super();
1717

1818
this.defaultClass = ['py-button'];
1919

20-
if (this.hasAttribute('label')) {
21-
this.label = this.getAttribute('label');
20+
const label = getAttribute(this, "label");
21+
if (label) {
22+
this.label = label;
2223
}
2324

2425
// Styling does the same thing as class in normal HTML. Using the name "class" makes the style to malfunction
25-
if (this.hasAttribute('styling')) {
26-
const klass = this.getAttribute('styling').trim();
26+
const styling = getAttribute(this, "styling");
27+
if ( styling ) {
28+
const klass = styling.trim();
2729
if (klass === '') {
2830
this.class = this.defaultClass;
2931
} else {
@@ -40,7 +42,7 @@ export function make_PyButton(runtime: Runtime) {
4042

4143
async connectedCallback() {
4244
this.checkId();
43-
this.code = htmlDecode(this.innerHTML);
45+
this.code = htmlDecode(this.innerHTML) || "";
4446
this.mount_name = this.id.split('-').join('_');
4547
this.innerHTML = '';
4648

pyscriptjs/src/components/pyinputbox.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { BaseEvalElement } from './base';
2-
import { addClasses, htmlDecode } from '../utils';
2+
import { getAttribute, addClasses, htmlDecode } from '../utils';
33
import { getLogger } from '../logger'
44
import type { Runtime } from '../runtime';
55

66
const logger = getLogger('py-inputbox');
77

88
export function make_PyInputBox(runtime: Runtime) {
99
class PyInputBox extends BaseEvalElement {
10-
widths: Array<string>;
11-
label: string;
12-
mount_name: string;
10+
widths: string[] = [];
11+
label: string | undefined = undefined;
12+
mount_name: string | undefined = undefined;
1313
constructor() {
1414
super();
1515

16-
if (this.hasAttribute('label')) {
17-
this.label = this.getAttribute('label');
16+
const label = getAttribute( this, "label");
17+
if (label) {
18+
this.label = label;
1819
}
1920
}
2021

pyscriptjs/src/components/pyloader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getLogger } from '../logger';
44
const logger = getLogger('py-loader');
55

66
export class PyLoader extends BaseEvalElement {
7-
widths: Array<string>;
7+
widths: string[];
88
label: string;
99
mount_name: string;
1010
details: HTMLElement;

pyscriptjs/src/components/pyrepl.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Compartment, StateCommand } from '@codemirror/state';
55
import { keymap } from '@codemirror/view';
66
import { defaultKeymap } from '@codemirror/commands';
77
import { oneDarkTheme } from '@codemirror/theme-one-dark';
8-
import { addClasses, htmlDecode } from '../utils';
8+
import { getAttribute, addClasses, htmlDecode } from '../utils';
99
import { BaseEvalElement } from './base';
1010
import type { Runtime } from '../runtime';
1111
import { getLogger } from '../logger';
@@ -25,7 +25,11 @@ export function make_PyRepl(runtime: Runtime) {
2525

2626
let initialTheme: string;
2727
function getEditorTheme(el: BaseEvalElement): string {
28-
return initialTheme || (initialTheme = el.getAttribute('theme'));
28+
const theme = getAttribute(el, 'theme');
29+
if( !initialTheme && theme){
30+
initialTheme = theme;
31+
}
32+
return initialTheme;
2933
}
3034

3135
class PyRepl extends BaseEvalElement {
@@ -120,26 +124,44 @@ export function make_PyRepl(runtime: Runtime) {
120124
this.setAttribute('root', this.id);
121125
}
122126

123-
if (this.hasAttribute('output')) {
124-
this.errorElement = this.outputElement = document.getElementById(this.getAttribute('output'));
127+
const output = getAttribute(this, "output")
128+
if (output) {
129+
const el = document.getElementById(output);
130+
if(el){
131+
this.errorElement = el;
132+
this.outputElement = el
133+
}
125134
} else {
126-
if (this.hasAttribute('std-out')) {
127-
this.outputElement = document.getElementById(this.getAttribute('std-out'));
135+
const stdOut = getAttribute(this, "std-out");
136+
if (stdOut) {
137+
const el = document.getElementById(stdOut);
138+
if(el){
139+
this.outputElement = el
140+
}
128141
} else {
129142
// In this case neither output or std-out have been provided so we need
130143
// to create a new output div to output to
131144
this.outputElement = document.createElement('div');
132145
this.outputElement.classList.add('output');
133146
this.outputElement.hidden = true;
134-
this.outputElement.id = this.id + '-' + this.getAttribute('exec-id');
147+
const stdOut = getAttribute(this, "exec-id") || "";
148+
this.outputElement.id = this.id + '-' + stdOut;
135149

136150
// add the output div id if there's not output pre-defined
137151
mainDiv.appendChild(this.outputElement);
138152
}
139153

140-
this.errorElement = this.hasAttribute('std-err')
141-
? document.getElementById(this.getAttribute('std-err'))
142-
: this.outputElement;
154+
const stdErr = getAttribute(this, "std-err");
155+
if( stdErr ){
156+
const el = document.getElementById(stdErr);
157+
if(el){
158+
this.errorElement = el;
159+
}else{
160+
this.errorElement = this.outputElement
161+
}
162+
}else{
163+
this.errorElement = this.outputElement
164+
}
143165
}
144166

145167
this.appendChild(mainDiv);
@@ -178,13 +200,15 @@ export function make_PyRepl(runtime: Runtime) {
178200
this.removeAttribute('auto-generate');
179201
}
180202

181-
if(this.hasAttribute('output-mode')) {
182-
newPyRepl.setAttribute('output-mode', this.getAttribute('output-mode'));
203+
const outputMode = getAttribute( this, 'output-mode')
204+
if(outputMode) {
205+
newPyRepl.setAttribute('output-mode', outputMode);
183206
}
184207

185208
const addReplAttribute = (attribute: string) => {
186-
if (this.hasAttribute(attribute)) {
187-
newPyRepl.setAttribute(attribute, this.getAttribute(attribute));
209+
const attr = getAttribute( this, attribute)
210+
if(attr) {
211+
newPyRepl.setAttribute(attribute, attr);
188212
}
189213
};
190214

@@ -193,7 +217,9 @@ export function make_PyRepl(runtime: Runtime) {
193217
addReplAttribute('std-err');
194218

195219
newPyRepl.setAttribute('exec-id', nextExecId.toString());
196-
this.parentElement.appendChild(newPyRepl);
220+
if( this.parentElement ){
221+
this.parentElement.appendChild(newPyRepl);
222+
}
197223
}
198224
}
199225

pyscriptjs/src/components/pyscript.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
addToScriptsQueue,
33
} from '../stores';
44

5-
import { addClasses, htmlDecode } from '../utils';
5+
import { getAttribute, addClasses, htmlDecode } from '../utils';
66
import { BaseEvalElement } from './base';
77
import type { Runtime } from '../runtime';
88
import { getLogger } from '../logger';
@@ -26,16 +26,25 @@ export class PyScript extends BaseEvalElement {
2626
addClasses(mainDiv, ['output']);
2727
// add Editor to main PyScript div
2828

29-
if (this.hasAttribute('output')) {
30-
this.errorElement = this.outputElement = document.getElementById(this< CDA2 /span>.getAttribute('output'));
29+
const output = getAttribute( this, "output");
30+
if (output) {
31+
const el = document.getElementById(output);
32+
if( el ){
33+
this.errorElement = el;
34+
this.outputElement = el;
35+
}
3136

3237
// in this case, the default output-mode is append, if hasn't been specified
3338
if (!this.hasAttribute('output-mode')) {
3439
this.setAttribute('output-mode', 'append');
3540
}
3641
} else {
37-
if (this.hasAttribute('std-out')) {
38-
this.outputElement = document.getElementById(this.getAttribute('std-out'));
42+
const stdOut = getAttribute( this, "std-out");
43+
if (stdOut) {
44+
const el = document.getElementById( stdOut );
45+
if( el){
46+
this.outputElement = el
47+
}
3948
} else {
4049
// In this case neither output or std-out have been provided so we need
4150
// to create a new output div to output to
@@ -49,8 +58,14 @@ export class PyScript extends BaseEvalElement {
4958
mainDiv.appendChild(this.outputElement);
5059
}
5160

52-
if (this.hasAttribute('std-err')) {
53-
this.errorElement = document.getElementById(this.getAttribute('std-err'));
61+
const stdErr = getAttribute( this, "std-err");
62+
if ( stdErr ) {
63+
const el = document.getElementById( stdErr );
64+
if( el ){
65+
this.errorElement = el;
66+
}else{
67+
this.errorElement = this.outputElement;
68+
}
5469
} else {
5570
this.errorElement = this.outputElement;
5671
}

pyscriptjs/src/components/pytitle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BaseEvalElement } from './base';
22
import { addClasses, htmlDecode } from '../utils';
33

44
export class PyTitle extends BaseEvalElement {
5-
widths: Array<string>;
5+
widths: string[];
66
label: string;
77
mount_name: string;
88
constructor() {

pyscriptjs/src/pyconfig.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import toml from '../src/toml'
22
import { getLogger } from './logger';
33
import { version } from './runtime';
4-
import { readTextFromPath, showError } from './utils'
4+
import { getAttribute, readTextFromPath, showError } from './utils'
55

66
const logger = getLogger('py-config');
77

@@ -15,10 +15,10 @@ export interface AppConfig extends Record<string, any> {
1515
author_email?: string;
1616
license?: string;
1717
autoclose_loader?: boolean;
18-
runtimes?: Array<RuntimeConfig>;
19-
packages?: Array<string>;
20-
paths?: Array<string>;
21-
plugins?: Array<string>;
18+
runtimes?: RuntimeConfig[];
19+
packages?: string[];
20+
paths?: string[];
21+
plugins?: string[];
2222
pyscript?: PyScriptMetadata;
2323
}
2424

@@ -63,7 +63,7 @@ export function loadConfigFromElement(el: Element): AppConfig {
6363
inlineConfig = {};
6464
}
65< A189 code>65
else {
66-
const configType: string = el.hasAttribute("type") ? el.getAttribute("type") : "toml";
66+
const configType = getAttribute(el, "type") || "toml";
6767
srcConfig = extractFromSrc(el, configType);
6868
inlineConfig = extractFromInline(el, configType);
6969
}
@@ -77,9 +77,8 @@ export function loadConfigFromElement(el: Element): AppConfig {
7777
}
7878

7979
function extractFromSrc(el: Element, configType: string) {
80-
if (el.hasAttribute('src'))
81-
{
82-
const src = el.getAttribute('src');
80+
const src = getAttribute(el, "src")
81+
if (src) {
8382
logger.info('loading ', src)
8483
return validateConfig(readTextFromPath(src), configType);
8584
}

0 commit comments

Comments
 (0)
0