Skip to content

Commit

Permalink
warn about duplicate attributes in jsx elements
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 25, 2022
1 parent 8c6c39a commit 98ab5e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
14 changes: 8 additions & 6 deletions internal/bundler/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5412,14 +5412,14 @@ func TestDuplicatePropertyWarning(t *testing.T) {
import './outside-node-modules'
import 'inside-node-modules'
`,
"/outside-node-modules/index.js": `
console.log({ a: 1, a: 2 })
"/outside-node-modules/index.jsx": `
console.log({ a: 1, a: 2 }, <div a2 a2={3}/>)
`,
"/outside-node-modules/package.json": `
{ "b": 1, "b": 2 }
`,
"/node_modules/inside-node-modules/index.js": `
console.log({ c: 1, c: 2 })
"/node_modules/inside-node-modules/index.jsx": `
console.log({ c: 1, c: 2 }, <div c2 c2={3}/>)
`,
"/node_modules/inside-node-modules/package.json": `
{ "d": 1, "d": 2 }
Expand All @@ -5430,8 +5430,10 @@ func TestDuplicatePropertyWarning(t *testing.T) {
Mode: config.ModeBundle,
AbsOutputDir: "/out",
},
expectedScanLog: `outside-node-modules/index.js: WARNING: Duplicate key "a" in object literal
outside-node-modules/index.js: NOTE: The original key "a" is here:
expectedScanLog: `outside-node-modules/index.jsx: WARNING: Duplicate key "a" in object literal
outside-node-modules/index.jsx: NOTE: The original key "a" is here:
outside-node-modules/index.jsx: WARNING: Duplicate "a2" attribute in JSX element
outside-node-modules/index.jsx: NOTE: The original "a2" attribute is here:
outside-node-modules/package.json: WARNING: Duplicate key "b" in object literal
outside-node-modules/package.json: NOTE: The original key "b" is here:
`,
Expand Down
16 changes: 11 additions & 5 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,17 @@ console.log(123);
================================================================================
TestDuplicatePropertyWarning
---------- /out/entry.js ----------
// outside-node-modules/index.js
console.log({ a: 1, a: 2 });

// node_modules/inside-node-modules/index.js
console.log({ c: 1, c: 2 });
// outside-node-modules/index.jsx
console.log({ a: 1, a: 2 }, /* @__PURE__ */ React.createElement("div", {
a2: true,
a2: 3
}));

// node_modules/inside-node-modules/index.jsx
console.log({ c: 1, c: 2 }, /* @__PURE__ */ React.createElement("div", {
c2: true,
c2: 3
}));

================================================================================
TestDynamicImportWithExpressionCJS
Expand Down
20 changes: 20 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,26 @@ func (p *parser) parseJSXElement(loc logger.Loc) js_ast.Expr {
break parseAttributes
}
}

// Check for and warn about duplicate attributes
if len(properties) > 1 && !p.suppressWarningsAboutWeirdCode {
keys := make(map[string]logger.Loc)
for _, property := range properties {
if property.Kind != js_ast.PropertySpread {
if str, ok := property.Key.Data.(*js_ast.EString); ok {
key := helpers.UTF16ToString(str.Value)
if prevLoc, ok := keys[key]; ok {
r := js_lexer.RangeOfIdentifier(p.source, property.Key.Loc)
p.log.AddIDWithNotes(logger.MsgID_JS_DuplicateObjectKey, logger.Warning, &p.tracker, r,
fmt.Sprintf("Duplicate %q attribute in JSX element", key),
[]logger.MsgData{p.tracker.MsgData(js_lexer.RangeOfIdentifier(p.source, prevLoc),
fmt.Sprintf("The original %q attribute is here:", key))})
}
keys[key] = property.Key.Loc
}
}
}
}
}

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

0 comments on commit 98ab5e1

Please sign in to comment.