pxt-core 7.5.44 → 7.5.45

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/built/pxt.js CHANGED
@@ -140422,11 +140422,19 @@ var pxt;
140422
140422
  return pxt.U.lookup(internalApis, name) || pxt.U.lookup(externalApis, name);
140423
140423
  }
140424
140424
  function lookupGlobalSymbol(name) {
140425
+ var _a;
140425
140426
  if (!name)
140426
140427
  return undefined;
140427
140428
  let sym = lookupApi(name);
140428
140429
  if (sym)
140429
140430
  getOrSetSymbolType(sym);
140431
+ else if (name.indexOf(".")) {
140432
+ const base = name.substring(0, name.lastIndexOf("."));
140433
+ const baseSymbol = lookupGlobalSymbol(base);
140434
+ if ((baseSymbol === null || baseSymbol === void 0 ? void 0 : baseSymbol.kind) === 8 /* Class */ && ((_a = baseSymbol.extendsTypes) === null || _a === void 0 ? void 0 : _a.length)) {
140435
+ return lookupGlobalSymbol(baseSymbol.extendsTypes[0] + name.substring(base.length));
140436
+ }
140437
+ }
140430
140438
  return sym;
140431
140439
  }
140432
140440
  function initApis(apisInfo, tsShadowFiles) {
@@ -140623,7 +140631,7 @@ var pxt;
140623
140631
  };
140624
140632
  }
140625
140633
  }
140626
- // next free error 9575
140634
+ // next free error 9576
140627
140635
  function error(astNode, code, msg) {
140628
140636
  diagnostics.push(mkDiag(astNode, pxtc.DiagnosticCategory.Error, code, msg));
140629
140637
  //const pos = position(astNode ? astNode.startPos || 0 : 0, mod.source)
@@ -140834,12 +140842,18 @@ var pxt;
140834
140842
  return n.symInfo;
140835
140843
  }
140836
140844
  // TODO optimize ?
