Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbcfb3d8

Browse files
committed
fix: lint
1 parentea2182a commitbcfb3d8

File tree

2 files changed

+85
-66
lines changed

2 files changed

+85
-66
lines changed

‎lib/StrictCompatibilityPlugin.js‎

Lines changed: 80 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ class StrictCompatibilityPlugin {
6060

6161
parser.hooks.program.tap(PLUGIN_NAME,(ast)=>{
6262
constmod=parser.state.module;
63-
constsourceObj=mod.originalSource&&mod.originalSource();
64-
constsrc=
65-
sourceObj&&typeofsourceObj.source==="function"
66-
?sourceObj.source()
67-
:undefined;
63+
64+
/**
65+
*@param {string} msg message
66+
*@param {DependencyLocation | undefined} loc location
67+
*/
6868
constreportError=(msg,loc)=>{
6969
if(mode==="error"){
7070
mod.addError(newStrictCompatibilityError(msg,loc));
7171
}else{
7272
mod.addWarning(newStrictCompatibilityWarning(msg,loc));
7373
}
7474
};
75+
/**
76+
*@param {string} msg message
77+
*@param {DependencyLocation | undefined} loc location
78+
*/
7579
constreportWarning=(msg,loc)=>{
7680
mod.addWarning(newStrictCompatibilityWarning(msg,loc));
7781
};
@@ -90,6 +94,10 @@ class StrictCompatibilityPlugin {
9094
functionDepth--;
9195
};
9296

97+
/**
98+
*@param {import("estree").Pattern | null | undefined} pattern pattern to collect from
99+
*@param {string[]} into array to collect identifiers into
100+
*/
93101
constcollectIdentsFromPattern=(pattern,into)=>{
94102
if(!pattern)return;
95103
switch(pattern.type){
@@ -149,21 +157,21 @@ class StrictCompatibilityPlugin {
149157
if(seen.has(n)){
150158
reportError(
151159
`Duplicate parameter name '${n}' is not allowed in strict mode`,
152-
node.loc
160+
node.loc||undefined
153161
);
154162
}else{
155163
seen.add(n);
156164
}
157165
if(n==="eval"||n==="arguments"){
158166
reportError(
159167
`Using '${n}' as parameter name is not allowed in strict mode`,
160-
node.loc
168+
node.loc||undefined
161169
);
162170
}
163171
if(STRICT_RESERVED_WORDS.has(n)){
164172
reportError(
165173
`Using reserved word '${n}' as parameter name is not allowed in strict mode`,
166-
node.loc
174+
node.loc||undefined
167175
);
168176
}
169177
}
@@ -177,6 +185,9 @@ class StrictCompatibilityPlugin {
177185
*@param {import("./Dependency").DependencyLocation | undefined} loc Source location for diagnostics
178186
*/
179187
constcheckDeclId=(id,loc)=>{
188+
/**
189+
*@param {string} n name to check
190+
*/
180191
constcheckName=(n)=>{
181192
if(n==="eval"||n==="arguments"){
182193
reportError(
@@ -207,6 +218,20 @@ class StrictCompatibilityPlugin {
207218
// unresolvable references via parser.isVariableDefined(name)
208219
// (Annex C / 6.2.5.6, 13.15)
209220

221+
/**
222+
* Type guard for ESTree Node.
223+
*@param {unknown} x value to test
224+
*@returns {x is import("estree").Node} true if the value looks like an ESTree Node
225+
*/
226+
constisNode=(x)=>{
227+
if(typeofx!=="object"||x===null)returnfalse;
228+
constr=/**@type {Record<string, unknown>} */(x);
229+
returntypeofr.type==="string";
230+
};
231+
232+
/**
233+
*@param {import("estree").Node | null | undefined} node node to walk
234+
*/
210235
constwalk=(node)=>{
211236
if(!node||typeofnode.type!=="string")return;
212237
switch(node.type){
@@ -217,7 +242,7 @@ class StrictCompatibilityPlugin {
217242
// ECMA-262 Strict Mode: with statement is prohibited (Annex C / 14.11.1)
218243
reportError(
219244
"with statement is not allowed in strict mode",
220-
node.loc
245+
node.loc||undefined
221246
);
222247
walk(node.object);
223248
walk(node.body);
@@ -231,7 +256,7 @@ class StrictCompatibilityPlugin {
231256
){
232257
reportError(
233258
"Deleting an unqualified identifier is not allowed in strict mode (delete x)",
234-
node.loc
259+
node.loc||undefined
235260
);
236261
}
237262
walk(node.argument);
@@ -246,7 +271,7 @@ class StrictCompatibilityPlugin {
246271
){
247272
reportError(
248273
`Using '${node.argument.name}' as operand of update expression is not allowed in strict mode`,
249-
node.loc
274+
node.loc||undefined
250275
);
251276
}
252277
return;
@@ -257,21 +282,21 @@ class StrictCompatibilityPlugin {
257282
if(n==="eval"||n==="arguments"){
258283
reportError(
259284
`Assignment to '${n}' is not allowed in strict mode`,
260-
node.loc
285+
node.loc||undefined
261286
);
262287
}
263288
// ECMA-262 Strict Mode: Unresolved references cannot be assigned (Annex C / 6.2.5.6, 13.15)
264289
if(!parser.isVariableDefined(n)){
265290
reportError(
266291
`Assignment to undeclared identifier '${n}' is not allowed in strict mode`,
267-
node.loc
292+
node.loc||undefined
268293
);
269294
}
270295
// ECMA-262 Strict Mode: Assignment to read-only global values (Annex C / 13.15)
271296
if(n==="undefined"||n==="Infinity"||n==="NaN"){
272297
reportError(
273298
`Assignment to read-only global '${n}' is not allowed in strict mode`,
274-
node.loc
299+
node.loc||undefined
275300
);
276301
}
277302
}elseif(
@@ -282,8 +307,14 @@ class StrictCompatibilityPlugin {
282307
// Flag assignment to those properties as well.
283308
constobj=node.left.object;
284309
constprop=node.left.computed
285-
?node.left.property&&node.left.property.value
286-
:node.left.property&&node.left.property.name;
310+
?node.left.property&&
311+
/**@type {import("estree").Literal} */(
312+
node.left.property
313+
).value
314+
:node.left.property&&
315+
/**@type {import("estree").Identifier} */(
316+
node.left.property
317+
).name;
287318
if(
288319
obj&&
289320
obj.type==="Identifier"&&
@@ -292,7 +323,7 @@ class StrictCompatibilityPlugin {
292323
){
293324
reportWarning(
294325
`Assignment to 'arguments.${prop}' may throw in strict mode`,
295-
node.loc
326+
node.loc||undefined
296327
);
297328
}
298329
}
@@ -302,7 +333,10 @@ class StrictCompatibilityPlugin {
302333
case"ArrowFunctionExpression":
303334
checkParams(node);
304335
if(node.type!=="ArrowFunctionExpression"){
305-
checkDeclId(node.id,node.loc);
336+
checkDeclId(
337+
node.id,
338+
/**@type {DependencyLocation | undefined} */(node.loc)
339+
);
306340
}
307341
enterFn();
308342
if(node.body){
@@ -316,22 +350,29 @@ class StrictCompatibilityPlugin {
316350
return;
317351
case"ClassDeclaration":
318352
// ECMA-262 Strict Mode: BindingIdentifier must not be eval/arguments even for class names (Annex C / 13.1.1)
319-
checkDeclId(node.id,node.loc);
353+
checkDeclId(
354+
node.id,
355+
/**@type {DependencyLocation | undefined} */(node.loc)
356+
);
320357
if(node.body){
321358
for(constelofnode.body.body)walk(el);
322359
}
323360
return;
324361
case"VariableDeclaration":
325362
for(constdofnode.declarations){
326-
checkDeclId(d.id,d.loc);
363+
checkDeclId(d.id,d.loc||undefined);
327364
if(d.init)walk(d.init);
328365
}
329366
return;
330367
case"MemberExpression":{
331368
constobj=node.object;
332369
constprop=node.computed
333-
?node.property&&node.property.value
334-
:node.property&&node.property.name;
370+
?node.property&&
371+
/**@type {import("estree").Literal} */(node.property)
372+
.value
373+
:node.property&&
374+
/**@type {import("estree").Identifier} */(node.property)
375+
.name;
335376
if(
336377
obj&&
337378
obj.type==="Identifier"&&
@@ -340,7 +381,7 @@ class StrictCompatibilityPlugin {
340381
){
341382
reportWarning(
342383
`'arguments.${prop}' is forbidden in strict mode`,
343-
node.loc
384+
node.loc||undefined
344385
);
345386
}
346387
// NOTE: Accessing Function#caller/arguments on strict functions throws a TypeError at runtime.
@@ -352,7 +393,7 @@ class StrictCompatibilityPlugin {
352393
){
353394
reportWarning(
354395
`Accessing '.${prop}' may be restricted in strict mode`,
355-
node.loc
396+
node.loc||undefined
356397
);
357398
}
358399
walk(node.object);
@@ -364,28 +405,34 @@ class StrictCompatibilityPlugin {
364405
if(functionDepth===0){
365406
reportWarning(
366407
"Top-level 'this' may be undefined in strict mode",
367-
node.loc
408+
node.loc||undefined
368409
);
369410
}
370411
return;
371412
case"CatchClause":
372413
// ECMA-262 Strict Mode: catch parameter must not be eval/arguments (Annex C / 14.15.1)
373414
if(node.param){
374-
checkDeclId(node.param,node.loc);
415+
checkDeclId(
416+
node.param,
417+
/**@type {DependencyLocation | undefined} */(node.loc)
418+
);
375419
}
376420
if(node.body){
377421
for(conststofnode.body.body)walk(st);
378422
}
379423
return;
380424
default:{
381-
for(constkinnode){
382-
constv=node[k];
425+
constrec=/**@type {Record<string, unknown>} */(
426+
/**@type {unknown} */(node)
427+
);
428+
for(constkinrec){
429+
constv=rec[k];
383430
if(!v)continue;
384431
if(Array.isArray(v)){
385432
for(constcofv){
386-
if(c&&typeofc.type==="string")walk(c);
433+
if(isNode(c))walk(c);
387434
}
388-
}elseif(v&&typeofv.type==="string"){
435+
}elseif(isNode(v)){
389436
walk(v);
390437
}
391438
}
@@ -404,42 +451,9 @@ class StrictCompatibilityPlugin {
404451
// These errors are raised at runtime by the engine in strict mode.
405452
// ---------------------------------------------------------------------------
406453

407-
// Numeric literals / string escapes: detect directly from source text
408-
// (most cases are parser-rejected already)
409-
if(src&&typeofsrc==="string"){
410-
// ECMA-262 Strict Mode: Legacy octal numeric literals are prohibited
411-
// - (Annex C / 7-10)
412-
// Legacy octal integer starting with 0 but not 0x/0o/0b (e.g. 0644)
413-
constoctalNum=/(^|[^.\w])0[0-7]+(?![0-9])/m;
414-
// ECMA-262 Strict Mode: NonOctalDecimalIntegerLiteral (e.g. 08, 09) is prohibited
415-
// - (Annex C / 7-10)
416-
constnonOctalDecimalNum=/(^|[^.\w])0[0-9]+(?![0-9])/m;
417-
// ECMA-262 Strict Mode: Legacy octal escape & NonOctalDecimalEscape (\8, \9) are prohibited
418-
// - (Annex C / 11-13)
419-
// Legacy octal escapes like \\1, \\12, \\123 (\\0 alone is allowed)
420-
constoctalEsc=/\\(?:[1-7][0-7]{0,2})/m;
421-
// Non-octal decimal escape sequences: \8 or \9
422-
constnonOctalDecimalEsc=/\\[89]/m;
423-
if(octalNum.test(src)){
424-
reportError(
425-
"Legacy octal numeric literal is not allowed in strict mode (use 0o...)",
426-
/**@type {DependencyLocation} */(mod.loc)
427-
);
428-
}
429-
// Non-octal decimal literal with a leading zero (e.g. 08, 09)
430-
if(!octalNum.test(src)&&nonOctalDecimalNum.test(src)){
431-
reportError(
432-
"Non-octal decimal integer literal with leading zero is not allowed in strict mode (e.g. 08, 09)",
433-
/**@type {DependencyLocation} */(mod.loc)
434-
);
435-
}
436-
if(octalEsc.test(src)||nonOctalDecimalEsc.test(src)){
437-
reportError(
438-
"Legacy/non-octal decimal escape sequences in string literals are not allowed in strict mode",
439-
/**@type {DependencyLocation} */(mod.loc)
440-
);
441-
}
442-
}
454+
// NOTE (Annex C 7–13): Legacy octal numeric literals and legacy/non-octal decimal
455+
// escape sequences are rejected by the parser in strict mode. To keep pack caches
456+
// stable we do not scan raw source; such cases surface as Acorn "Module parse failed".
443457
});
444458
};
445459

‎test/configCases/parsing/strict-compatibility/webpack.config.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
/**@type {import("../../../../").Configuration[]} */
44
module.exports=(()=>{
5+
/**
6+
* Build a data URI JavaScript entry from inline source.
7+
*@param {string} code inline JavaScript source
8+
*@returns {string} data URI for webpack entry
9+
*/
510
constjs=(code)=>`data:text/javascript,${encodeURIComponent(code)}`;
611

712
return[

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp