Browse Source

Add binding initialisations to srcMap. Closes #353.

Dmitry Panov 3 years ago
parent
commit
3f9136fa23
2 changed files with 35 additions and 0 deletions
  1. 2 0
      compiler_stmt.go
  2. 33 0
      compiler_test.go

+ 2 - 0
compiler_stmt.go

@@ -756,6 +756,7 @@ func (c *compiler) emitVarAssign(name unistring.String, offset int, init compile
 	if init != nil {
 		c.emitVarRef(name, offset)
 		c.emitNamed(init, name)
+		c.p.addSrcMap(offset)
 		c.emit(initValueP)
 	}
 }
@@ -779,6 +780,7 @@ func (c *compiler) emitLexicalAssign(name unistring.String, offset int, init com
 	}
 	if init != nil {
 		c.emitNamed(init, name)
+		c.p.addSrcMap(offset)
 	} else {
 		if isConst {
 			c.throwSyntaxError(offset, "Missing initializer in const declaration")

+ 33 - 0
compiler_test.go

@@ -4531,6 +4531,39 @@ func TestSrcLocationThrowLiteral(t *testing.T) {
 	}
 }
 
+func TestSrcLocation(t *testing.T) {
+	prg := MustCompile("test.js", `
+f();
+var x = 1;
+let y = 1;
+let [z1, z2] = [0, 0];
+
+var [z3, z4] = [0, 0];
+	`, false)
+	const (
+		varLine     = 3
+		letLine     = 4
+		dstrLetLine = 5
+		dstrVarLine = 7
+	)
+	linesOfInterest := map[int]string{
+		varLine:     "var",
+		letLine:     "let",
+		dstrLetLine: "destruct let",
+		dstrVarLine: "destruct var",
+	}
+	for i := range prg.code {
+		loc := prg.src.Position(prg.sourceOffset(i))
+		delete(linesOfInterest, loc.Line)
+		if len(linesOfInterest) == 0 {
+			break
+		}
+	}
+	for _, v := range linesOfInterest {
+		t.Fatalf("no %s line", v)
+	}
+}
+
 func TestBadObjectKey(t *testing.T) {
 	_, err := Compile("", "({!:0})", false)
 	if err == nil {