Skip to content

Provide a lint to not use identical when the arguments are JS types #60224

Open
@gmpassos

Description

@gmpassos

The code below, when run in the browser (Chrome), shows that identical behaves differently in JS and Wasm:

import 'package:web/web.dart';

void main() {
  var output = document.querySelector('#output'); // a div in the index.html

  var div1 = HTMLDivElement()
    ..id = 'div1'
    ..textContent = 'aaa';

  var div2 = HTMLDivElement()
    ..id = 'div2'
    ..textContent = 'aaa';

  var div1a = div1;

  print('div1 == div2 = ${div1 == div2}');
  print('identical(div1, div2) = ${identical(div1, div2)}');
  print('div1 == div1a = ${div1 == div1a}');
  print('identical(div1, div1a) = ${identical(div1, div1a)}');

  output!.appendChild(div1);
  output.appendChild(div2);

  print('-- From querySelector:');

  var div1B = output.querySelector('#div1') as HTMLDivElement;
  var div2B = output.querySelector('#div2') as HTMLDivElement;

  print('div1 == div1B = ${div1 == div1B}');
  print('identical(div1, div1B) = ${identical(div1, div1B)}');
  print('div2 == div2B = ${div2 == div2B}');
  print('identical(div2, div2B) = ${identical(div2, div2B)}');
}

Output dart2js:

div1 == div2 = false
identical(div1, div2) = false
div1 == div1a = true
identical(div1, div1a) = true
-- From querySelector:
div1 == div1B = true
identical(div1, div1B) = true
div2 == div2B = true
identical(div2, div2B) = true

Output dart2wasm:

div1 == div2 = false
identical(div1, div2) = false
div1 == div1a = true
identical(div1, div1a) = true
-- From querySelector:
div1 == div1B = true
identical(div1, div1B) = false
div2 == div2B = true
identical(div2, div2B) = false

My suggestion is to at least document this behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.devexp-linterIssues with the analyzer's support for the linter packageweb-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions