-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change comment recognition and replace to regexp-based
- Loading branch information
Showing
46 changed files
with
875 additions
and
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ Desktop.ini | |
|
||
# Build files | ||
dist | ||
build | ||
coverage | ||
lib/*.js | ||
test/*.js | ||
|
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 |
---|---|---|
@@ -1,24 +1,11 @@ | ||
import * as gonzales from 'gonzales-pe' | ||
import { generateBlockCommentContent } from './util' | ||
import { generateBlockCommentContent, removeOldCopyrightWithinBlockComment, EOL } from './util' | ||
|
||
function render(source: string, template: string, syntax = 'css') { | ||
const parseTree = gonzales.parse(source, { | ||
syntax, | ||
}) | ||
function render(source: string, template: string) { | ||
const desiredComment = `/*${generateBlockCommentContent(template)}*/${EOL + EOL}` | ||
|
||
const commentNode = gonzales.createNode({ | ||
syntax, | ||
type: 'multilineComment', | ||
content: generateBlockCommentContent(template), | ||
}) | ||
const blankLineNode = gonzales.createNode({ | ||
type: 'space', | ||
content: '\n\n', | ||
}) | ||
parseTree.insert(0, blankLineNode) | ||
parseTree.insert(0, commentNode) | ||
source = removeOldCopyrightWithinBlockComment(source) | ||
|
||
return parseTree.toString() | ||
return desiredComment + source.trimLeft() | ||
} | ||
|
||
export { render } |
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 |
---|---|---|
@@ -1,44 +1,11 @@ | ||
import * as recast from 'recast' | ||
import { generateBlockCommentContent } from './util' | ||
import { generateBlockCommentContent, removeOldCopyrightWithinBlockComment, EOL } from './util' | ||
|
||
const builders = recast.types.builders | ||
function render(source: string, template: string) { | ||
const desiredComment = `/*${generateBlockCommentContent(template)}*/${EOL + EOL}` | ||
|
||
function parse(source: string, parserType = 'babel') { | ||
return recast.parse(source, { | ||
parser: require(`recast/parsers/${parserType}`), | ||
}) | ||
} | ||
|
||
function print(ast: any) { | ||
return recast.print(ast, { | ||
lineTerminator: '\n', | ||
}).code | ||
} | ||
|
||
function appendCopyright(ast: any, crText: string) { | ||
const comment = builders.commentBlock(generateBlockCommentContent(crText)) | ||
|
||
const firstStatement = ast.program.body[0] | ||
|
||
if (firstStatement) { | ||
firstStatement.comments = firstStatement.leadingComments || [] | ||
|
||
firstStatement.comments.unshift(comment) | ||
} | ||
|
||
return ast | ||
} | ||
|
||
function render(source: string, template: string, parserType?: 'babel' | 'typescript') { | ||
const ast = parse(source, parserType) | ||
|
||
const alteredAst = appendCopyright(ast, template) | ||
|
||
return addBlankLineBetweenCopyrightAndCode(print(alteredAst)) | ||
} | ||
source = removeOldCopyrightWithinBlockComment(source) | ||
|
||
function addBlankLineBetweenCopyrightAndCode(fileString: string) { | ||
return fileString.replace(/\*\/$/m, '*/\n') | ||
return desiredComment + source.trimLeft() | ||
} | ||
|
||
export { render } |
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 |
---|---|---|
@@ -1,9 +1,49 @@ | ||
function generateBlockCommentContent(text: string, lineChar = '*', needTopChar = true) { | ||
return `${needTopChar ? lineChar : ''}\n${text | ||
export const EOL = '\n' | ||
|
||
export function generateBlockCommentContent(text: string, lineChar = '*', needTopChar = true) { | ||
return `${needTopChar ? lineChar : ''}${EOL}${text | ||
.trim() | ||
.split('\n') | ||
.split(EOL) | ||
.map(line => ` ${lineChar} ${line}`.trimRight()) | ||
.join('\n')}\n ` | ||
.join(EOL)}${EOL} ` | ||
} | ||
|
||
export function removeOldCopyrightWithinBlockComment(source: string) { | ||
return removeOldCopyright(source, blockCommentRegexp) | ||
} | ||
|
||
export function removeOldCopyrightWithinXML(source: string) { | ||
return removeOldCopyright(source, xmlCommentRegexp) | ||
} | ||
|
||
export function removeOldCopyrightWithinYAML(source: string) { | ||
return removeOldCopyright(source, yamlMultilineCommentRegexp) | ||
} | ||
|
||
export { generateBlockCommentContent } | ||
export function removeOldCopyright(source: string, commentRegexp: RegExp, cpRegExp = copyrightRegexp) { | ||
if (cpRegExp.test(source)) { | ||
let matches | ||
const existingVersions = [] | ||
const input = source.trim() | ||
// tslint:disable-next-line:no-conditional-assignment | ||
while (matches = commentRegexp.exec(input)) { | ||
// find out the copyright comment | ||
if (cpRegExp.test(matches[0])) { | ||
existingVersions.push(matches[0]) | ||
} | ||
} | ||
|
||
// remove all existing copyrights | ||
existingVersions.forEach(item => source = source.replace(item, EOL + EOL)) | ||
} | ||
|
||
return source | ||
} | ||
|
||
export const copyrightRegexp = /copyright/i | ||
|
||
export const xmlCommentRegexp = /\s*<!--([\s\S]*?)-->\s*/g | ||
|
||
export const blockCommentRegexp = /\s*\/\*\*\s*\n([\S\s]*?)\*\/\s*/g | ||
|
||
export const yamlMultilineCommentRegexp = /\s*(#\s*[^\n]+\n){2,}\s*/g |
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 |
---|---|---|
@@ -1,19 +1,11 @@ | ||
import { generateBlockCommentContent } from './util' | ||
|
||
const xmlCommentRegexp = /$(<!--([\s\S]*?)-->)/g | ||
import { generateBlockCommentContent, EOL, removeOldCopyrightWithinXML } from './util' | ||
|
||
function render(source: string, template: string) { | ||
const comment = `<!--${generateBlockCommentContent(template, '-', false)}-->\n\n` | ||
|
||
const matches = xmlCommentRegexp.exec(source.trim()) | ||
|
||
let result | ||
const desiredComment = `<!--${generateBlockCommentContent(template, '-', false)}-->${EOL + EOL}` | ||
|
||
if (!matches) { | ||
result = comment + source | ||
} | ||
source = removeOldCopyrightWithinXML(source) | ||
|
||
return result | ||
return desiredComment + source.trimLeft() | ||
} | ||
|
||
export { render } |
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 |
---|---|---|
@@ -1,20 +1,11 @@ | ||
import * as yaml from 'yaml' | ||
import { EOL, removeOldCopyrightWithinYAML } from './util'; | ||
|
||
function render(source: string, template: string) { | ||
const templateLines = template.split('\n') | ||
const comment = templateLines.map(line => ` ${line}`.trimRight()).join('\n') | ||
const desiredComment = template.split('\n').map(line => `# ${line}`.trimRight()).join(EOL) + EOL + EOL | ||
|
||
const doc = yaml.parseDocument(source, { | ||
keepCstNodes: true, | ||
}) | ||
source = removeOldCopyrightWithinYAML(source) | ||
|
||
doc.commentBefore = comment | ||
|
||
const preout = doc.toString() | ||
|
||
const lastTemplateLine = templateLines[templateLines.length - 1] | ||
|
||
return preout.replace(lastTemplateLine, `${lastTemplateLine}\n`) | ||
return desiredComment + source.trimLeft() | ||
} | ||
|
||
export { render } |
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,25 @@ | ||
/** | ||
* Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
* Unauthorized copying of this file, via any medium is strictly prohibited | ||
* Proprietary and confidential | ||
* Written by John Doe <jdoe@heaven.com>, 1984 | ||
* | ||
* From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
*/ | ||
|
||
/** | ||
* Lorem Ipsum is simply dummy text of the printing and typesetting industry. | ||
* Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, | ||
* when an unknown printer took a galley of type and scrambled it to make a type | ||
* specimen book. It has survived not only five centuries, but also the leap into | ||
* electronic typesetting, remaining essentially unchanged. | ||
*/ | ||
|
||
body { | ||
background-color: lightblue; | ||
} | ||
|
||
h1 { | ||
color: navy; | ||
margin-left: 20px; | ||
} |
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,17 @@ | ||
/** | ||
* Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
* Unauthorized copying of this file, via any medium is strictly prohibited | ||
* Proprietary and confidential | ||
* Written by John Doe <jdoe@heaven.com>, 1984 | ||
* | ||
* From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
*/ | ||
|
||
body { | ||
background-color: lightblue; | ||
} | ||
|
||
h1 { | ||
color: navy; | ||
margin-left: 20px; | ||
} |
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,17 @@ | ||
/** | ||
* Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
* Unauthorized copying of this file, via any medium is strictly prohibited | ||
* Proprietary and confidential | ||
* Written by John Doe <jdoe@heaven.com>, 1984 | ||
* | ||
* From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
*/ | ||
|
||
body { | ||
background-color: lightblue; | ||
} | ||
|
||
h1 { | ||
color: navy; | ||
margin-left: 20px; | ||
} |
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,25 @@ | ||
<!-- | ||
- Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
- Unauthorized copying of this file, via any medium is strictly prohibited | ||
- Proprietary and confidential | ||
- Written by John Doe <jdoe@heaven.com>, 1984 | ||
- | ||
- From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
--> | ||
|
||
<!DOCTYPE html> | ||
|
||
<html> | ||
|
||
<head> | ||
<title>Page Title</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<h1>This is a Heading</h1> | ||
<p>This is a paragraph.</p> | ||
|
||
</body> | ||
|
||
</html> |
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,32 @@ | ||
<!-- | ||
- Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
- Unauthorized copying of this file, via any medium is strictly prohibited | ||
- Proprietary and confidential | ||
- Written by John Doe <jdoe@heaven.com>, 1984 | ||
- | ||
- From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
--> | ||
|
||
<!-- | ||
- Lorem Ipsum is simply dummy text of the printing and typesetting industry. | ||
- Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, | ||
- when an unknown printer took a galley of type and scrambled it to make a type | ||
- specimen book. It has survived not only five centuries, but also the leap into | ||
- electronic typesetting, remaining essentially unchanged. | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Page Title</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<h1>This is a Heading</h1> | ||
<p>This is a paragraph.</p> | ||
|
||
</body> | ||
|
||
</html> |
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,24 @@ | ||
<!-- | ||
- Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
- Unauthorized copying of this file, via any medium is strictly prohibited | ||
- Proprietary and confidential | ||
- Written by John Doe <jdoe@heaven.com>, 1984 | ||
- | ||
- From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Page Title</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<h1>This is a Heading</h1> | ||
<p>This is a paragraph.</p> | ||
|
||
</body> | ||
|
||
</html> |
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,24 @@ | ||
<!-- | ||
- Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
- Unauthorized copying of this file, via any medium is strictly prohibited | ||
- Proprietary and confidential | ||
- Written by John Doe <jdoe@heaven.com>, 1984 | ||
- | ||
- From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Page Title</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<h1>This is a Heading</h1> | ||
<p>This is a paragraph.</p> | ||
|
||
</body> | ||
|
||
</html> |
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,22 @@ | ||
/** | ||
* Copyright (C) xxx Systems, Inc - All Rights Reserved | ||
* Unauthorized copying of this file, via any medium is strictly prohibited | ||
* Proprietary and confidential | ||
* Written by John Doe <jdoe@heaven.com>, 1984 | ||
* | ||
* From https://softwareengineering.stackexchange.com/a/68150/266765 | ||
*/ | ||
|
||
/** | ||
* Lorem Ipsum is simply dummy text of the printing and typesetting industry. | ||
* Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, | ||
* when an unknown printer took a galley of type and scrambled it to make a type | ||
* specimen book. It has survived not only five centuries, but also the leap into | ||
* electronic typesetting, remaining essentially unchanged. | ||
*/ | ||
|
||
function add(a, b) { | ||
return a + b; | ||
} | ||
|
||
module.exports = add; |
Oops, something went wrong.