Skip to content

Commit

Permalink
feat(tools): remove ts-ignore pragmas from stories during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Nov 10, 2021
1 parent 7e24bdf commit c0664e8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,39 @@ describe('migrate-converged-pkg generator', () => {
]),
);
});

it(`should remove @ts-ignore pragmas from all stories`, async () => {
const { paths } = setup({ createDummyStories: true });
append(
tree,
paths.storyOne,
stripIndents`
// https://github.com/microsoft/fluentui/pull/18695#issuecomment-868432982
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { Button } from '@fluentui/react-button';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { Text } from '@fluentui/react-text';
`,
);

await generator(tree, options);

expect(tree.read(paths.storyOne)?.toString('utf-8')).toMatchInlineSnapshot(`
"import * as Implementation from './index';
export const Foo = (props: FooProps) => { return <div>Foo</div>; }\\\\n
import { Button } from '@fluentui/react-button';
import { Text } from '@fluentui/react-text';"
`);
});
});

describe(`e2e config`, () => {
Expand Down Expand Up @@ -1254,7 +1287,7 @@ function append(tree: Tree, filePath: string, content: string) {
filePath,
stripIndents`
${tree.read(filePath)?.toString('utf-8')}\n
${content};
${content}
`,
);

Expand Down
33 changes: 33 additions & 0 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) {

return json;
});

removeTsIgnorePragmas();
}

if (sbAction === 'remove') {
Expand Down Expand Up @@ -621,6 +623,37 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) {
});
}

function removeTsIgnorePragmas() {
const stories: string[] = [];
visitNotIgnoredFiles(tree, options.paths.sourceRoot, treePath => {
if (treePath.includes('.stories.')) {
stories.push(treePath);
}
});

stories.forEach(storyPath => {
const content = tree.read(storyPath)?.toString('utf-8');

if (!content) {
throw new Error('story file has no code');
}

let updatedContent = content.replace(/\/\/\s+@ts-ignore/g, '');
updatedContent = updatedContent.replace(
/\/\/\s+eslint-disable-next-line\s+@typescript-eslint\/ban-ts-comment/g,
'',
);
updatedContent = updatedContent.replace(
/\/\/\s+https:\/\/github\.com\/microsoft\/fluentui\/pull\/18695#issuecomment-868432982/g,
'',
);

tree.write(storyPath, updatedContent);
});

return tree;
}

return tree;
}

Expand Down

0 comments on commit c0664e8

Please sign in to comment.