Skip to content

Commit

Permalink
Take addition and deletion sign into account when building regex (Oct…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck authored Dec 6, 2017
1 parent ecbee44 commit 07dfc9f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 15 additions & 8 deletions packages/helper-grammar-regex-collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const captureJsQuotedWord = regex`
['"\`] # end quote
`;

const diffSigns = regex`
^[\+\-]?
`;

const importMembers = regex`[\r\n\s\w{},*\$]*`;
const from = regex`\s from \s`;

Expand Down Expand Up @@ -70,6 +74,7 @@ export const DOCKER_FROM = regex`
`;

export const VIM_PLUGIN = regex`
${diffSigns}
(
(
(Neo)?
Expand All @@ -83,20 +88,21 @@ export const VIM_PLUGIN = regex`
`;

export const RUST_CRATE = regex`
^ (extern \s crate | use)
${diffSigns}
(extern \s crate | use)
\s
(?<$1>[^:;\s]+)
`;

export const PYTHON_IMPORT = regex`
^\s*
${diffSigns}
(import|from)
\s
(?<$1>[^\s]*)
`;

export const REQUIREMENTS_TXT = regex`
^\s*
${diffSigns}
(?<$1>[\w-]+) # the package name
(
\s*
Expand All @@ -107,20 +113,21 @@ export const REQUIREMENTS_TXT = regex`
`;

export const HASKELL_IMPORT = regex`
((^|\s)import\s+(qualified\s)?)
${diffSigns}
(import\s+(qualified\s)?)
(?<$1> [A-Z][\w.]+)
`;

export const CSS_IMPORT = regex`
^\s*
${diffSigns}
@import
\s
((url|URL)\()?
${captureQuotedWord}
`;

export const LESS_IMPORT = regex`
^\s*
${diffSigns}
@import
\s
( \([a-z, ]+\) \s+ )? # http://lesscss.org/features/#import-options
Expand All @@ -129,7 +136,7 @@ export const LESS_IMPORT = regex`
`;

export const HTML_IMPORT = regex`
^\s*
${diffSigns}
<link
\s
rel="import"
Expand All @@ -155,7 +162,7 @@ export const NET_PACKAGE = regex`
`;

export const JAVA_IMPORT = regex`
^\s*
${diffSigns}
import
\s
(?<$1>
Expand Down
13 changes: 12 additions & 1 deletion packages/helper-grammar-regex-collection/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,20 @@ function fixturesIterator(fixturesList, next) {
});
}

function addModifiedLines(valid) {
const [text, expected] = valid[0];
const diffLines = [[`-${text}`, expected], [`+${text}`, expected]];

return [].concat([], diffLines, valid);
}

describe('helper-grammar-regex-collection', () => {
Object.keys(fixtures).forEach(grammar => {
const { valid, invalid } = fixtures[grammar];
const spec = fixtures[grammar];

const { invalid } = spec;
const valid = addModifiedLines(spec.valid);

let regexes = REGEX[grammar];

// Help ensure that `regexes` is a function that returns an array of RegExp
Expand Down

0 comments on commit 07dfc9f

Please sign in to comment.