Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source-loader: Fix default exports of type TSAsExpression #12099

Merged
merged 2 commits into from Aug 19, 2020

Conversation

petermikitsh
Copy link
Contributor

Issue: #11994

What I did

Added support for TypeScript as <Type> syntax:

Screen Shot 2020-08-17 at 11 00 40 PM

The logic for parsing story sourcecode is fairly surgical. The existing implementation was too strict to permit the TSAsExpression type.

How to test

  • Is this testable with Jest or Chromatic screenshots? Unsure
  • Does this need a new example in the kitchen sink apps? Unsure. I think this could be demoed in the existing react-ts example. (I have those changes locally & can push them. If this sounds good, let me know.)
  • Does this need an update to the documentation? No

If your answer is yes to any of these, please make sure to include it in your PR.

Notes: this is my first PR-- open to any feedback! TIA.

@petermikitsh
Copy link
Contributor Author

petermikitsh commented Aug 18, 2020

A little more context. So basically, default exports as object expressions vs TsAsExpressions are nearly identical AST's, with slightly different shapes. This changeset helps accommodate both. Sample AST's in here:

ExportDefaultDeclaration: ObjectExpression vs TSAsExpression
{
  type: 'ExportDefaultDeclaration',
  declaration: {
    type: 'ObjectExpression',
    properties: [
      {
        type: 'Property',
        key: {
          type: 'Identifier',
          name: 'component',
          range: [ 131, 140 ],
          loc: {
            start: { line: 5, column: 17 },
            end: { line: 5, column: 26 }
          }
        },
        value: {
          type: 'Identifier',
          name: 'Button',
          range: [ 142, 148 ],
          loc: {
            start: { line: 5, column: 28 },
            end: { line: 5, column: 34 }
          }
        },
        computed: false,
        method: false,
        shorthand: false,
        kind: 'init',
        range: [ 131, 148 ],
        loc: {
          start: { line: 5, column: 17 },
          end: { line: 5, column: 34 }
        }
      },
      {
        type: 'Property',
        key: {
          type: 'Identifier',
          name: 'title',
          range: [ 150, 155 ],
          loc: {
            start: { line: 5, column: 36 },
            end: { line: 5, column: 41 }
          }
        },
        value: {
          type: 'Literal',
          raw: "'Examples / Button'",
          value: 'Examples / Button',
          range: [ 157, 176 ],
          loc: {
            start: { line: 5, column: 43 },
            end: { line: 5, column: 62 }
          }
        },
        computed: false,
        method: false,
        shorthand: false,
        kind: 'init',
        range: [ 150, 176 ],
        loc: {
          start: { line: 5, column: 36 },
          end: { line: 5, column: 62 }
        }
      }
    ],
    range: [ 129, 178 ],
    loc: { start: { line: 5, column: 15 }, end: { line: 5, column: 64 } }
  },
  range: [ 114, 192 ],
  loc: { start: { line: 5, column: 0 }, end: { line: 5, column: 78 } },
  start: 114,
  end: 192
}
{
  type: 'ExportDefaultDeclaration',
  declaration: {
    type: 'TSAsExpression',
    expression: {
      type: 'ObjectExpression',
      properties: [
        {
          type: 'Property',
          key: {
            type: 'Identifier',
            name: 'component',
            range: [ 717, 726 ],
            loc: {
              start: { line: 13, column: 17 },
              end: { line: 13, column: 26 }
            }
          },
          value: {
            type: 'Identifier',
            name: 'Button',
            range: [ 728, 734 ],
            loc: {
              start: { line: 13, column: 28 },
              end: { line: 13, column: 34 }
            }
          },
          computed: false,
          method: false,
          shorthand: false,
          kind: 'init',
          range: [ 717, 734 ],
          loc: {
            start: { line: 13, column: 17 },
            end: { line: 13, column: 34 }
          }
        },
        {
          type: 'Property',
          key: {
            type: 'Identifier',
            name: 'title',
            range: [ 736, 741 ],
            loc: {
              start: { line: 13, column: 36 },
              end: { line: 13, column: 41 }
            }
          },
          value: {
            type: 'Literal',
            raw: "'Examples / Button'",
            value: 'Examples / Button',
            range: [ 743, 762 ],
            loc: {
              start: { line: 13, column: 43 },
              end: { line: 13, column: 62 }
            }
          },
          computed: false,
          method: false,
          shorthand: false,
          kind: 'init',
          range: [ 736, 762 ],
          loc: {
            start: { line: 13, column: 36 },
            end: { line: 13, column: 62 }
          }
        }
      ],
      range: [ 715, 764 ],
      loc: {
        start: { line: 13, column: 15 },
        end: { line: 13, column: 64 }
      }
    },
    typeAnnotation: {
      type: 'TSTypeReference',
      typeName: {
        type: 'Identifier',
        name: 'Meta',
        range: [ 768, 772 ],
        loc: {
          start: { line: 13, column: 68 },
          end: { line: 13, column: 72 }
        }
      },
      typeParameters: undefined,
      range: [ 768, 772 ],
      loc: {
        start: { line: 13, column: 68 },
        end: { line: 13, column: 72 }
      }
    },
    range: [ 715, 772 ],
    loc: { start: { line: 13, column: 15 }, end: { line: 13, column: 72 } }
  },
  range: [ 700, 773 ],
  loc: { start: { line: 13, column: 0 }, end: { line: 13, column: 73 } },
  start: 700,
  end: 773
}

A visual summary of the shape differences between the ASTs:

{	
  type: 'ExportDefaultDeclaration',	
  declaration: {	
-   type: 'ObjectExpression',
+   type: 'TSAsExpression',
-   properties: [ ... ],
+   expression: {
+     type: 'ObjectExpression',
+     properties: [ ... ],
+   }
  }
}

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

LGTM! Any chance we can get a test case to go along with this?

@petermikitsh
Copy link
Contributor Author

@shilman I have updated the react-ts examples project

@shilman shilman changed the title fix(storysource): support default exports of type TSAsExpression Source-loader: Fix default exports of type TSAsExpression Aug 19, 2020
@shilman shilman merged commit 231886e into storybookjs:next Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants