更新开发文档和技能索引,优化角色驱动的技能使用规范。调整了各角色的主技能和辅助技能的描述,确保开发者在不同目录下遵循相应的开发风格和流程。增加了变更关联检查的要求,确保代码变更后进行必要的检查以防漏改。

This commit is contained in:
Alex-larget
2026-02-25 11:04:08 +08:00
parent 28deb34645
commit 8e4d61e22b
369 changed files with 91393 additions and 16 deletions

View File

@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Mathias Buus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,89 @@
# generate-function
Module that helps you write generated functions in Node
```
npm install generate-function
```
[![build status](http://img.shields.io/travis/mafintosh/generate-function.svg?style=flat)](http://travis-ci.org/mafintosh/generate-function)
## Disclamer
Writing code that generates code is hard.
You should only use this if you really, really, really need this for performance reasons (like schema validators / parsers etc).
## Usage
``` js
const genfun = require('generate-function')
const { d } = genfun.formats
function addNumber (val) {
const gen = genfun()
gen(`
function add (n) {')
return n + ${d(val)}) // supports format strings to insert values
}
`)
return gen.toFunction() // will compile the function
}
const add2 = addNumber(2)
console.log('1 + 2 =', add2(1))
console.log(add2.toString()) // prints the generated function
```
If you need to close over variables in your generated function pass them to `toFunction(scope)`
``` js
function multiply (a, b) {
return a * b
}
function addAndMultiplyNumber (val) {
const gen = genfun()
gen(`
function (n) {
if (typeof n !== 'number') {
throw new Error('argument should be a number')
}
const result = multiply(${d(val)}, n + ${d(val)})
return result
}
`)
// use gen.toString() if you want to see the generated source
return gen.toFunction({multiply})
}
const addAndMultiply2 = addAndMultiplyNumber(2)
console.log(addAndMultiply2.toString())
console.log('(3 + 2) * 2 =', addAndMultiply2(3))
```
You can call `gen(src)` as many times as you want to append more source code to the function.
## Variables
If you need a unique safe identifier for the scope of the generated function call `str = gen.sym('friendlyName')`.
These are safe to use for variable names etc.
## Object properties
If you need to access an object property use the `str = gen.property('objectName', 'propertyName')`.
This returns `'objectName.propertyName'` if `propertyName` is safe to use as a variable. Otherwise
it returns `objectName[propertyNameAsString]`.
If you only pass `gen.property('propertyName')` it will only return the `propertyName` part safely
## License
MIT

View File

@@ -0,0 +1,27 @@
const genfun = require('./')
const { d } = genfun.formats
function multiply (a, b) {
return a * b
}
function addAndMultiplyNumber (val) {
const fn = genfun(`
function (n) {
if (typeof n !== 'number') {
throw new Error('argument should be a number')
}
const result = multiply(${d(val)}, n + ${d(val)})
return result
}
`)
// use fn.toString() if you want to see the generated source
return fn.toFunction({multiply})
}
const addAndMultiply2 = addAndMultiplyNumber(2)
console.log(addAndMultiply2.toString())
console.log('(3 + 2) * 2 =', addAndMultiply2(3))

View File

@@ -0,0 +1,181 @@
var util = require('util')
var isProperty = require('is-property')
var INDENT_START = /[\{\[]/
var INDENT_END = /[\}\]]/
// from https://mathiasbynens.be/notes/reserved-keywords
var RESERVED = [
'do',
'if',
'in',
'for',
'let',
'new',
'try',
'var',
'case',
'else',
'enum',
'eval',
'null',
'this',
'true',
'void',
'with',
'await',
'break',
'catch',
'class',
'const',
'false',
'super',
'throw',
'while',
'yield',
'delete',
'export',
'import',
'public',
'return',
'static',
'switch',
'typeof',
'default',
'extends',
'finally',
'package',
'private',
'continue',
'debugger',
'function',
'arguments',
'interface',
'protected',
'implements',
'instanceof',
'NaN',
'undefined'
]
var RESERVED_MAP = {}
for (var i = 0; i < RESERVED.length; i++) {
RESERVED_MAP[RESERVED[i]] = true
}
var isVariable = function (name) {
return isProperty(name) && !RESERVED_MAP.hasOwnProperty(name)
}
var formats = {
s: function(s) {
return '' + s
},
d: function(d) {
return '' + Number(d)
},
o: function(o) {
return JSON.stringify(o)
}
}
var genfun = function() {
var lines = []
var indent = 0
var vars = {}
var push = function(str) {
var spaces = ''
while (spaces.length < indent*2) spaces += ' '
lines.push(spaces+str)
}
var pushLine = function(line) {
if (INDENT_END.test(line.trim()[0]) && INDENT_START.test(line[line.length-1])) {
indent--
push(line)
indent++
return
}
if (INDENT_START.test(line[line.length-1])) {
push(line)
indent++
return
}
if (INDENT_END.test(line.trim()[0])) {
indent--
push(line)
return
}
push(line)
}
var line = function(fmt) {
if (!fmt) return line
if (arguments.length === 1 && fmt.indexOf('\n') > -1) {
var lines = fmt.trim().split('\n')
for (var i = 0; i < lines.length; i++) {
pushLine(lines[i].trim())
}
} else {
pushLine(util.format.apply(util, arguments))
}
return line
}
line.scope = {}
line.formats = formats
line.sym = function(name) {
if (!name || !isVariable(name)) name = 'tmp'
if (!vars[name]) vars[name] = 0
return name + (vars[name]++ || '')
}
line.property = function(obj, name) {
if (arguments.length === 1) {
name = obj
obj = ''
}
name = name + ''
if (isProperty(name)) return (obj ? obj + '.' + name : name)
return obj ? obj + '[' + JSON.stringify(name) + ']' : JSON.stringify(name)
}
line.toString = function() {
return lines.join('\n')
}
line.toFunction = function(scope) {
if (!scope) scope = {}
var src = 'return ('+line.toString()+')'
Object.keys(line.scope).forEach(function (key) {
if (!scope[key]) scope[key] = line.scope[key]
})
var keys = Object.keys(scope).map(function(key) {
return key
})
var vals = keys.map(function(key) {
return scope[key]
})
return Function.apply(null, keys.concat(src)).apply(null, vals)
}
if (arguments.length) line.apply(null, arguments)
return line
}
genfun.formats = formats
module.exports = genfun

View File

@@ -0,0 +1,32 @@
{
"name": "generate-function",
"version": "2.3.1",
"description": "Module that helps you write generated functions in Node",
"main": "index.js",
"scripts": {
"test": "tape test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/mafintosh/generate-function"
},
"keywords": [
"generate",
"code",
"generation",
"function",
"performance"
],
"author": "Mathias Buus",
"license": "MIT",
"bugs": {
"url": "https://github.com/mafintosh/generate-function/issues"
},
"homepage": "https://github.com/mafintosh/generate-function",
"devDependencies": {
"tape": "^4.9.1"
},
"dependencies": {
"is-property": "^1.0.2"
}
}

View File

@@ -0,0 +1,49 @@
var tape = require('tape')
var genfun = require('./')
tape('generate add function', function(t) {
var fn = genfun()
('function add(n) {')
('return n + %d', 42)
('}')
t.same(fn.toString(), 'function add(n) {\n return n + 42\n}', 'code is indented')
t.same(fn.toFunction()(10), 52, 'function works')
t.end()
})
tape('generate function + closed variables', function(t) {
var fn = genfun()
('function add(n) {')
('return n + %d + number', 42)
('}')
var notGood = fn.toFunction()
var good = fn.toFunction({number:10})
try {
notGood(10)
t.ok(false, 'function should not work')
} catch (err) {
t.same(err.message, 'number is not defined', 'throws reference error')
}
t.same(good(11), 63, 'function with closed var works')
t.end()
})
tape('generate property', function(t) {
var gen = genfun()
t.same(gen.property('a'), 'a')
t.same(gen.property('42'), '"42"')
t.same(gen.property('b', 'a'), 'b.a')
t.same(gen.property('b', '42'), 'b["42"]')
t.same(gen.sym(42), 'tmp')
t.same(gen.sym('a'), 'a')
t.same(gen.sym('a'), 'a1')
t.same(gen.sym(42), 'tmp1')
t.same(gen.sym('const'), 'tmp2')
t.end()
})