Skip to content

Commit 98ab5e1

Browse files
committed
warn about duplicate attributes in jsx elements
1 parent 8c6c39a commit 98ab5e1

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

internal/bundler/bundler_default_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5412,14 +5412,14 @@ func TestDuplicatePropertyWarning(t *testing.T) {
54125412
import './outside-node-modules'
54135413
import 'inside-node-modules'
54145414
`,
5415-
"/outside-node-modules/index.js": `
5416-
console.log({ a: 1, a: 2 })
5415+
"/outside-node-modules/index.jsx": `
5416+
console.log({ a: 1, a: 2 }, <div a2 a2={3}/>)
54175417
`,
54185418
"/outside-node-modules/package.json": `
54195419
{ "b": 1, "b": 2 }
54205420
`,
5421-
"/node_modules/inside-node-modules/index.js": `
5422-
console.log({ c: 1, c: 2 })
5421+
"/node_modules/inside-node-modules/index.jsx": `
5422+
console.log({ c: 1, c: 2 }, <div c2 c2={3}/>)
54235423
`,
54245424
"/node_modules/inside-node-modules/package.json": `
54255425
{ "d": 1, "d": 2 }
@@ -5430,8 +5430,10 @@ func TestDuplicatePropertyWarning(t *testing.T) {
54305430
Mode: config.ModeBundle,
54315431
AbsOutputDir: "/out",
54325432
},
5433-
expectedScanLog: `outside-node-modules/index.js: WARNING: Duplicate key "a" in object literal
5434-
outside-node-modules/index.js: NOTE: The original key "a" is here:
5433+
expectedScanLog: `outside-node-modules/index.jsx: WARNING: Duplicate key "a" in object literal
5434+
outside-node-modules/index.jsx: NOTE: The original key "a" is here:
5435+
outside-node-modules/index.jsx: WARNING: Duplicate "a2" attribute in JSX element
5436+
outside-node-modules/index.jsx: NOTE: The original "a2" attribute is here:
54355437
outside-node-modules/package.json: WARNING: Duplicate key "b" in object literal
54365438
outside-node-modules/package.json: NOTE: The original key "b" is here:
54375439
`,

internal/bundler/snapshots/snapshots_default.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,17 @@ console.log(123);
552552
================================================================================
553553
TestDuplicatePropertyWarning
554554
---------- /out/entry.js ----------
555-
// outside-node-modules/index.js
556-
console.log({ a: 1, a: 2 });
557-
558-
// node_modules/inside-node-modules/index.js
559-
console.log({ c: 1, c: 2 });
555+
// outside-node-modules/index.jsx
556+
console.log({ a: 1, a: 2 }, /* @__PURE__ */ React.createElement("div", {
557+
a2: true,
558+
a2: 3
559+
}));
560+
561+
// node_modules/inside-node-modules/index.jsx
562+
console.log({ c: 1, c: 2 }, /* @__PURE__ */ React.createElement("div", {
563+
c2: true,
564+
c2: 3
565+
}));
560566

561567
================================================================================
562568
TestDynamicImportWithExpressionCJS

internal/js_parser/js_parser.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,6 +4536,26 @@ func (p *parser) parseJSXElement(loc logger.Loc) js_ast.Expr {
45364536
break parseAttributes
45374537
}
45384538
}
4539+
4540+
// Check for and warn about duplicate attributes
4541+
if len(properties) > 1 && !p.suppressWarningsAboutWeirdCode {
4542+
keys := make(map[string]logger.Loc)
4543+
for _, property := range properties {
4544+
if property.Kind != js_ast.PropertySpread {
4545+
if str, ok := property.Key.Data.(*js_ast.EString); ok {
4546+
key := helpers.UTF16ToString(str.Value)
4547+
if prevLoc, ok := keys[key]; ok {
4548+
r := js_lexer.RangeOfIdentifier(p.source, property.Key.Loc)
4549+
p.log.AddIDWithNotes(logger.MsgID_JS_DuplicateObjectKey, logger.Warning, &p.tracker, r,
4550+
fmt.Sprintf("Duplicate %q attribute in JSX element", key),
4551+
[]logger.MsgData{p.tracker.MsgData(js_lexer.RangeOfIdentifier(p.source, prevLoc),
4552+
fmt.Sprintf("The original %q attribute is here:", key))})
4553+
}
4554+
keys[key] = property.Key.Loc
4555+
}
4556+
}
4557+
}
4558+
}
45394559
}
45404560

45414561
// People sometimes try to use the output of "JSON.stringify()" as a JSX

0 commit comments

Comments
 (0)