Skip to content

Commit

Permalink
Ensure rules work when flow pragma is in multi line comment blocks (#58)
Browse files Browse the repository at this point in the history
* fix flow strict multi line

* handle flow new lines the same way

* also check that /* */ style blocks are handled too

* clean up eslint
  • Loading branch information
Brianzchen committed May 7, 2024
1 parent b9def79 commit 0d0d624
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/rules/newlineAfterFlowAnnotation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import _ from 'lodash';

const looksLikeFlowFileAnnotation = (comment) => /@(?:no)?flo/ui.test(comment);
const looksLikeFlowFileAnnotation = (comment) => /^\s*.*@(?:no)?flow(.|\s)*/u.test(comment);

const schema = [
{
Expand All @@ -19,8 +17,7 @@ const create = (context) => {
Program(node) {
const sourceCode = context.getSourceCode();

const potentialFlowFileAnnotation = _.find(
context.getSourceCode().getAllComments(),
const potentialFlowFileAnnotation = sourceCode.getAllComments().find(
(comment) => looksLikeFlowFileAnnotation(comment.value),
);

Expand Down
2 changes: 1 addition & 1 deletion src/rules/noFlowSuppressionsInStrictFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Rule$Create } from 'eslint';

import { suppressionTypes } from '../utilities';

const FLOW_STRICT_MATCHER = /^\s*@(?:no)?flow\s*strict(?:-local)?\s*$/u;
const FLOW_STRICT_MATCHER = /^\s*.*@(?:no)?flow\s*strict(?:-local)?\s*.*/u;

const isStrictFlowFile = (context) => context
.getAllComments()
Expand Down
1 change: 0 additions & 1 deletion src/utilities/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @flow
// eslint-disable-next-line import/no-namespace
import * as spacingFixers from './spacingFixers';

export {
Expand Down
26 changes: 26 additions & 0 deletions tests/rules/assertions/newlineAfterFlowAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export default {
options: ['never'],
output: '// @flow\n',
},
{
code: `/*
* @flow
*
* something multi lined
*/
const text: string = 42;`,
errors: [{ message: 'Expected newline after flow annotation' }],
output: `/*
* @flow
*
* something multi lined
*/
const text: string = 42;`,
},
],
valid: [
{
Expand All @@ -37,5 +53,15 @@ export default {
code: '// @flow\nimport Foo from \'./foo\';',
options: ['never'],
},
{
code: `/*
* @flow
*
* something multi lined
*/
const text: string = 42;`,
options: ['always'],
},
],
};
17 changes: 17 additions & 0 deletions tests/rules/assertions/noFlowSuppressionsInStrictFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ export default {
invalid('// @flow strict\n\n// $FlowFixMe\nconst text: string = 42;'),
invalid('// @flow strict-local\n\n// $FlowFixMe\nconst text: string = 42;'),
invalid('// @flow strict\n\n// $FlowExpectedError[xxx]\nconst text: string = 42;'),
invalid('/* @flow strict */\n\n// $FlowExpectedError[xxx]\nconst text: string = 42;'),
invalid(`/*
* @flow strict
*
* something multi lined
*/
// $FlowExpectedError[xxx]
const text: string = 42;`),
invalid(`/*
* @flow strict
*
* something multi lined
*/
/* $FlowIgnore[xxx] */
const text: string = 42;`),
invalid(
'// @flow strict\n\n// $FlowFixMe\nconst text: string = 42;',
{
Expand Down

0 comments on commit 0d0d624

Please sign in to comment.