Skip to content

[labs/analyzer] Don't export (most) helper methods, refactor onto model classes#3254

Merged
kevinpschaaf merged 6 commits intomainfrom
analyzer-isLitElement
Sep 8, 2022
Merged

[labs/analyzer] Don't export (most) helper methods, refactor onto model classes#3254
kevinpschaaf merged 6 commits intomainfrom
analyzer-isLitElement

Conversation

@kevinpschaaf
Copy link
Copy Markdown
Member

@kevinpschaaf kevinpschaaf commented Aug 30, 2022

Fixes #3234 by ensuring that consumers of an analyzer model don't use incompatible helper methods (due to instanceof, caching, etc.) from a separate installation of the analyzer library.

Restricts package exports to index.js, and only exports the Analyzer class + types (and select cross-package-safe helpers). Helpers like isLitElementDeclaration and getModulesWithDeclarations are now class methods on model classes, rather than standalone module exports.

Fixes #3235 by ensuring that consumers of an analyzer model don't use incompatible helper methods (due to instanceof, caching, etc.) from a separate installation of the analyzer library. Restricts package exports to index.js, and only exports the Analyzer class + types (and select cross-package-safe helpers). Helpers like `isLitElementDeclaration` and `getModulesWithDeclarations` are now class methods on model classes, rather than standalone module exports.
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Aug 30, 2022

🦋 Changeset detected

Latest commit: 9cacac2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@lit-labs/analyzer Minor
@lit-labs/gen-wrapper-angular Minor
@lit-labs/gen-wrapper-react Minor
@lit-labs/gen-wrapper-vue Minor
@lit-labs/cli Patch
@lit-labs/gen-utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 30, 2022

📊 Tachometer Benchmark Results

Summary

nop-update

  • lit-html-kitchen-sink: unsure 🔍 -9% - +12% (-2.49ms - +3.19ms)
    this-change vs tip-of-tree

