fix: post-increment, pre-decrement working on #76

This commit is contained in:
harttle
2018-07-21 22:59:52 +08:00
parent b5cfd2d2c5
commit c3de3fbb19
12 changed files with 146 additions and 92 deletions
+27 -16
View File
@@ -5,16 +5,16 @@ const assert = require('./util/assert.js')
var Scope = {
getAll: function () {
return this.scopes.reduce((ctx, val) => Object.assign(ctx, val), Object.create(null))
return this.contexts.reduce((ctx, val) => Object.assign(ctx, val), Object.create(null))
},
get: function (path) {
let paths = this.propertyAccessSeq(path)
let scope = this.findScopeFor(paths[0])
let scope = this.findContextFor(paths[0]) || _.last(this.contexts)
return paths.reduce((value, key) => this.readProperty(value, key), scope)
},
set: function (path, v) {
let paths = this.propertyAccessSeq(path)
let scope = this.findScopeFor(paths[0])
let scope = this.findContextFor(paths[0]) || _.last(this.contexts)
paths.some((key, i) => {
if (!_.isObject(scope)) {
return true
@@ -29,28 +29,32 @@ var Scope = {
scope = scope[key]
})
},
unshift: function (ctx) {
return this.contexts.unshift(ctx)
},
push: function (ctx) {
this.scopes.push(ctx)
return this.contexts.push(ctx)
},
pop: function (ctx) {
if (!arguments.length) {
return this.scopes.pop()
return this.contexts.pop()
}
let i = this.scopes.findIndex(scope => scope === ctx)
let i = this.contexts.findIndex(scope => scope === ctx)
if (i === -1) {
throw new TypeError('scope not found, cannot pop')
}
return this.scopes.splice(i, 1)[0]
return this.contexts.splice(i, 1)[0]
},
findScopeFor: function (key) {
let i = this.scopes.length - 1
while (i >= 0 && !(key in this.scopes[i])) {
i--
findContextFor: function (key, filter) {
filter = filter || (() => true)
for (let i = this.contexts.length - 1; i >= 0; i--) {
let candidate = this.contexts[i]
if (!filter(candidate)) continue
if (key in candidate) {
return candidate
}
}
if (i < 0) {
i = this.scopes.length - 1
}
return this.scopes[i]
return null
},
readProperty: function (obj, key) {
let val
@@ -153,6 +157,13 @@ exports.factory = function (ctx, opts) {
}
var scope = Object.create(Scope)
scope.opts = _.assign(defaultOptions, opts)
scope.scopes = [ctx || {}]
scope.contexts = [ctx || {}]
return scope
}
exports.types = {
AssignScope: Object.create(null),
CaptureScope: Object.create(null),
IncrementScope: Object.create(null),
DecrementScope: Object.create(null)
}
+11 -10
View File
@@ -1,26 +1,27 @@
'use strict'
const lexical = require('./lexical.js')
const Syntax = require('./syntax.js')
const assert = require('./util/assert.js')
function hash (markup, scope) {
var obj = {}
var match
let obj = {}
let match
lexical.hashCapture.lastIndex = 0
while ((match = lexical.hashCapture.exec(markup))) {
var k = match[1]
var v = match[2]
let k = match[1]
let v = match[2]
obj[k] = Syntax.evalValue(v, scope)
}
return obj
}
module.exports = function () {
var tagImpls = {}
let tagImpls = {}
var _tagInstance = {
let _tagInstance = {
render: function (scope) {
var obj = hash(this.token.args, scope)
var impl = this.tagImpl
let obj = hash(this.token.args, scope)
let impl = this.tagImpl
if (typeof impl.render !== 'function') {
return Promise.resolve('')
}
@@ -31,7 +32,7 @@ module.exports = function () {
this.token = token
this.name = token.name
var tagImpl = tagImpls[this.name]
let tagImpl = tagImpls[this.name]
assert(tagImpl, `tag ${this.name} not found`)
this.tagImpl = Object.create(tagImpl)
if (this.tagImpl.parse) {
@@ -45,7 +46,7 @@ module.exports = function () {
}
function construct (token, tokens) {
var instance = Object.create(_tagInstance)
let instance = Object.create(_tagInstance)
instance.parse(token, tokens)
return instance
}
+2 -1
View File
@@ -1,6 +1,7 @@
const resolve = require('resolve-url')
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/
const urlRe = /^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$/
const _ = require('./underscore')
// https://github.com/jinder/path/blob/master/path.js#L567
exports.extname = function (path) {
@@ -16,7 +17,7 @@ exports.resolve = function (root, path) {
if (Object.prototype.toString.call(root) === '[object Array]') {
root = root[0]
}
if (root && root.charAt(root.length - 1) !== '/') {
if (root && _.last(root) !== '/') {
root += '/'
}
return resolve(root, path)