Skip to content

Commit f184d66

Browse files
committed
fix: avoid validating arrow function parameters too early
Only check validity of the left-hand value of the assignment when we're not possibly inside an arrow function, as the AST will have e.g. an ObjectExpression, not an ObjectPattern at this point (the conversion happens later) Fixes sveltejs/svelte#14393
1 parent be66eeb commit f184d66

File tree

4 files changed

+246
-2
lines changed

4 files changed

+246
-2
lines changed

.changeset/salty-cats-rhyme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/acorn-typescript': patch
3+
---
4+
5+
fix: avoid validating arrow function parameters too early

__test__/__snapshot__/arrow-function/type.ts

+221
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,227 @@ const ArrowFunctionTypeSnapshot = {
18891889
}
18901890
],
18911891
sourceType: 'module'
1892+
},
1893+
DestructuringDefaultValue: {
1894+
type: 'Program',
1895+
start: 0,
1896+
end: 82,
1897+
loc: { start: { line: 1, column: 0, index: 0 }, end: { line: 3, column: 1, index: 82 } },
1898+
body: [
1899+
{
1900+
type: 'VariableDeclaration',
1901+
start: 0,
1902+
end: 82,
1903+
loc: { start: { line: 1, column: 0, index: 0 }, end: { line: 3, column: 1, index: 82 } },
1904+
declarations: [
1905+
{
1906+
type: 'VariableDeclarator',
1907+
start: 6,
1908+
end: 82,
1909+
loc: {
1910+
start: { line: 1, column: 6, index: 6 },
1911+
end: { line: 3, column: 1, index: 82 }
1912+
},
1913+
id: {
1914+
type: 'Identifier',
1915+
start: 6,
1916+
end: 15,
1917+
loc: {
1918+
start: { line: 1, column: 6, index: 6 },
1919+
end: { line: 1, column: 15, index: 15 }
1920+
},
1921+
name: 'increment'
1922+
},
1923+
init: {
1924+
type: 'ArrowFunctionExpression',
1925+
start: 18,
1926+
end: 82,
1927+
loc: {
1928+
start: { line: 1, column: 18, index: 18 },
1929+
end: { line: 3, column: 1, index: 82 }
1930+
},
1931+
id: null,
1932+
expression: false,
1933+
generator: false,
1934+
async: false,
1935+
params: [
1936+
{
1937+
type: 'AssignmentPattern',
1938+
start: 19,
1939+
end: 62,
1940+
loc: {
1941+
start: { line: 1, column: 19, index: 19 },
1942+
end: { line: 1, column: 62, index: 62 }
1943+
},
1944+
left: {
1945+
type: 'ObjectPattern',
1946+
start: 19,
1947+
loc: { start: { line: 1, column: 19, index: 19 }, end: 57 },
1948+
properties: [
1949+
{
1950+
type: 'Property',
1951+
start: 21,
1952+
end: 30,
1953+
loc: {
1954+
start: { line: 1, column: 21, index: 21 },
1955+
end: { line: 1, column: 30, index: 30 }
1956+
},
1957+
method: false,
1958+
shorthand: true,
1959+
computed: false,
1960+
key: {
1961+
type: 'Identifier',
1962+
start: 21,
1963+
end: 30,
1964+
loc: {
1965+
start: { line: 1, column: 21, index: 21 },
1966+
end: { line: 1, column: 30, index: 30 }
1967+
},
1968+
name: 'increment'
1969+
},
1970+
kind: 'init',
1971+
value: {
1972+
type: 'Identifier',
1973+
start: 21,
1974+
end: 30,
1975+
loc: {
1976+
start: { line: 1, column: 21, index: 21 },
1977+
end: { line: 1, column: 30, index: 30 }
1978+
},
1979+
name: 'increment'
1980+
}
1981+
}
1982+
],
1983+
typeAnnotation: {
1984+
type: 'TSTypeAnnotation',
1985+
start: 33,
1986+
end: 57,
1987+
loc: {
1988+
start: { line: 1, column: 33, index: 33 },
1989+
end: { line: 1, column: 57, index: 57 }
1990+
},
1991+
typeAnnotation: {
1992+
type: 'TSTypeLiteral',
1993+
start: 35,
1994+
end: 57,
1995+
loc: {
1996+
start: { line: 1, column: 35, index: 35 },
1997+
end: { line: 1, column: 57, index: 57 }
1998+
},
1999+
members: [
2000+
{
2001+
type: 'TSPropertySignature',
2002+
start: 37,
2003+
end: 55,
2004+
loc: {
2005+
start: { line: 1, column: 37, index: 37 },
2006+
end: { line: 1, column: 55, index: 55 }
2007+
},
2008+
computed: false,
2009+
key: {
2010+
type: 'Identifier',
2011+
start: 37,
2012+
end: 46,
2013+
loc: {
2014+
start: { line: 1, column: 37, index: 37 },
2015+
end: { line: 1, column: 46, index: 46 }
2016+
},
2017+
name: 'increment'
2018+
},
2019+
optional: true,
2020+
typeAnnotation: {
2021+
type: 'TSTypeAnnotation',
2022+
start: 47,
2023+
end: 55,
2024+
loc: {
2025+
start: { line: 1, column: 47, index: 47 },
2026+
end: { line: 1, column: 55, index: 55 }
2027+
},
2028+
typeAnnotation: {
2029+
type: 'TSNumberKeyword',
2030+
start: 49,
2031+
end: 55,
2032+
loc: {
2033+
start: { line: 1, column: 49, index: 49 },
2034+
end: { line: 1, column: 55, index: 55 }
2035+
}
2036+
}
2037+
}
2038+
}
2039+
]
2040+
}
2041+
}
2042+
},
2043+
right: {
2044+
type: 'ObjectExpression',
2045+
start: 60,
2046+
end: 62,
2047+
loc: {
2048+
start: { line: 1, column: 60, index: 60 },
2049+
end: { line: 1, column: 62, index: 62 }
2050+
},
2051+
properties: []
2052+
}
2053+
}
2054+
],
2055+
body: {
2056+
type: 'BlockStatement',
2057+
start: 67,
2058+
end: 82,
2059+
loc: {
2060+
start: { line: 1, column: 67, index: 67 },
2061+
end: { line: 3, column: 1, index: 82 }
2062+
},
2063+
body: [
2064+
{
2065+
type: 'ExpressionStatement',
2066+
start: 70,
2067+
end: 80,
2068+
loc: {
2069+
start: { line: 2, column: 1, index: 70 },
2070+
end: { line: 2, column: 11, index: 80 }
2071+
},
2072+
expression: {
2073+
type: 'AssignmentExpression',
2074+
start: 70,
2075+
end: 80,
2076+
loc: {
2077+
start: { line: 2, column: 1, index: 70 },
2078+
end: { line: 2, column: 11, index: 80 }
2079+
},
2080+
operator: '+=',
2081+
left: {
2082+
type: 'Identifier',
2083+
start: 70,
2084+
end: 75,
2085+
loc: {
2086+
start: { line: 2, column: 1, index: 70 },
2087+
end: { line: 2, column: 6, index: 75 }
2088+
},
2089+
name: 'count'
2090+
},
2091+
right: {
2092+
type: 'Literal',
2093+
start: 79,
2094+
end: 80,
2095+
loc: {
2096+
start: { line: 2, column: 10, index: 79 },
2097+
end: { line: 2, column: 11, index: 80 }
2098+
},
2099+
value: 1,
2100+
raw: '1'
2101+
}
2102+
}
2103+
}
2104+
]
2105+
}
2106+
}
2107+
}
2108+
],
2109+
kind: 'const'
2110+
}
2111+
],
2112+
sourceType: 'module'
18922113
}
18932114
};
18942115