render

  • lit-element-list: 70.87ms - 72.11ms
  • lit-html-kitchen-sink: unsure 🔍 -5% - +6% (-1.52ms - +1.69ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -4% - +0% (-0.42ms - +0.04ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -2% - +0% (-1.33ms - +0.09ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -1% - +1% (-0.73ms - +0.71ms)
    this-change vs tip-of-tree

update

  • lit-element-list: 681.85ms - 686.59ms
  • lit-html-kitchen-sink: unsure 🔍 -3% - +12% (-2.05ms - +10.04ms)
    this-change vs tip-of-tree
  • lit-html-repeat: faster ✔ 0% - 3% (0.16ms - 7.20ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -1% - +1% (-1.52ms - +0.89ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -1% - +0% (-4.31ms - +2.69ms)
    this-change vs tip-of-tree

update-reflect

  • lit-element-list: 694.26ms - 698.80ms
  • reactive-element-list: unsure 🔍 -1% - +2% (-10.82ms - +11.96ms)
    this-change vs tip-of-tree

Results

lit-element-list

render

VersionAvg timevs
70.87ms - 72.11ms-

update

VersionAvg timevs
681.85ms - 686.59ms-

update-reflect

VersionAvg timevs
694.26ms - 698.80ms-
lit-html-kitchen-sink

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
27.90ms - 30.04ms-unsure 🔍
-5% - +6%
-1.52ms - +1.69ms
unsure 🔍
-4% - +5%
-1.11ms - +1.52ms
tip-of-tree
tip-of-tree
27.69ms - 30.08msunsure 🔍
-6% - +5%
-1.69ms - +1.52ms
-unsure 🔍
-5% - +5%
-1.30ms - +1.54ms
previous-release
previous-release
27.99ms - 29.53msunsure 🔍
-5% - +4%
-1.52ms - +1.11ms
unsure 🔍
-5% - +4%
-1.54ms - +1.30ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
82.18ms - 92.14ms-unsure 🔍
-3% - +12%
-2.05ms - +10.04ms
unsure 🔍
-6% - +8%
-5.15ms - +7.08ms
tip-of-tree
tip-of-tree
79.74ms - 86.59msunsure 🔍
-11% - +2%
-10.04ms - +2.05ms
-unsure 🔍
-9% - +2%
-7.96ms - +1.90ms
previous-release
previous-release
82.64ms - 89.74msunsure 🔍
-8% - +6%
-7.08ms - +5.15ms
unsure 🔍
-2% - +10%
-1.90ms - +7.96ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
24.99ms - 29.22ms-unsure 🔍
-9% - +12%
-2.49ms - +3.19ms
unsure 🔍
-14% - +10%
-3.84ms - +2.87ms
tip-of-tree
tip-of-tree
24.87ms - 28.64msunsure 🔍
-12% - +9%
-3.19ms - +2.49ms
-unsure 🔍
-14% - +8%
-4.05ms - +2.38ms
previous-release
previous-release
24.99ms - 30.19msunsure 🔍
-11% - +14%
-2.87ms - +3.84ms
unsure 🔍
-9% - +15%
-2.38ms - +4.05ms
-
lit-html-repeat

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
10.02ms - 10.31ms-unsure 🔍
-4% - +0%
-0.42ms - +0.04ms
unsure 🔍
-3% - +1%
-0.30ms - +0.15ms
tip-of-tree
tip-of-tree
10.18ms - 10.53msunsure 🔍
-0% - +4%
-0.04ms - +0.42ms
-unsure 🔍
-1% - +4%
-0.13ms - +0.37ms
previous-release
previous-release
10.07ms - 10.41msunsure 🔍
-2% - +3%
-0.15ms - +0.30ms
unsure 🔍
-4% - +1%
-0.37ms - +0.13ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
262.92ms - 266.69ms-faster ✔
0% - 3%
0.16ms - 7.20ms
unsure 🔍
-1% - +1%
-2.89ms - +1.99ms
tip-of-tree
tip-of-tree
265.51ms - 271.46msslower ❌
0% - 3%
0.16ms - 7.20ms
-unsure 🔍
-0% - +2%
-0.13ms - +6.59ms
previous-release
previous-release
263.70ms - 266.81msunsure 🔍
-1% - +1%
-1.99ms - +2.89ms
unsure 🔍
-2% - +0%
-6.59ms - +0.13ms
-
lit-html-template-heavy

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
52.64ms - 53.59ms-unsure 🔍
-2% - +0%
-1.33ms - +0.09ms
unsure 🔍
-0% - +2%
-0.22ms - +1.04ms
tip-of-tree
tip-of-tree
53.21ms - 54.25msunsure 🔍
-0% - +3%
-0.09ms - +1.33ms
-slower ❌
1% - 3%
0.36ms - 1.69ms
previous-release
previous-release
52.29ms - 53.12msunsure 🔍
-2% - +0%
-1.04ms - +0.22ms
faster ✔
1% - 3%
0.36ms - 1.69ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
114.92ms - 116.69ms-unsure 🔍
-1% - +1%
-1.52ms - +0.89ms
unsure 🔍
-1% - +1%
-1.20ms - +1.56ms
tip-of-tree
tip-of-tree
115.30ms - 116.94msunsure 🔍
-1% - +1%
-0.89ms - +1.52ms
-unsure 🔍
-1% - +2%
-0.84ms - +1.83ms
previous-release
previous-release
114.57ms - 116.68msunsure 🔍
-1% - +1%
-1.56ms - +1.20ms
unsure 🔍
-2% - +1%
-1.83ms - +0.84ms
-
reactive-element-list

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
51.01ms - 51.81ms-unsure 🔍
-1% - +1%
-0.73ms - +0.71ms
unsure 🔍
-1% - +1%
-0.77ms - +0.48ms
tip-of-tree
tip-of-tree
50.82ms - 52.02msunsure 🔍
-1% - +1%
-0.71ms - +0.73ms
-unsure 🔍
-2% - +1%
-0.91ms - +0.63ms
previous-release
previous-release
51.08ms - 52.03msunsure 🔍
-1% - +1%
-0.48ms - +0.77ms
unsure 🔍
-1% - +2%
-0.63ms - +0.91ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
692.59ms - 697.72ms-unsure 🔍
-1% - +0%
-4.31ms - +2.69ms
unsure 🔍
-0% - +1%
-3.14ms - +4.24ms
tip-of-tree
tip-of-tree
693.59ms - 698.35msunsure 🔍
-0% - +1%
-2.69ms - +4.31ms
-unsure 🔍
-0% - +1%
-2.20ms - +4.93ms
previous-release
previous-release
691.95ms - 697.26msunsure 🔍
-1% - +0%
-4.24ms - +3.14ms
unsure 🔍
-1% - +0%
-4.93ms - +2.20ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
786.62ms - 801.28ms-unsure 🔍
-1% - +2%
-10.82ms - +11.96ms
unsure 🔍
-2% - +1%
-12.38ms - +7.76ms
tip-of-tree
tip-of-tree
784.65ms - 802.10msunsure 🔍
-2% - +1%
-11.96ms - +10.82ms
-unsure 🔍
-2% - +1%
-14.01ms - +8.24ms
previous-release
previous-release
789.35ms - 803.16msunsure 🔍
-1% - +2%
-7.76ms - +12.38ms
unsure 🔍
-1% - +2%
-8.24ms - +14.01ms
-

tachometer-reporter-action v2 for Benchmarks

"lint-staged": "^13.0.3",
"mocha": "^10.0.0",
"prettier": "^2.4.1",
"prettier": "^2.7.1",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed in this PR? Does it change formatting and if so was that applied to all packages?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried updating it because the formatter was just breaking a certain code construct by inserting a random semicolon. I couldn't reproduce it in the prettier REPL, and ended up just changing the code.

We apparently don't include format in CI anymore, because (1) there were a couple of files that weren't formatted, and (2) there were a couple of generated files that were errantly not being excluded from lint (npm run format runs lint first, for some reason). Outside of those changes, I did run format and it doesn't change anything.

): ClassDeclaration => {
return new ClassDeclaration({
name: declaration.name?.text,
name: declaration.name?.text ?? '',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like name being undefined if a class actually doesn't have a name. What was this change for?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes all Declarations now extend a common base class that has name: string (before some were string | undefined). Ultimately I think this is correct since I don't think we need any anonymous declarations in the model -- if an anonymous class/function expression is assigned to a const, we'll use that name in the declaration (I have later versions that do that). Once we support that, this will go away, so I'll add a TODO.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have something like:

customElements.define('my-element, class extends LitElement { ... });

Rare, but possible.

import {IPackageJson as PackageJson} from 'package-json-type';
export {PackageJson};

export type ModuleWithDeclarations<T extends Declaration> = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module already has declarations. Can you add a comment on what this is for? To narrow the declaration type?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the return value from getModulesWithDeclarations, which returns a filtered list of modules containing a certain declaration type AND the filtered list of those declarations (in order to implement getLitElementModules()). This was probably premature factoring, I'll just revert this to getLitElementModules(); I can't really think of a use case for getting modules filtered by a different declaration type.

this.modules = init.modules;
}

getModulesWithDeclarations<T extends Declaration>(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, and commented getLitElementModules

constructor(init: DeclarationInit) {
this.name = init.name;
}
isVariableDeclaration(): this is VariableDeclaration {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we followed the module object pattern like TypeScript, we could also have a bunch of top-level functions for model object, like analyzer.isClassDeclaration(), etc.

@kevinpschaaf kevinpschaaf changed the title Don't export (most) helper methods, refactor onto model classes [labs/analyzer] Don't export (most) helper methods, refactor onto model classes Aug 31, 2022

// Any non-type exports below must be safe to use on objects between multiple
// versions of the analyzer library
export {getImportsStringForReferences} from './lib/model.js';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow on, let's consider putting this on the analyzer itself and passing that around. That way we don't have to be careful about this.

Copy link
Copy Markdown
Member

@sorvell sorvell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one comment to handle as a follow on.

Copy link
Copy Markdown
Collaborator

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still LGTM!

): ClassDeclaration => {
return new ClassDeclaration({
name: declaration.name?.text,
name: declaration.name?.text ?? '',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have something like:

customElements.define('my-element, class extends LitElement { ... });

Rare, but possible.

@kevinpschaaf kevinpschaaf merged commit fc2fd4c into main Sep 8, 2022
@kevinpschaaf kevinpschaaf deleted the analyzer-isLitElement branch September 8, 2022 00:43
This was referenced Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[labs/cli] labs gen command fails to detect lit component when globally installed

3 participants