140837
- function listClassFields(cd) {
140845
+ function listClassFields(cd, excludeVariables = true) {
140838
140846
  let qn = cd.symInfo.qName;
140839
- return pxt.U.values(internalApis).filter(e => e.namespace == qn && e.kind == 2 /* Property */);
140847
+ return pxt.U.values(internalApis).filter(e => e.namespace == qn && ((e.kind == 4 /* Variable */ && !excludeVariables) || e.kind == 2 /* Property */));
140840
140848
  }
140841
140849
  function getClassField(ct, n, checkOnly = false, skipBases = false) {
140842
- let qid = ct.pyQName + "." + n;
140850
+ let qid;
140851
+ if (n === "__init__") {
140852
+ qid = ct.pyQName + ".__constructor";
140853
+ }
140854
+ else {
140855
+ qid = ct.pyQName + "." + n;
140856
+ }
140843
140857
  let f = lookupGlobalSymbol(qid);
140844
140858
  if (f)
140845
140859
  return f;
@@ -141066,14 +141080,17 @@ var pxt;
141066
141080
  return mkType({});
141067
141081
  }
141068
141082
  function doArgs(n, isMethod) {
141083
+ var _a;
141069
141084
  const args = n.args;
141070
141085
  if (args.kwonlyargs.length)
141071
141086
  error(n, 9517, pxt.U.lf("keyword-only arguments not supported yet"));
141072
141087
  let nargs = args.args.slice();
141073
141088
  if (isMethod) {
141074
- if (nargs[0].arg != "self")
141075
- error(n, 9518, pxt.U.lf("first argument of method has to be called 'self'"));
141076
- nargs.shift();
141089
+ if (((_a = nargs[0]) === null || _a === void 0 ? void 0 : _a.arg) !== "self")
141090
+ n.symInfo.isStatic = true;
141091
+ else {
141092
+ nargs.shift();
141093
+ }
141077
141094
  }
141078
141095
  else {
141079
141096
  if (nargs.some(a => a.arg == "self"))
@@ -141235,6 +141252,7 @@ var pxt;
141235
141252
  }
141236
141253
  function emitFunctionDef(n, inline = false) {
141237
141254
  return guardedScope(n, () => {
141255
+ var _a, _b, _c, _d;
141238
141256
  const isMethod = !!ctx.currClass && !ctx.currFun;
141239
141257
  const topLev = isTopLevel();
141240
141258
  const nested = !!ctx.currFun;
@@ -141295,6 +141313,9 @@ var pxt;
141295
141313
  }
141296
141314
  if (!prefix) {
141297
141315
  prefix = funname[0] == "_" ? (sym.isProtected ? "protected" : "private") : "public";
141316
+ if (n.symInfo.isStatic) {
141317
+ prefix += " static";
141318
+ }
141298
141319
  }
141299
141320
  nodes.push(B.mkText(prefix + " "), quote(funname));
141300
141321
  }
@@ -141306,7 +141327,7 @@ var pxt;
141306
141327
  else
141307
141328
  nodes.push(B.mkText("export function "), quote(funname));
141308
141329
  }
141309
- let retType = n.returns ? compileType(n.returns) : sym.pyRetType;
141330
+ let retType = n.name == "__init__" ? undefined : (n.returns ? compileType(n.returns) : sym.pyRetType);
141310
141331
  nodes.push(doArgs(n, isMethod), retType && canonicalize(retType) != tpVoid ? typeAnnot(retType) : B.mkText(""));
141311
141332
  // make sure type is initialized
141312
141333
  getOrSetSymbolType(sym);
@@ -141314,6 +141335,13 @@ var pxt;
141314
141335
  if (n.name == "__init__") {
141315
141336
  if (!ctx.currClass)
141316
141337
  error(n, 9533, lf("__init__ method '{0}' is missing current class context", sym.pyQName));
141338
+ if ((_a = ctx.currClass) === null || _a === void 0 ? void 0 : _a.baseClass) {
141339
+ const firstStatement = n.body[0];
141340
+ const superConstructor = ctx.currClass.baseClass.pyQName + ".__constructor";
141341
+ if (((_d = (_c = (_b = firstStatement.value) === null || _b === void 0 ? void 0 : _b.func) === null || _c === void 0 ? void 0 : _c.symbolInfo) === null || _d === void 0 ? void 0 : _d.pyQName) !== superConstructor) {
141342
+ error(n, 9575, lf("Sub classes must call 'super().__init__' as the first statement inside an __init__ method"));
141343
+ }
141344
+ }
141317
141345
  for (let f of listClassFields(ctx.currClass)) {
141318
141346
  let p = f.pyAST;
141319
141347
  if (p && p.value) {
@@ -141357,22 +141385,59 @@ var pxt;
141357
141385
  nodes.push(B.mkCommaSep(n.bases.map(expr)));
141358
141386
  let b = getClassDef(n.bases[0]);
141359
141387
  if (b) {
141360
- n.baseClass = b;
141388
+ n.baseClass = b.symInfo;
141361
141389
  sym.extendsTypes = [b.symInfo.pyQName];
141362
141390
  }
141391
+ else {
141392
+ const nm = tryGetName(n.bases[0]);
141393
+ if (nm) {
141394
+ const localSym = lookupSymbol(nm);
141395
+ const globalSym = lookupGlobalSymbol(nm);
141396
+ n.baseClass = localSym || globalSym;
141397
+ if (n.baseClass)
141398
+ sym.extendsTypes = [n.baseClass.pyQName];
141399
+ }
141400
+ }
141363
141401
  }
141364
141402
  }
141365
- let body = stmts(n.body);
141403
+ const classDefs = n.body.filter(s => n.isNamespace || s.kind === "FunctionDef");
141404
+ const staticStmts = n.isNamespace ? [] : n.body.filter(s => classDefs.indexOf(s) === -1);
141405
+ let body = stmts(classDefs);
141366
141406
  nodes.push(body);
141367
- let fieldDefs = listClassFields(n)
141368
- .filter(f => f.kind == 2 /* Property */ && f.isInstance)
141369
- .map(f => {
141370
- if (!f.pyName || !f.pyRetType)
141371
- error(n, 9535, lf("field definition missing py name or ret type", f.qName));
141372
- return f;
141373
- })
141374
- .map((f) => B.mkStmt(accessAnnot(f), quote(f.pyName), typeAnnot(f.pyRetType)));
141375
- body.children = fieldDefs.concat(body.children);
141407
+ // Python classes allow arbitrary statements in their bodies, sort of like namespaces.
141408
+ // Take all of these statements and put them in a static method that we can call when
141409
+ // the class is defined.
141410
+ let generatedInitFunction = false;
141411
+ if (staticStmts.length) {
141412
+ generatedInitFunction = true;
141413
+ const staticBody = stmts(staticStmts);
141414
+ const initFun = B.mkStmt(B.mkGroup([
141415
+ B.mkText(`public static __init${n.name}() `),
141416
+ staticBody
141417
+ ]));
141418
+ body.children.unshift(initFun);
141419
+ }
141420
+ if (!n.isNamespace) {
141421
+ let isStatic = (f) => f.kind === 2 /* Property */ && !f.isInstance || f.kind === 4 /* Variable */;
141422
+ const fieldDefs = listClassFields(n, false)
141423
+ .filter(f => f.kind == 2 /* Property */ || isStatic(f))
141424
+ .map(f => {
141425
+ if (!f.pyName || !f.pyRetType)
141426
+ error(n, 9535, lf("field definition missing py name or return type", f.qName));
141427
+ return f;
141428
+ });
141429
+ const instanceFields = fieldDefs.filter(f => !isStatic(f))
141430
+ .map((f) => B.mkStmt(accessAnnot(f), quote(f.pyName), typeAnnot(f.pyRetType)));
141431
+ const staticFields = fieldDefs.filter(f => isStatic(f))
141432
+ .map((f) => B.mkStmt(accessAnnot(f), B.mkText("static "), quote(f.pyName), typeAnnot(f.pyRetType)));
141433
+ body.children = staticFields.concat(instanceFields).concat(body.children);
141434
+ }
141435
+ if (generatedInitFunction) {
141436
+ nodes = [
141437
+ B.mkStmt(B.mkGroup(nodes)),
141438
+ B.mkStmt(B.mkText(`${n.name}.__init${n.name}()`))
141439
+ ];
141440
+ }
141376
141441
  return B.mkStmt(B.mkGroup(nodes));
141377
141442
  }),
141378
141443
  Return: (n) => {
@@ -141721,7 +141786,9 @@ var pxt;
141721
141786
  error(n, 9539, lf("function '{0}' missing return type", fd.pyQName));
141722
141787
  unifyTypeOf(target, fd.pyRetType);
141723
141788
  fd.isInstance = false;
141724
- pref = ctx.currClass.isNamespace ? `export ${isConstCall ? "const" : "let"} ` : "static ";
141789
+ if (ctx.currClass.isNamespace) {
141790
+ pref = `export ${isConstCall ? "const" : "let"} `;
141791
+ }
141725
141792
  }
141726
141793
  if (value)
141727
141794
  unifyTypeOf(target, typeOf(value));
@@ -141787,6 +141854,7 @@ var pxt;
141787
141854
  }