__test__/arrow-function/type.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,16 @@ describe('arrow-function type test', () => {
7272

7373
equalNode(node, ArrowFunctionTypeSnapshot.AsyncRestParameter);
7474
});
75+
76+
it('destructuring + default value', () => {
77+
const node = parseSource(
78+
generateSource([
79+
`const increment = ({ increment } : { increment?: number } = {}) => {`,
80+
` count += 1`,
81+
`}`
82+
])
83+
);
84+
85+
equalNode(node, ArrowFunctionTypeSnapshot.DestructuringDefaultValue);
86+
});
7587
});

src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -3966,8 +3966,14 @@ export function tsPlugin(options?: {
39663966
}
39673967
if (refDestructuringErrors.shorthandAssign >= left.start)
39683968
refDestructuringErrors.shorthandAssign = -1; // reset because shorthand default was used correctly
3969-
if (this.type === tt.eq) this.checkLValPattern(left);
3970-
else this.checkLValSimple(left);
3969+
3970+
// Only check validity when we're not possibly inside an arrow function, as the AST
3971+
// will have e.g. an ObjectExpression, not an ObjectPattern at this point (the conversion happens later)
3972+
if (!this.maybeInArrowParameters) {
3973+
if (this.type === tt.eq) this.checkLValPattern(left);
3974+
else this.checkLValSimple(left);
3975+
}
3976+
39713977
node.left = left;
39723978
this.next();
39733979
node.right = this.parseMaybeAssign(forInit);

0 commit comments

Comments
 (0)