Skip to content

Commit 6267f0c

Browse files
committed
fix: support non-null expressions in more places
Fixes TyrealHu/acorn-typescript#64 Fixes sveltejs/svelte#13409
1 parent d8b2a7e commit 6267f0c

File tree

4 files changed

+341
-0
lines changed

4 files changed

+341
-0
lines changed

.changeset/yummy-dogs-dance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/acorn-typescript': patch
3+
---
4+
5+
fix: support non-null expressions in more places

__test__/__snapshot__/expression/variables.ts

+325
Original file line numberDiff line numberDiff line change
@@ -5011,6 +5011,331 @@ const VariablesTypeSnapshot = {
50115011
}
50125012
],
50135013
sourceType: 'module'
5014+
},
5015+
NonNullAssignment: {
5016+
type: 'Program',
5017+
start: 0,
5018+
end: 44,
5019+
loc: {
5020+
start: {
5021+
line: 1,
5022+
column: 0,
5023+
index: 0
5024+
},
5025+
end: {
5026+
line: 3,
5027+
column: 7,
5028+
index: 44
5029+
}
5030+
},
5031+
body: [
5032+
{
5033+
type: 'VariableDeclaration',
5034+
start: 0,
5035+
end: 29,
5036+
loc: {
5037+
start: {
5038+
line: 1,
5039+
column: 0,
5040+
index: 0
5041+
},
5042+
end: {
5043+
line: 1,
5044+
column: 29,
5045+
index: 29
5046+
}
5047+
},
5048+
declarations: [
5049+
{
5050+
type: 'VariableDeclarator',
5051+
start: 4,
5052+
end: 29,
5053+
loc: {
5054+
start: {
5055+
line: 1,
5056+
column: 4,
5057+
index: 4
5058+
},
5059+
end: {
5060+
line: 1,
5061+
column: 29,
5062+
index: 29
5063+
}
5064+
},
5065+
id: {
5066+
type: 'Identifier',
5067+
start: 4,
5068+
end: 25,
5069+
loc: {
5070+
start: {
5071+
line: 1,
5072+
column: 4,
5073+
index: 4
5074+
},
5075+
end: {
5076+
line: 1,
5077+
column: 25,
5078+
index: 25
5079+
}
5080+
},
5081+
name: 'a',
5082+
typeAnnotation: {
5083+
type: 'TSTypeAnnotation',
5084+
start: 5,
5085+
end: 25,
5086+
loc: {
5087+
start: {
5088+
line: 1,
5089+
column: 5,
5090+
index: 5
5091+
},
5092+
end: {
5093+
line: 1,
5094+
column: 25,
5095+
index: 25
5096+
}
5097+
},
5098+
typeAnnotation: {
5099+
type: 'TSUnionType',
5100+
start: 7,
5101+
end: 25,
5102+
loc: {
5103+
start: {
5104+
line: 1,
5105+
column: 7,
5106+
index: 7
5107+
},
5108+
end: {
5109+
line: 1,
5110+
column: 25,
5111+
index: 25
5112+
}
5113+
},
5114+
types: [
5115+
{
5116+
type: 'TSNumberKeyword',
5117+
start: 7,
5118+
end: 13,
5119+
loc: {
5120+
start: {
5121+
line: 1,
5122+
column: 7,
5123+
index: 7
5124+
},
5125+
end: {
5126+
line: 1,
5127+
column: 13,
5128+
index: 13
5129+
}
5130+
}
5131+
},
5132+
{
5133+
type: 'TSUndefinedKeyword',
5134+
start: 16,
5135+
end: 25,
5136+
loc: {
5137+
start: {
5138+
line: 1,
5139+
column: 16,
5140+
index: 16
5141+
},
5142+
end: {
5143+
line: 1,
5144+
column: 25,
5145+
index: 25
5146+
}
5147+
}
5148+
}
5149+
]
5150+
}
5151+
}
5152+
},
5153+
init: {
5154+
type: 'Literal',
5155+
start: 28,
5156+
end: 29,
5157+
loc: {
5158+
start: {
5159+
line: 1,
5160+
column: 28,
5161+
index: 28
5162+
},
5163+
end: {
5164+
line: 1,
5165+
column: 29,
5166+
index: 29
5167+
}
5168+
},
5169+
value: 1,
5170+
raw: '1'
5171+
}
5172+
}
5173+
],
5174+
kind: 'let'
5175+
},
5176+
{
5177+
type: 'ExpressionStatement',
5178+
start: 30,
5179+
end: 36,
5180+
loc: {
5181+
start: {
5182+
line: 2,
5183+
column: 0,
5184+
index: 30
5185+
},
5186+
end: {
5187+
line: 2,
5188+
column: 6,
5189+
index: 36
5190+
}
5191+
},
5192+
expression: {
5193+
type: 'AssignmentExpression',
5194+
start: 30,
5195+
end: 36,
5196+
loc: {
5197+
start: {
5198+
line: 2,
5199+
column: 0,
5200+
index: 30
5201+
},
5202+
end: {
5203+
line: 2,
5204+
column: 6,
5205+
index: 36
5206+
}
5207+
},
5208+
operator: '=',
5209+
left: {
5210+
type: 'Identifier',
5211+
start: 30,
5212+
end: 31,
5213+
loc: {
5214+
start: {
5215+
line: 2,
5216+
column: 0,
5217+
index: 30
5218+
},
5219+
end: {
5220+
line: 2,
5221+
column: 1,
5222+
index: 31
5223+
}
5224+
},
5225+
name: 'a'
5226+
},
5227+
right: {
5228+
type: 'Literal',
5229+
start: 35,
5230+
end: 36,
5231+
loc: {
5232+
start: {
5233+
line: 2,
5234+
column: 5,
5235+
index: 35
5236+
},
5237+
end: {
5238+
line: 2,
5239+
column: 6,
5240+
index: 36
5241+
}
5242+
},
5243+
value: 2,
5244+
raw: '2'
5245+
}
5246+
}
5247+
},
5248+
{
5249+
type: 'ExpressionStatement',
5250+
start: 37,
5251+
end: 44,
5252+
loc: {
5253+
start: {
5254+
line: 3,
5255+
column: 0,
5256+
index: 37
5257+
},
5258+
end: {
5259+
line: 3,
5260+
column: 7,
5261+
index: 44
5262+
}
5263+
},
5264+
expression: {
5265+
type: 'AssignmentExpression',
5266+
start: 37,
5267+
end: 44,
5268+
loc: {
5269+
start: {
5270+
line: 3,
5271+
column: 0,
5272+
index: 37
5273+
},
5274+
end: {
5275+
line: 3,
5276+
column: 7,
5277+
index: 44
5278+
}
5279+
},
5280+
operator: '+=',
5281+
left: {
5282+
type: 'TSNonNullExpression',
5283+
start: 37,
5284+
end: 39,
5285+
loc: {
5286+
start: {
5287+
line: 3,
5288+
column: 0,
5289+
index: 37
5290+
},
5291+
end: {
5292+
line: 3,
5293+
column: 2,
5294+
index: 39
5295+
}
5296+
},
5297+
expression: {
5298+
type: 'Identifier',
5299+
start: 37,
5300+
end: 38,
5301+
loc: {
5302+
start: {
5303+
line: 3,
5304+
column: 0,
5305+
index: 37
5306+
},
5307+
end: {
5308+
line: 3,
5309+
column: 1,
5310+
index: 38
5311+
}
5312+
},
5313+
name: 'a'
5314+
}
5315+
},
5316+
right: {
5317+
type: 'Literal',
5318+
start: 43,
5319+
end: 44,
5320+
loc: {
5321+
start: {
5322+
line: 3,
5323+
column: 6,
5324+
index: 43
5325+
},
5326+
end: {
5327+
line: 3,
5328+
column: 7,
5329+
index: 44
5330+
}
5331+
},
5332+
value: 3,
5333+
raw: '3'
5334+
}
5335+
}
5336+
}
5337+
],
5338+
sourceType: 'module'
50145339
}
50155340
};
50165341

__test__/expression/variables.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,12 @@ describe('variables declaration', () => {
197197

198198
equalNode(node, VariablesTypeSnapshot.IssueFile43);
199199
});
200+
201+
it('TSNonNullExpression', () => {
202+
const node = parseSource(
203+
generateSource([`let a: number | undefined = 1`, `a! = 2`, `a! += 3`])
204+
);
205+
206+
equalNode(node, VariablesTypeSnapshot.NonNullAssignment);
207+
});
200208
});

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,9 @@ function tsPlugin(options?: {
25922592
}
25932593

25942594
checkLValSimple(expr: any, bindingType: any = acornScope.BIND_NONE, checkClashes?: any) {
2595+
if (expr.type === 'TSNonNullExpression') {
2596+
expr = expr.expression;
2597+
}
25952598
return super.checkLValSimple(expr, bindingType, checkClashes);
25962599
}
25972600

0 commit comments

Comments
 (0)