This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from styled-components/refactor
Refactor
- Loading branch information
Showing
3 changed files
with
66 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Parsing helpers | ||
*/ | ||
|
||
const parseImports = (node, currentNames) => { | ||
const names = Object.assign({}, currentNames) | ||
const imports = node.specifiers.filter((specifier) => ( | ||
specifier.type === 'ImportDefaultSpecifier' | ||
|| specifier.type === 'ImportSpecifier' | ||
)) | ||
|
||
imports.forEach((singleImport) => { | ||
if (singleImport.imported) { | ||
// Is helper method | ||
names[singleImport.imported.name] = singleImport.local.name | ||
} else { | ||
// Is default import | ||
names.default = singleImport.local.name | ||
} | ||
}) | ||
|
||
return names | ||
} | ||
|
||
const getSourceMap = (fullCSS, fragmentCSS, startInSource) => { | ||
const correction = {} | ||
// Save which line in the full CSS is which line in the source | ||
const fullCSSLength = fullCSS.split(/\n/).length | ||
const currentCSSLength = fragmentCSS.split(/\n/).length | ||
const currentCSSStart = (fullCSSLength - currentCSSLength) + 1 | ||
// eslint-disable-next-line no-plusplus | ||
for (let i = 0; i < currentCSSLength + 1; i++) { | ||
correction[currentCSSStart + i] = startInSource + i | ||
} | ||
return correction | ||
} | ||
|
||
exports.parseImports = parseImports | ||
exports.getSourceMap = getSourceMap |