141788
141855
  }
141789
141856
  function possibleDef(n, excludeLet = false) {
141857
+ var _a, _b;
141790
141858
  let id = n.id;
141791
141859
  let currScopeVar = lookupScopeSymbol(id);
141792
141860
  let curr = currScopeVar === null || currScopeVar === void 0 ? void 0 : currScopeVar.symbol;
@@ -141821,6 +141889,10 @@ var pxt;
141821
141889
  if (n.isdef && !excludeLet) {
141822
141890
  return B.mkGroup([B.mkText("let "), quote(id)]);
141823
141891
  }
141892
+ else if ((curr === null || curr === void 0 ? void 0 : curr.namespace) && (curr === null || curr === void 0 ? void 0 : curr.qName) && !(((_a = ctx.currClass) === null || _a === void 0 ? void 0 : _a.isNamespace) && ((_b = ctx.currClass) === null || _b === void 0 ? void 0 : _b.name) === (curr === null || curr === void 0 ? void 0 : curr.namespace))) {
141893
+ // If this is a static variable in a class, we want the full qname
141894
+ return quote(curr.qName);
141895
+ }
141824
141896
  else
141825
141897
  return quote(id);
141826
141898
  }
@@ -141834,20 +141906,27 @@ var pxt;
141834
141906
  //return id.replace(/([a-z0-9])_([a-zA-Z0-9])/g, (f: string, x: string, y: string) => x + y.toUpperCase())
