Monday, May 5, 2014

Query is Not Deprecated in Dart


Look, I know that Dart Editor says that query() is deprecated in Dart, but it's really not.

I mean, sure it's deprecated, but it's deprecated so that a DOM method of the same name that returns nearly the same thing can replace it. And, since the newer version will (most likely) return an active node list, it'll be more what developers tend to expect and use less memory than the current version. So I stick with query() in my various books and will continue to do so until the deprecation is completed and the new an improved query() arrives.

That leaves me with a bit of a bother, however. What is the best practice for ignoring query() / queryAll() “deprecation” warnings? In Dart for Hipsters, I had been ignoring all hints in my test_runner.sh:
# Static type analysis
echo
echo "Static type analysis (expecting 4 warnings)..."

results=$(dartanalyzer --no-hints test.dart 2>&1)
I think that is probably a case of throwing the baby out with the bath water. So instead, I opt for an approach that we have found works nicely in the ICE Code Editor test suite—we grep-ignore hints that are OK which should leave me with no hints. In the book's test quite, this looks something like:
echo "Static type analysis..."

results=$(dartanalyzer test.dart 2>&1)
results_ignoring_ok_deprecations=$(
  echo "$results" | \
    grep -v "'query' is deprecated" | \
    grep -v "'queryAll' is deprecated" | \
    grep -v "hints found.$"
)
echo "$results_ignoring_ok_deprecations"
The results variable name, results_ignoring_ok_deprecations is a bit, but it was the best that we could come up with—and all agreed that a long, descriptive name was better than the alternative.

Since I am intentionally writing some broken code in Dart for Hipsters, I also use this as an opportunity to ignore warnings in specific files from specific lines. The static dartanalyzer code thus becomes:
echo "Static type analysis..."

results=$(dartanalyzer test.dart 2>&1)
results_ignoring_ok_deprecations=$(
  echo "$results" | \
    grep -v "'query' is deprecated" | \
    grep -v "'queryAll' is deprecated" | \

    # Known / intentional errors:
    grep -v "code/primitives/types.dart, line 34" | \
    grep -v "code/primitives/types.dart, line 42" | \
    grep -v "code/varying_the_behavior/test/calling_methods_from_no_such_method.dart, line 22" | \
    grep -v "code/varying_the_behavior/test/calling_methods_from_no_such_method.dart, line 26" | \

    grep -v "hints found.$"
)
echo "$results_ignoring_ok_deprecations"


count=$(echo "$results_ignoring_ok_deprecations" | wc -l)
if [[ "$count" != "1" ]]
then
  exit 1
fi

echo
echo "Looks good!"
I feel much better about that approach than the previous baby and bath water approach. Now, if you'll excuse me, it seems that I have two dozen or so unused imports in my code...


Day #55

No comments:

Post a Comment