141835
141907
  }
141836
141908
  function tryGetName(e) {
141909
+ var _a;
141837
141910
  if (e.kind == "Name") {
141838
141911
  let s = e.id;
141839
141912
  let scopeV = lookupVar(s);
141840
141913
  let v = scopeV === null || scopeV === void 0 ? void 0 : scopeV.symbol;
141841
- if (v && v.expandsTo)
141842
- return v.expandsTo;
141843
- else
141844
- return s;
141914
+ if (v) {
141915
+ if (v.expandsTo)
141916
+ return v.expandsTo;
141917
+ else if (ctx.currClass && !ctx.currFun && !(scopeV === null || scopeV === void 0 ? void 0 : scopeV.modifier) && v.qName)
141918
+ return v.qName;
141919
+ }
141920
+ return s;
141845
141921
  }
141846
141922
  if (e.kind == "Attribute") {
141847
141923
  let pref = tryGetName(e.value);
141848
141924
  if (pref)
141849
141925
  return pref + "." + e.attr;
141850
141926
  }
141927
+ if (isSuper(e) && ((_a = ctx.currClass) === null || _a === void 0 ? void 0 : _a.baseClass)) {
141928
+ return ctx.currClass.baseClass.qName;
141929
+ }
141851
141930
  return undefined;
141852
141931
  }
141853
141932
  function getName(e) {
@@ -142055,6 +142134,7 @@ var pxt;
142055
142134
  return r;
142056
142135
  },
142057
142136
  Call: (n) => {
142137
+ var _a, _b, _c, _d, _e;
142058
142138
  // TODO(dz): move body out; needs seperate PR that doesn't touch content
142059
142139
  n.func.inCalledPosition = true;
142060
142140
  let nm = tryGetName(n.func);
@@ -142085,7 +142165,7 @@ var pxt;
142085
142165
  if (ctx.currClass && ctx.currClass.baseClass) {
142086
142166
  if (!n.tsType)
142087
142167
  error(n, 9543, lf("call expr missing ts type"));
142088
- unifyClass(n, n.tsType, ctx.currClass.baseClass.symInfo);
142168
+ unifyClass(n, n.tsType, ctx.currClass.baseClass);
142089
142169
  }
142090
142170
  return B.mkText("super");
142091
142171
  }
@@ -142145,6 +142225,17 @@ var pxt;
142145
142225
  unify(n, n.tsType, tpString);
142146
142226
  return B.mkInfix(B.mkText(`""`), "+", expr(n.args[0]));
142147
142227
  }
142228
+ const isSuperAttribute = n.func.kind === "Attribute" && isSuper(n.func.value);
142229
+ if (!fun && isSuperAttribute) {
142230
+ fun = lookupGlobalSymbol(nm);
142231
+ }
142232
+ const isSuperConstructor = ((_a = ctx.currFun) === null || _a === void 0 ? void 0 : _a.name) === "__init__" &&
142233
+ (fun === null || fun === void 0 ? void 0 : fun.name) === "__constructor" &&
142234
+ ((_c = (_b = ctx.currClass) === null || _b === void 0 ? void 0 : _b.baseClass) === null || _c === void 0 ? void 0 : _c.pyQName) === (fun === null || fun === void 0 ? void 0 : fun.namespace) &&
142235
+ isSuperAttribute;
142236
+ if (isSuperConstructor) {
142237
+ fun = lookupSymbol(((_e = (_d = ctx.currClass) === null || _d === void 0 ? void 0 : _d.baseClass) === null || _e === void 0 ? void 0 : _e.pyQName) + ".__constructor");
142238
+ }
142148
142239
  if (!fun) {
142149
142240
  error(n, 9508, pxt.U.lf("can't find called function '{0}'", nm));
142150
142241
  }
@@ -142252,7 +142343,14 @@ var pxt;
142252
142343
  ]);
142253
142344
  }
142254
142345
  }
142255
- let fn = methName ? B.mkInfix(expr(recv), ".", B.mkText(methName)) : expr(n.func);
142346
+ // TODO (riknoll): Make sure __init__ isn't being added as a symbol by the super call in the subclass. Should be converted to .__constructor
142347
+ let fn;
142348
+ if (isSuperConstructor) {
142349
+ fn = B.mkText("super");
142350
+ }
142351
+ else {
142352
+ fn = methName ? B.mkInfix(expr(recv), ".", B.mkText(methName)) : expr(n.func);
142353
+ }
142256
142354
  let nodes = [
142257
142355
  fn,
142258
142356
  B.mkText("("),
@@ -14988,6 +14988,15 @@ var pxtblockly;
14988
14988
  }
14989
14989
  })(pxtblockly || (pxtblockly = {}));
14990
14990
  /// <reference path="../../localtypings/pxtblockly.d.ts" />
14991
+ // common time options -- do not remove
14992
+ // lf("100 ms")
14993
+ // lf("200 ms")
14994
+ // lf("500 ms")
14995
+ // lf("1 second")
14996
+ // lf("2 seconds")
14997
+ // lf("5 seconds")
14998
+ // lf("1 minute")
14999
+ // lf("1 hour")
14991
15000
  var pxtblockly;
14992
15001
  (function (pxtblockly) {
14993
15002
  class FieldNumberDropdown extends Blockly.FieldNumberDropdown {
@@ -14999,7 +15008,12 @@ var pxtblockly;
14999
15008
  let newOptions;
15000
15009
  if (this.menuGenerator_) {
15001
15010
  newOptions = JSON.parse(this.menuGenerator_).map((x) => {
15002
- return (typeof x == 'object') ? x : [String(x), String(x)];
15011
+ if (typeof x == 'object') {
15012
+ return [pxt.Util.rlf(x[0]), x[1]];
15013
+ }
15014
+ else {
15015
+ return [String(x), String(x)];
15016
+ }
15003
15017
  });
15004
15018
  }
15005
15019
  return newOptions;
@@ -11426,6 +11426,15 @@ var pxtblockly;
11426
11426
  }
11427
11427
  })(pxtblockly || (pxtblockly = {}));
11428
11428
  /// <reference path="../../localtypings/pxtblockly.d.ts" />
11429
+ // common time options -- do not remove
11430
+ // lf("100 ms")
11431
+ // lf("200 ms")
11432
+ // lf("500 ms")
11433
+ // lf("1 second")
11434
+ // lf("2 seconds")
11435
+ // lf("5 seconds")
11436
+ // lf("1 minute")
11437
+ // lf("1 hour")
11429
11438
  var pxtblockly;
11430
11439
  (function (pxtblockly) {
11431
11440
  class FieldNumberDropdown extends Blockly.FieldNumberDropdown {
@@ -11437,7 +11446,12 @@ var pxtblockly;
11437
11446
  let newOptions;
11438
11447
  if (this.menuGenerator_) {
11439
11448
  newOptions = JSON.parse(this.menuGenerator_).map((x) => {
11440
- return (typeof x == 'object') ? x : [String(x), String(x)];
11449
+ if (typeof x == 'object') {
11450
+ return [pxt.Util.rlf(x[0]), x[1]];
11451
+ }
11452
+ else {
11453
+ return [String(x), String(x)];
11454
+ }
11441
11455
  });
11442
11456
  }
11443
11457
  return newOptions;
package/built/pxtpy.d.ts CHANGED
@@ -16,6 +16,7 @@ declare namespace pxt.py {
16
16
  pyAST?: AST;
17
17
  isProtected?: boolean;
18
18
  moduleTypeMarker?: {};
19
+ isStatic?: boolean;
19
20
  declared?: number;
20
21
  }
21
22
  interface TypeOptions {
@@ -170,7 +171,7 @@ declare namespace pxt.py {
170
171
  keywords: Keyword[];
171
172
  body: Stmt[];
172
173
  decorator_list: Expr[];
173
- baseClass?: ClassDef;
174
+ baseClass?: SymbolInfo;
174
175
  isEnum?: boolean;
175
176
  isNamespace?: boolean;
176
177
  }